Friday, June 23, 2017

how to check list of opened and closed port in linux ?

There are different commands on both Linux and UNIX server to see what TCP/UDP ports are listening or open on your server. You can use netstat command, which prints network connections, routing tables, interface statistics, masquerade connections, and multicast memberships, etc.

Method 1:
netstat command to find open ports

Syntax :

netstat --listen

Or

netstat -l

Example:

ravi@linuxforfreshers.com>>sudo netstat --listen
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State     
tcp        0      0 *:1234                  *:*                     LISTEN    
tcp        0      0 *:8084                  *:*                     LISTEN    
tcp        0      0 192.168.122.1:domain    *:*                     LISTEN    
tcp        0      0 *:ssh                   *:*                     LISTEN    
tcp        0      0 *:ipp                   *:*                     LISTEN    
tcp        0      0 *:microsoft-ds          *:*                     LISTEN    
tcp        0      0 *:7070                  *:*                     LISTEN    
tcp        0      0 localhost:mysql         *:*                     LISTEN    
tcp        0      0 *:netbios-ssn           *:*                     LISTEN       
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN    
tcp6       0      0 [::]:ipp                [::]:*                  LISTEN    
tcp6       0      0 [::]:microsoft-ds       [::]:*                  LISTEN    
tcp6       0      0 [::]:netbios-ssn        [::]:*                  LISTEN    
udp        0      0 *:39505                 *:*                               
udp        0      0 *:ipp                   *:*                               
udp        0      0 *:mdns                  *:*                               
udp        0      0 *:mdns                  *:*                               

Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     12950    /var/run/acpid.socket
unix  2      [ ACC ]     STREAM     LISTENING     18259042 @atpl-com.canonical.Unity.Scope.rhythmbox.T516689809663571
unix  2      [ ACC ]     STREAM     LISTENING     19096    /run/user/1000/keyring-n7CcyZ/control
unix  2      [ ACC ]     STREAM     LISTENING     22589    @/tmp/.ICE-unix/3779
unix  2      [ ACC ]     STREAM     LISTENING     21540    @/tmp/dbus-u6IauIGH5I

To display open ports and established TCP connections, enter:

netstat -vatn

Example:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State     
tcp        0      0 0.0.0.0:1234            0.0.0.0:*               LISTEN    
tcp        0      0 0.0.0.0:8084            0.0.0.0:*               LISTEN    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:631             0.0.0.0:*               LISTEN    
tcp        0      0 0.0.0.0:445             0.0.0.0:*               LISTEN    
tcp        0      0 0.0.0.0:7070            0.0.0.0:*               LISTEN    

To display only open UDP ports try the following command:

netstat -vaun

Example:

ravi@linuxforfreshers.com>>sudo netstat -vaun
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State     
udp        0      0 0.0.0.0:39505           0.0.0.0:*                         
udp        0      0 0.0.0.0:631             0.0.0.0:*   

Using netstat -lntu

Where
     -l = only services which are listening on some port
     -n = show port number, don't try to resolve the service name
     -t = tcp ports
     -u = udp ports
     -p = name of the program          


Method 2:

Using lsof Command

To display the list of open ports, enter:
# lsof -i
To display all open files, use:
# lsof

To display all open IPv4 network files in use by the process whose PID is 10050, use:
# lsof -i 4 -a -p 10050

Another example:
# lsof -iTCP -sTCP:LISTEN

Method 3:

Using telnet

Quickest way to test if a TCP port is open (including any hardware firewalls you may have), is to type, from a remote computer (e.g. your desktop):

Syntax:

telnet hostip port_number

Example 1:

ravi@linuxforfreshers.com>>telnet 192.168.101.156 22
Trying 192.168.101.156...
Connected to 192.168.101.156
Escape character is '^]'.

Example 2:

ravi@linuxforfreshers.com>>telnet localhost 22
Trying 127.0.0.1...
Connected to localhost.

Method 3:

Using ss command

Syntax:
ss -lntu
Example:

ravi@linuxforfreshers.com>>ss -lntu
Netid State      Recv-Q Send-Q                                                                                     Local Address:Port                                                                                       Peer Address:Port
tcp   UNCONN     0      0                                                                                                      *:39505                                                                                                 *:*    
tcp   UNCONN     0      0                                                                                                      *:631                                                                                                   *:*    
tcp   UNCONN     0      0                                                                                                      *:5353                                                                                                  *:*



No comments:

Post a Comment