Introduction
The Netstat command is used to display various network-related information, such as network connections, routing tables, Interface states (Interface Statistics), masquerade connections, multicast members (multicast memberships), and so on.
output message meaning
After executing the netstat, the output is
Active Internet connections (w/o servers) Proto recv-q send-q Local address Foreign address statetcp 0 2 210.34.6.89:telnet 210.34.6.96:2873 establishedtcp 296 0 210.34.6.89:1165 210.34.6.84:netbios-ssn establishedtcp 0 0 localhost.localdom : 9001 localhost.localdom:1162 establishedtcp 0 0 localhost.localdom:1162 localhost.localdom:9001 ESTABLISHEDtcp 0 80 210.34.6.89:1161 210.34.6.10:netbios-ssn closeactive UNIX domain sockets (w/o servers) Proto refcnt Flags Type State I-node Pathunix 1 [] Stream CONNECTED 16178 @000000ddunix 1 [] Stream CONNECTED 16176 @000000dcunix 9 [] Dgram 5292/dev/logu Nix 1 [] STREAM CONNECTED 16182 @000000df
As a whole, the output of Netstat can be divided into two parts:
One is the active Internet connections, called the active TCP connection, where "recv-q" and "Send-q" refer to the Receive queue and the send queue for%0a. These figures are generally supposed to be 0. If not, it means that the package is accumulating in the queue. This situation can only be seen in very few cases.
The other is the active UNIX domain sockets, known as the active UNIX Domains socket interface (as with network sockets, but only for native communication, which can be increased by one-fold performance).
Proto shows the protocol used by the connection, refcnt represents the process number connected to this set of interfaces, types shows the type of the socket interface, state shows the current status of the socket interface, and path represents the pathname used by other processes connected to the socket.
Common Parameters
-A (All) displays all options and does not show listen related by default
-T (TCP) displays only TCP-related options
-U (UDP) displays only UDP-related options
-N refuses to display aliases, showing all numbers converted to numbers.
-l list only service status in Listen (listening)
-P Displays the program name that establishes the associated link
-R display routing information, routing table
-e display extended information, such as UID, etc.
-S statistics according to each protocol
-C executes the netstat command every other fixed time.
Hint: The status of listen and listening can only be seen with-a or-l
Practical Command Instances
1. List all ports (including listening and not listening)
List all ports netstat-a
# Netstat-a | More Active Internet connections (servers and established) Proto recv-q send-q Local address Foreign address State TCP 0 0 localhost:30037 *:* LISTEN UDP 0 0 *:bootpc *:* Active UNIX domain sockets ( Servers and established) Proto refcnt Flags Type State i-node Path Unix 2 [ACC] STREAM LISTENING 6135 /tmp/. X11-unix/x0 UNIX 2 [ACC] STREAM LISTENING 5140 /var/run/acpid.socket
List all TCP ports Netstat-at
# Netstat-at Active Internet connections (servers and established) Proto recv-q send-q Local Address Foreign address< C1/>state TCP 0 0 localhost:30037 *:* LISTEN tcp 0 0 Localhost:ipp *:* LISTEN TCP 0 0 *:smtp *:* LISTEN tcp6 0 0 Localhost:ipp [::]:* LISTEN
List all UDP ports Netstat-au
# Netstat-au Active Internet connections (servers and established) Proto recv-q send-q Local address Foreign Address State UDP 0 0 *:bootpc *:* UDP 0 0 *:49119 *:* UDP 0 0 *:mdns *:*
2. List all Sockets in the listening state
Display only the listening port netstat-l
# netstat-l Active Internet connections (only servers) Proto recv-q send-q Local address Foreign address State TC P 0 0 Localhost:ipp *:* LISTEN tcp6 0 0 Localhost:ipp [::]:* LISTEN UDP 0 0 *:49119 *:*
List all listening TCP ports only NETSTAT-LT
# netstat-lt Active Internet connections (only servers) Proto recv-q send-q Local address Foreign address State TCP 0 0 localhost:30037 *:* LISTEN tcp 0 0 *:smtp *:* LISTEN tcp6 0 0 Localhost:ipp [::]:* LISTEN
List all listening UDP ports only Netstat-lu
# Netstat-lu Active Internet connections (only servers) Proto recv-q send-q Local address Foreign address State u DP 0 0 *:49119 *:* UDP 0 0 *:mdns *:*
List all listening UNIX ports only NETSTAT-LX
# NETSTAT-LX Active UNIX domain sockets (only servers) Proto refcnt Flags Type State i-node Path UNIX 2 [ACC] Stream LISTENING 6294 private/maildrop Unix 2 [ACC] stream LISTENING 6203 public/cleanup Unix 2 [ACC] STREAM LISTENING 6302 private/ifmail Unix 2 [ACC] STREAM LISTENING 6306 private/bsmtp
3. Display statistics for each protocol
Displays statistics for all ports netstat-s
# netstat-s ip:11150 Total Packets received 1 with invalid addresses 0 forwarded 0 incoming packets discarded 11149 Inco Ming packets delivered 11635 requests sent out icmp:0 ICMP messages received 0 input ICMP message failed. tcp:582 Active Connections Openings 2 failed connection attempts + connection resets received udp:1183 packets received 4 packets to unknown Port received. .....
Displays statistics for TCP or UDP ports netstat-st or-SU
# netstat-st # NETSTAT-SU
4. Display the PID and process name in the netstat output netstat-p
Netstat-p can be used with other switches, you can add "Pid/process name" to the netstat output, so debugging can easily find a specific port to run the program.
# netstat-pt Active Internet connections (w/o servers) Proto recv-q send-q Local address Foreign address State
pid/program name TCP 1 0 ramesh-laptop.loc:47212 192.168.185.75:www close_wait 2109/firefox TCP 0 0 ramesh-laptop.loc:52750 lax:www established 2109/firefox
5. Host, port, and user name are not displayed in the Netstat output
When you don't want to let the host, port and user name display, use Netstat-n. Numbers will be used instead of those names.
The output can also be accelerated because there is no comparison query.
# Netstat-an
If you just don't want one of these three names to be displayed, use the following command
# netsat-a--numeric-ports# netsat-a--numeric-hosts# netsat-a--numeric-users
6. Continuous output of netstat information
Netstat will output network information every second.
# netstat-c Active Internet connections (w/o servers) Proto recv-q send-q Local address Foreign address State TCP 0 0 ramesh-laptop.loc:36130 101-101-181-225.ama:www established TCP 1 1 ramesh-laptop.loc:52564 101.11.169.230:www CLOSING TCP 0 0 ramesh-laptop.loc:43758 server-101-101-43-2:www established TCP 1 1 ramesh-laptop.loc:42367 101.101.34.101:www CLOSING ^c
7. Show address families not supported by the system (addr families)
Netstat--verbose
At the end of the output, there is the following information
Netstat:no support for ' AF-IPX ' on the ' system.netstat:no support for ' AF AX25 ' on this system.netstat:no AF X25 ' on the System.netstat:no support for ' AF Netrom ' on the This system.
8. Display Core routing information Netstat-r
# netstat-r Kernel IP routing table Destination Gateway genmask Flags MSS Window Irtt Iface 192.168.1.0 * 255.255.255.0 u 0 0 0 eth2 link-local * 255.255.0.0 U 0 0 0 eth2 default 192.168.1.1 0.0.0.0 UG 0 0 0 eth2
Note: use NETSTAT-RN to display the number format without querying the host name.
9. Find out which port the program is running on
Not all processes can be found, no permissions will not be displayed, use root permissions to view all the information.
# Netstat-ap | grep ssh TCP 1 0 dev-db:ssh 101.174.100.22:39213 close_wait -tcp 1 0 dev-db:ssh 101.174.100.22:57643 Close_wait -
Locate the process running on the specified port
# Netstat-an | grep ': 80 '
10. Display the list of network interfaces
# netstat-i Kernel Interface table Iface MTU Met rx-ok rx-err rx-drp rx-ovr tx-ok tx-err tx-drp tx-ovr Flg et H0 0 0 0 0 0 0 0 0 0 BMU eth2 0 26196 0 0 0 26883 6 0 0 Bmru lo 16436 0 4 0 0 0 4 0 0 0 LRU
Show details like ifconfig using Netstat-ie:
# Netstat-ie Kernel Interface table eth0 Link encap:ethernet HWaddr 00:10:40:11:11:11 up broadcast multicast
mtu:1500 metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) memory:f6ae0000-f6b00000
IP and TCP analysis
View the IP addresses that connect to a service port
[Email protected]:~$ Netstat-nat | grep "192.168.1.15:22" |awk ' {print $} ' |awk-f: ' {print '} ' |sort|uniq-c|sort-nr|head-2018 221.136.168.363 154.74.45.2 422 78.173.31.2362 62.183.207.982 192.168.1.142 182.48.111.2152 124.193.219.342 119.145.41.22 114.255.41.301 75.102.11.99
TCP Various status lists
[Email protected]:~$ netstat-nat |awk ' {print $6} ' established] foreignlistentime_waitestablishedtime_waitsyn_sent
[PHP]View Plaincopyprint?
- <span style="line-height:1.5; Font-size:14px "> First take out the state, then use UNIQ-C statistics, then sort. </span>
[Email protected]:~$ netstat-nat |awk ' {print $6} ' |sort|uniq-c143 ESTABLISHED1 fin_wait11 Foreign1 last_ack36 LISTEN6 SY N_SENT113 Time_wait1 established)
[PHP]View Plaincopyprint?
- <span style="line-height:1.5; Font-size:14px "> The final command is as follows:</span>
Netstat-nat |awk ' {print $6} ' |sort|uniq-c|sort-rn
[PHP]View Plaincopyprint?
- <span style="line-height:1.5; Font-size:14px "><strong> Analytics Access.log get access to the top 10 IP addresses </strong></span>
awk ' {print '} ' Access.log |sort|uniq-c|sort-nr|head-10
Linux netstat command detailed