In this article, We introduces how to use the Netstat command in Linux/Ubuntu. Detailed explanations of basic Netstat Linux command, along with advanced options and illustrated examples, are provided to help users gain a better understanding of this tool and use it effectively in network management.
How to Install and Use Netstat Command Linux / Ubuntu
Netstat in Linux is an essential utility to help administrators troubleshoot network connectivity problems by providing detailed information about network connection status, ports in use, and data transfer between applications. If the netstat command is not available on your Ubuntu or Linux system, you can use the command to install it, the net-tools package also includes additional utilities such as arp, ifconfig, rarp, nameif and route.
sudo apt install net-tools
Some examples and explanations of netstat command Linux/Ubuntu
netstat -a;
The netstat command Linux display all open connections and sockets on the system. It is useful for troubleshooting network connectivity problems and monitoring running applications.
For example, if you are having trouble connecting to a MySQL database, you can use the command netstat -a
on Linux or Ubuntu to determine which TCP connections are waiting for the port MySQL is using (usually port 3306). If you see a lot of TCP connections waiting for this port, it is possible that the MySQL server is overloaded and does not have enough resources to handle the new connection requests.
Thereby you can use the netstat command in linux to find and fix the problem by increasing server resources or optimizing the database to reduce the load on MySQL.
netstat -t;
The netstat command in Linux shows only active TCP netstat -t
connections. It is useful when you want to check the TCP connections established on the system and help identify network connection problems on the TCP protocol.
For example, to check the connection to MySQL on TCP port 3306 in Linux/Ubuntu, we can use the netstat -t command and filter the results using grep, which helps check the network connection status of the ports and application on the system.
netstat -t | grep 3306
The results will show all active connections to port 3306, including established connections and pending connections, when using the netstat command in linux. If any connections are blocked, you can check your firewall configurations or other security software to make sure they are not blocking connections to TCP port 3306.
netstat -p;
This command shows the process using connections and ports. The command netstat -p
in Ubuntu or in is very useful to determine which applications are using your network connection and ports on your system.
netstat -s;
The command netstat -s
is used to display statistics of network statistics such as TCP, UDP, ICMP, IP protocols, network errors and more. Here are some specific examples of using the command netstat -s
on Ubuntu and Linux servers:
- Display statistics for TCP protocol:
netstat -s | grep TCP
- Display statistics for UDP protocol:
netstat -s | grep UDP
- Display statistics for ICMP protocol:
netstat -s | grep ICMP
netstat -n;
This command displays all active connections and sockets on your system, but does not perform IP address resolution to domain names. It is useful when you want to see the real IP address of the network connections on your system.
The command example netstat
in Linux with options -n
and screenshot below shows the 5 connections shown, including:
- A TCP connection is in “CLOSING” state, the local IP address is 192.168.1.141, the local port is 42626, the remote connection partner’s IP address is 35.174.127.31 and the port is remote is 443.
- Four TCP6 connections are active on different IPv6 addresses, including “TIME_WAIT”, “ESTABLISHED” and “CLOSE_WAIT” states.
netstat -i;
This command will display statistics about network interfaces. Using the netstat command in linux with the option -i
, you can view the number of packets sent and received over each interface, the total amount of data transferred over each interface, the number of errors that occurred, and other information related to the network interface. regarding network interfaces. This command helps you to monitor and manage the performance of network interfaces.
The result of the “netstat -i” command displays a statistics table of network interfaces on the computer, including information such as:
- Iface: the name of the network interface
- MTU: the maximum size of the packet transmitted over that interface
- RX-OK: number of packets received without error
- RX-ERR: number of packets received in error
- RX-DRP: number of packets rejected due to error or buffer overload
- RX-OVR: number of packets rejected due to buffer overflow
- TX-OK: number of packets sent successfully
- TX-ERR: number of packets sent in error
- TX-DRP: number of packets rejected due to error or buffer overload
- TX-OVR: number of packets rejected due to buffer overflow
- flg: interface flags
In the example in the screenshot above, there are two interfaces listed: eno1 and lo.
netstat -r;
This command will display the computer’s routing table. When using netstat command in ubuntu with -r
this option you can see the list of current routes on your computer. This command is useful when you want to determine the path of packets and helps you to check the correctness of the routing table.
netstat -g;
The Linux command netstat
with the option -g
will display information about the multicast groups in use on the computer. Using this command, you can view the list of multicast groups on your computer and the IP addresses of the group members. The netstat command in linux -g
helps you manage multicast groups on your network.
netstat -c;
This command will display the results continuously and automatically update every second. Using this command, you can view changes related to the network connections on your computer. This command is useful for monitoring network activity and detecting network connectivity problems.
For example, to use the netstat command in Linux or Ubuntu with the option to -c
continuously view the current active TCP connections on the system, we can use the following command:
netstat -c -t
The results will be displayed continuously and updated every second, helping us to monitor the active connections on the system quickly and conveniently.
Those are some shares and tutorials on how to use the netstat command in Linux and specific examples to help network administrators and sysadmins to check and analyze network connections on the system. system more effectively. Mastering how to use this netstat command helps detect and resolve network connection problems quickly and accurately.