|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
|
The netstat command is used to query TCP/IP about the network status of the local host. This command is located in the /usr/bin directory. When used with the -i option, netstat displays the state of the Ethernet interfaces, with r option it displaied routing information and with -s option statistical information:
netstat -i # the state of the Enthernet interfaces
netstat -r # displays routing info
netstat -s -- statistical information
More extended list of typical idioms can be found at Sun Microsystems - BigAdmin Shell Commands:
netstat -a | grep EST | wc -l
/* Displays number active established connections to the localhost */2005-03-22 netstat -a | more
/* Show the state of all the sockets on a machine */2005-03-22 netstat -i
/* Show the state of the interfaces used for TCP/IP traffice */2005-03-22 netstat -k hme0
/* Undocumented netstat command */2005-03-22 netstat -np
/* Similar to arp -a without name resolution */2005-03-22 netstat -r
/* Show the state of the network routing table for TCP/IP traffic */2005-03-22 netstat -rn
/* Displays routing information but bypasses hostname lookup. */2005-03-22
The exact syntax of this command is very implementation-dependent. See the User's Guide or the Command Reference Manual of your implementation for full details. It is a useful tool for debugging purposes.
In general, NETSTAT will provide information on:
The NETSTAT command is implemented in TCP/IP for VM, MVS, OS/400, OS/2, DOS and all AIX systems.
Typical usage
netstat -r The -r switch can be used to display the interface list and the routing table. This information is useful when troubleshooting a network or dialup connection that is not pingable and can not ping out. This shows how information is being routed out of the computer and how it is being looped around to the local host.
|
Using Netstat For Surveillance And Troubleshooting
Two of the fundamental aspects of Linux system security and troubleshooting are knowing what services are running, and what connections and services are available. We're all familiar with
psfor viewing active services.netstatgoes a couple of steps further, and displays all available connections, services, and their status. It shows one type of service thatpsdoes not: services run from inetd or xinetd, because inetd/xinetd start them up on demand. If the service is available but not active, such as telnet, all you see inpsis either inetd or xinetd:$ ps ax | grep -E 'telnet|inetd' 520 ? Ss 0:00 /usr/sbin/inetdBut
netstatshows telnet sitting idly, waiting for a connection:$ netstat --inet -a | grep telnet tcp 0 0 *:telnet *:* LISTENThis
netstatinvocation shows all activity:$ netstat -a Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 *:telnet *:* LISTEN tcp 0 0 *:ipp *:* LISTEN tcp 0 0 *:smtp *:* LISTEN tcp 0 0 192.168.1.5:32851 nest.anthill.echid:ircd ESTABLISHED udp 0 0 *:ipp *:* Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix 2 [ ACC ] STREAM LISTENING 1065 /tmp/ksocket-carla/klaunchertDCh2b.slave-socket unix 2 [ ACC ] STREAM LISTENING 1002 /tmp/ssh-OoMGfFm666/agent.666 unix 2 [ ACC ] STREAM LISTENING 819 private/smtpYour total output will probably run to a couple hundred lines. (A fun and quick way to count lines of output is
netstat -a | wc -l.) You can ignore everything under "Active UNIX domain sockets." Those are local inter-process communications, not network connections. To avoid displaying them at all, do this:$ netstat --inet -aThis will display only network connections, both listening and established. Already
netstathas earned its keep--both the telnet and smtp services are running. This is bad, because I don't want to have either a telnet or smtp server running on this machine. So now I know I need to turn them off, and re-configure my startup files so they won't start at boot.How do you know what services you want running? That is a mondo subject for another day, and an important one. For example, if your system has been compromised, this is one place to find evidence of a Trojan horse or other malware phoning home. In this example, ipp is Internet Printing Protocol, which belongs to CUPS (Common Unix Printing System.) If you want your printer to work, this needs to be here. The connection on 192.168.1.5:32851 is my active IRC (Internet Relay Chat) connection. Refer to your
/etc/servicesfile to learn more about TCP and UDP ports, and the services assigned to them.
What It Means
"Proto" is short for protocol, which is either TCP or UDP. "Recv-Q" and "Send-Q" mean receiving queue and sending queue. These should always be zero; if they're not you might have a problem. Packets should not be piling up in either queue, except briefly, as this example shows:
tcp 0 593 192.168.1.5:34321 venus.euao.com:smtp ESTABLISHEDThat happened when I hit the "check mail" button in KMail; a brief queuing of outgoing packets is normal behavior. If the receiving queue is consistently jamming up, you might be experiencing a denial-of-service attack. If the sending queue does not clear quickly, you might have an application that is sending them out too fast, or the receiver cannot accept them quickly enough.
"Local address" is either your IP and port number, or IP and the name of a service. "Foreign address" is the hostname and service you are connected to. The asterisk is a placeholder for IP addresses, which of course cannot be known until a remote host connects. "State" is the current status of the connection. Any TCP state can be displayed here, but these three are the ones you want to see:
LISTEN- waiting to receive a connectionthis should last only a minute or two, then change back to LISTEN. The socket pair cannot be re-used as long the TIME_WAIT state persists.
ESTABLISHED- a connection is active
TIME_WAIT- a recently terminated connection;
UDP is stateless, so the "State" column is always blank.
A socket pair is both sides of a TCP/IP connection, like this example for a locally-attached printer:
localhost:ipp localhost:34493 ESTABLISHEDOr a telnet connection to a remote server:
192.168.1.5:34437 65.106.57.106.pt:telnet ESTABLISHEDA socket is any hostname-port combination, or IP address-port.
Continuous Capture, "Borken" DNS, and Interface Checking
Because all these things change often, how do you capture the changes? Run
netstatcontinuously with the-cflag and record the output:$ netstat --inet -a -c > netstat.txtThen check email, start and stop services, surf the web, log in to a telnet BBS and play Legend of the Red Dragon; then review your capture file to see what it all looks like.
If
netstatis taking too long, or not resolving a hostname at all, give it the-nflag to turn off DNS lookups:$ netstat --inet -an
netstatcan help diagnose NIC problems. Use the-iflag when you're troubleshooting a flakey connection, and you suspect your NIC:$ 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 eth0 1500 0 28698 0 0 0 33742 0 0 0 BMRU lo 16436 0 14 0 0 0 14 0 0 0 LRUYou should see large numbers in the RX-OK (received OK) and TX-OK (transmitted OK) columns, and very low numbers in all the others. If you are seeing a lot of RX-ERRs or TX-ERRs, suspect the NIC or the patch cable. This is what the flags mean:B = broadcast address
L = loopback device
M = promicuous mode
R = interface is running
U = interface is up
Resources
Linux Network Administrator's Guide, by Olaf Kirch & Terry Dawson
The netstat command displays the contents of certain network-related data structures in various formats, depending on the options you select.
The netstat command has the several forms shown in the SYNOPSIS section, above, listed as follows:
These forms are described in greater detail below.
With no arguments (the first form), netstat displays connected sockets for PF_INET, PF_INET6, and PF_UNIX, unless modified otherwise by the -f option.
You can specify multiple instances of -f to specify multiple filters. For example:
The preceding command displays routes within network 10.0.0.0/8, with mask length 8 or greater, and an output interface of either hme0 or hme1, and excludes all other routes.
The following options support interval: -i, -m, -s and -Ms. Some values are configuration parameters and are just redisplayed at each interval.
The display for each active socket shows the local and remote address, the send and receive queue sizes (in bytes), the send and receive windows (in bytes), and the internal state of the protocol.
The symbolic format normally used to display socket addresses is either:
| hostname.port |
network.port |
The numeric host address or network number associated with the socket is used to look up the corresponding symbolic hostname or network name in the hosts or networks database.
If the network or hostname for an address is not known, or if the -n option is specified, the numerical network address is shown. Unspecified, or "wildcard", addresses and ports appear as "*". For more information regarding the Internet naming conventions, refer to inet(7P) and inet6(7P).
For SCTP sockets, because an endpoint can be represented by multiple addresses, the verbose option (-v) displays the list of all the local and remote addresses.
The possible state values for TCP sockets are as follows:
The possible state values for SCTP sockets are as follows:
The form of the display depends upon which of the -g, -m, -p, or -s options you select.
The statistics use the MIB specified variables. The defined values for ipForwarding are:
The IPv6 and ICMPv6 protocol layers maintain per-interface statistics. If the -a option is specified with the -s option, then the per-interface statistics as well as the total sums are displayed. Otherwise, just the sum of the statistics are shown.
For the second, third, and fourth forms of the command, you must specify at least -g, -p, or -s. You can specify any combination of these options. You can also specify -m (the fifth form) with any set of the -g, -p, and -s options. If you specify more than one of these options, netstat displays the information for each one of them.
The interface status display lists information for all current interfaces, one interface per line. If an interface is specified using the -I option, it displays information for only the specified interface.
The list consists of the interface name, mtu (maximum transmission unit, or maximum packet size)(see ifconfig(1M)), the network to which the interface is attached, addresses for each interface, and counter associated with the interface. The counters show the number of input packets, input errors, output packets, output errors, and collisions, respectively. For Point-to-Point interfaces, the Net/Dest field is the name or address on the other side of the link.
If the -a option is specified with either the -i option or the -I option, then the output includes names of the physical interface(s), counts for input packets and output packets for each logical interface, plus additional information.
If the -n option is specified, the list displays the IP address instead of the interface name.
If an optional interval is specified, the output will be continually displayed in interval seconds until interrupted by the user or until count is reached. See OPERANDS.
The physical interface is specified using the -I option. When used with the interval operand, output for the -I option has the following format:
input eri0 output input (Total) output packets errs packets errs colls packets errs packets errs colls 227681 0 659471 1 502 261331 0 99597 1 502 10 0 0 0 0 10 0 0 0 0 8 0 0 0 0 8 0 0 0 0 10 0 2 0 0 10 0 2 0 0
If the input interface is not specified, the first interface of address family inet or inet6 will be displayed.
The routing table display lists the available routes and the status of each. Each route consists of a destination host or network, and a gateway to use in forwarding packets. The flags column shows the status of the route. These flags are as follows:
If the -a option is specified, there will be routing entries with the following flags:
Interface routes are created for each interface attached to the local host; the gateway field for such entries shows the address of the outgoing interface.
The use column displays the number of packets sent using a combined routing and address resolution (A) or a broadcast (B) route. For a local (L) route, this count is the number of packets received, and for all other routes it is the number of times the routing entry has been used to create a new combined route and address resolution entry.
The interface entry indicates the network interface utilized for the route.
The multicast routing table consists of the virtual interface table and the actual routing table.
The DHCP interface information consists of the interface name, its current state, lease information, packet counts, and a list of flags.
The states correlate with the specifications set forth in RFC 2131.
Lease information includes:
The flags currently defined include:
See attributes(5) for descriptions of the following attributes:
| ATTRIBUTE TYPE | ATTRIBUTE VALUE |
|---|---|
| Availability | SUNWcsu |
arp(1M), dhcpinfo(1), dhcpagent(1M), ifconfig(1M), iostat(1M), kstat(1M), mibiisa(1M), savecore(1M), vmstat(1M), hosts(4), inet_type(4), networks(4), protocols(4), services(4), attributes(5), kstat(7D), inet(7P), inet6(7P)
Droms, R., RFC 2131, Dynamic Host Configuration Protocol, Network Working Group, March 1997.
When displaying interface information, netstat honors the DEFAULT_IP setting in /etc/default/inet_type. If it is set to IP_VERSION4, then netstat will omit information relating to IPv6 interfaces, statistics, connections, routes and the like.
However, you can override the DEFAULT_IP setting in /etc/default/inet_type on the command-line. For example, if you have used the command-line to explicitly request IPv6 information by using the inet6 address family or one of the IPv6 protocols, it will override the DEFAULT_IP setting.
If you need to examine network status information following a kernel crash, use the mdb(1) utility on the savecore(1M) output.
The netstat utility obtains TCP statistics from the system by opening /dev/tcp and issuing queries. Because of this, netstat might display an extra, unused connection in IDLE state when reporting connection status.
Previous versions of netstat had undocumented methods for reporting kernel statistics published using the kstat(7D) facility. This functionality has been removed. Use kstat(1M) instead.
netstatnetstat [options] [delay] TCP/IP command. Show network status. Print information on active sockets, routing tables, interfaces, masquerade connections, or multicast memberships. By default, netstat lists open sockets. When a delay is specified, netstat will print new information every delay seconds. OptionsThe first five options (-g, -i, -M, -r, and -s) determine what kind of information netstat should display.
|
Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.
Q1. Which command (and options) will show the routing table, but will bypass hostname lookup ?
A: netstat –nr
Q2. Which command (and options) will show the state of all sockets ?
A: netstat –a
Last modified: February 28, 2008