netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both incoming and outgoing, as well as information about the routing tables on a system.

The basic syntax for the command is:

netstat [options]

When the netstat command is run without any options, it will display a list of active network connections. The output of the command will include the protocol (TCP or UDP), the local and remote addresses, and the state of the connection. For example:

netstat

The netstat command has a number of options that can be used to customize its behavior. Some of the most commonly used options include:

  • -a: This option will display all active connections, including listening connections.
  • -t: This option will display only TCP connections.
  • -u: This option will display only UDP connections.
  • -l: This option will display only listening connections.
  • -n: This option will display the IP addresses and port numbers in numerical form, rather than resolving them to hostnames or service names.
  • -r: This option will display the routing table for the system.
  • -p: This option will display the process ID (PID) and name of the program that is responsible for each connection.

The netstat command can also be used to display various network statistics, such as the number of packets sent and received, the number of errors, and the number of collisions. To view these statistics, you can use the -s option with the netstat command. For example:

netstat -s

The netstat command can also be used to display the state of a specific connection. To view the state of a connection, you can use the -c option with the netstat command, and specify the connection by protocol, local address, and local port number. For example:

netstat -c -t -l

Additionally, the netstat command can also be used to display the status of a specific protocol. To view the status of a protocol, you can use the -s option with the netstat command, and specify the protocol by name. For example:

netstat -s -t

Another useful feature of the netstat command is its ability to monitor network connections in real-time. By using the -i option, you can display the number of packets and bytes sent and received on a network interface, and by using the -w option, you can specify the time interval for which the statistics are displayed.

netstat -i -w 2

This command will display the statistics for all network interfaces, updating the statistics every 2 seconds.

The netstat command can also be used to display the status of the multicast groups that a system is a member of. To display the status of multicast groups, you can use the -g option with the netstat command.

netstat -g

In summary, the netstat command in Linux is a powerful command-line utility that can be used to display a wide range of network information, including active connections, routing tables, network statistics, and protocol status. It can also be used to monitor network connections in real-time, and display the status of multicast groups. With the various options available, the netstat command can be used in a variety of ways to troubleshoot and monitor network issues on a Linux system.