The netstat command in Linux is used to display network statistics and information about network connections, routing tables, and network interfaces on a system. It provides valuable insights into network activities and helps troubleshoot network-related issues.
Here’s an overview of how to use the netstat command:
- Displaying Network Connections:
To display active network connections, use the following syntax:
netstat -tuln
This command shows all TCP (-t) and UDP (-u) connections with their respective ports (-n) and listening (-l) status.
- Displaying Network Interfaces:
To display information about network interfaces, use the following syntax:
netstat -i
This command lists all network interfaces along with their associated statistics, such as packets and errors.
- Displaying Routing Table:
To display the routing table, use the following syntax:
netstat -r
This command shows the routing table, including destination networks, gateways, and interface information.
- Displaying Network Statistics:
To display general network statistics, use the following syntax:
netstat -s
This command provides detailed statistics about different network protocols, including TCP, UDP, IP, and ICMP.
- Filtering Output:
You can filter the output ofnetstatusing various options. Some commonly used options include:
- Filtering by Protocol: Use
-tfor TCP,-ufor UDP, and-pfollowed by the protocol name to filter by a specific protocol. - Filtering by Port: Use
-pfollowed by the port number to filter by a specific port. - Filtering by IP Address: Use
-nto display IP addresses instead of hostnames and use-sfollowed by the IP address to filter by a specific IP.
- Examples:
- Display all active TCP and UDP connections:
netstat -tuln - Display network interfaces and their statistics:
netstat -i - Display the routing table:
netstat -r - Display network statistics for all protocols:
netstat -s - Filter output to display TCP connections on a specific port (e.g., port 80):
netstat -tuln | grep :80 - Filter output to display connections associated with a specific IP address (e.g., 192.168.0.1):
netstat -tuln | grep 192.168.0.1
The netstat command is a versatile tool for examining network connections, interfaces, routing tables, and statistics. It helps administrators monitor network activities, identify network-related issues, and analyze network performance.
For more information about the netstat command and its options, you can refer to the manual page by typing man netstat in your terminal.