View your Linux server's network connections with netstat (2024)

I shared some important first steps to help manage your personal Linux server in a previous article. I briefly mentioned monitoring network connections for listening ports, and I want to expand on this by using the netstat command for Linux systems.

Service monitoring and port scanning are standard industry practices. There's very good software like Prometheus to help automate the process, and SELinux to help contextualize and protect system access. However, I believe that understanding how your server connects to other networks and devices is key to establishing a baseline of what's normal for your server, which helps you recognize abnormalities that may suggest a bug or intrusion. As a beginner, I've discovered that the netstat command provides important insight into my server, both for monitoring and network troubleshooting.

Netstat and similar network monitoring tools, grouped together in the net-tools package, display information about active network connections. Because services running on open ports are often vulnerable to exploitation, practicing regular network monitoring can help you detect suspicious activity early.

Installnetstat

Netstat is frequently pre-installed on Linux distributions. If netstat is not installed on your server, install it with your package manager. On a Debian-based system:

$ sudo apt-get install net-tools

For Fedora-based systems:

$ dnf install net-tools

Usenetstat

On its own, the netstat command displays all established connections. You can use the netstat options above to specify the intended output further. For example, to show all listening and non-listening connections, use the --all (-a for short) option. This returns a lot of results, so in this example I pipe the output to head to display just the first 15 lines of output:

$ netstat --all | head -n 15Active Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 *:27036 *:* LISTEN tcp 0 0 localhost:27060 *:* LISTEN tcp 0 0 *:16001 *:* LISTEN tcp 0 0 localhost:6463 *:* LISTEN tcp 0 0 *:ssh *:* LISTEN tcp 0 0 localhost:57343 *:* LISTEN tcp 0 0 *:ipp *:* LISTEN tcp 0 0 *:4713 *:* LISTEN tcp 0 0 10.0.1.222:48388 syd15s17-in-f5.1e:https ESTABLISHEDtcp 0 0 10.0.1.222:48194 ec2-35-86-38-2.us:https ESTABLISHEDtcp 0 0 10.0.1.222:56075 103-10-125-164.va:27024 ESTABLISHEDtcp 0 0 10.0.1.222:46680 syd15s20-in-f10.1:https ESTABLISHEDtcp 0 0 10.0.1.222:52730 syd09s23-in-f3.1e:https ESTABLISHED

To show only TCP ports, use the --all and --tcp options, or -at for short:

$ netstat -at | head -n 5Active Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 *:27036 *:* LISTEN tcp 0 0 localhost:27060 *:* LISTEN tcp 0 0 *:16001 *:* LISTEN

To show only UDP ports, use the --all and --udp options, or -au for short:

$ netstat -au | head -n 5Active Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:27036 *:* udp 0 0 10.0.1.222:44741 224.0.0.56:46164 ESTABLISHEDudp 0 0 *:bootpc 

The options for netstat are often intuitive. For example, to show all listening TCP and UDP ports with process ID (PID) and numerical address:

$ sudo netstat --tcp --udp --listening --programs --numericActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Addr State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 2500/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1726/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1721/cupsd tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 4023/sshd: tux@ tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::22 :::* LISTEN 1726/sshd tcp6 0 0 ::1:631 :::* LISTEN 1721/cupsd tcp6 0 0 ::1:6010 :::* LISTEN 4023/sshd: tux@ udp 0 0 0.0.0.0:40514 0.0.0.0:* 1499/avahi-daemon: udp 0 0 192.168.122.1:53 0.0.0.0:* 2500/dnsmasq udp 0 0 0.0.0.0:67 0.0.0.0:* 2500/dnsmasq udp 0 0 0.0.0.0:111 0.0.0.0:* 1/systemd udp 0 0 0.0.0.0:5353 0.0.0.0:* 1499/avahi-daemon: udp6 0 0 :::111 :::* 1/systemd udp6 0 0 :::44235 :::* 1499/avahi-daemon: udp6 0 0 :::5353 :::* 1499/avahi-daemon:

The short version of this common combination is -tulpn.

To display information about a specific service, filter with grep:

$ sudo netstat -anlp | grep cupstcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1721/cupsd tcp6 0 0 ::1:631 :::* LISTEN 1721/cupsdunix 2 [ ACC ] STREAM LISTENING 27251 1/systemd /var/run/cups/cups.sockunix 2 [ ] DGRAM 59530 1721/cupsdunix 3 [ ] STREAM CONNECTED 55196 1721/cupsd /var/run/cups/cups.sock

More Linux resources

Next steps

Once you've run the netstat command, you can take steps to secure your system by ensuring that only services that you actively use are listening on your network.

  1. Recognize commonly exploited ports and services. As a general rule, close the ports you're not actually using.
  2. Be on the lookout for uncommon port numbers, and learn to recognize legitimate ports in use on your system.
  3. Pay close attention to SELinux errors. Sometimes all you need to do is update contexts to match a legitimate change you've made to your system, but read the errors to make sure that SELinux isn't alerting you of suspicious or malicious activity.

If you find that a port is running a suspicious service, or you simply want to close a port that you no longer use, you can manually deny port access through firewall rules by following these steps:

If you're using firewall-cmd, run these commands:

$ sudo firewall-cmd –remove-port=<port number>/tcp$ sudo firewall-cmd –runtime-to-permanent

If you're using UFW, run the following command:

$ sudo ufw deny <port number>

Next, stop the service itself using systemctl:

$ systemctl stop <service>

Learn netstat

Netstat is a useful tool to quickly collect information about your server's network connections. Regular network monitoring is important an important part of getting to know your system, and it helps you keep your system safe. To incorporate this step into your administrative routine, you can use network monitoring tools like netstat or ss, as well as open source port scanners such as Nmap or sniffers like Wireshark, which allow for scheduled tasks.

As servers house larger amounts of personal data, it's increasingly important to ensure the security of personal servers. By understanding how your server connects to the Internet, you can decrease your machine's vulnerability, while still benefiting from the growing connectivity of the digital age.

View your Linux server's network connections with netstat (1)This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.

View your Linux server's network connections with netstat (2024)

FAQs

How to check network connections using netstat? ›

Step 1: In the search bar type “cmd” (Command Prompt) and press enter. This would open the command prompt window. “netstat -a” shows all the currently active connections and the output display the protocol, source, and destination addresses along with the port numbers and the state of the connection.

How to check Linux network connections? ›

[LINUX] Checking the Network Connection
  1. Open Terminal (CTRL+ALT+T)
  2. Type ip addr show.
  3. NOTE: If no IP Address is assigned, please restart the device.
Dec 27, 2022

How do I see total connections in Linux? ›

We use the netstat command to check connection numbers on the server.

How to check server to server connection in Linux? ›

Use netstat

$ sudo netstat --tcp --udp --listening --programs --numeric Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Addr State PID/Program name tcp 0 0 0.0. 0.0:111 0.0.

How do I check my network connections? ›

Select the Start button, then type settings. Select Settings > Network & internet. The status of your network connection will appear at the top. Windows 10 lets you quickly check your network connection status.

How do I show all network connections? ›

How to use the netstat command to view network connections
  1. Click the 'Start' button.
  2. Enter 'cmd' into the search bar to open the command prompt.
  3. Wait for command prompt (black window) to appear. ...
  4. Enter 'netstat -a' to view current connections. ...
  5. Enter 'netstat -b' to see the programs using connections.
May 6, 2019

How to check netstat in Linux? ›

How to Monitor Network Services Using Netstat Command
  1. Netstat is a tool in Linux which can be used to monitor and report information on network services. ...
  2. netstat -ant | grep 80. ...
  3. netstat -an |grep :80 |wc –l. ...
  4. netstat -ant | grep LISTEN.

What is the netstat command in Linux? ›

Netstat is a command line utility for Linux that prints network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. Netstat can be used to diagnose network issues and service problems.

How to check server to server connectivity? ›

How to run a ping network test
  1. Type “cmd” to bring up the Command Prompt.
  2. Open the Command Prompt.
  3. Type “ping” in the black box and hit the space bar.
  4. Type the IP address you'd like to ping (e.g., 192.XXX.X.X).
  5. Review the ping results displayed.

What are the netstat commands? ›

netstat commands using Windows as an example
[OPTION]CommandDescription
netstatStandard listing of all active connections
-anetstat -aDisplays all active ports
-enetstat -eShows statistics about your network connection (received and sent data packets, etc.)
-inetstat -iBrings up the netstat overview menu
5 more rows
Mar 1, 2023

What replaced netstat? ›

Formally, ss is the socket statistics command that replaces netstat .

How to check network issues in Linux? ›

Linux command:
  1. ip addr. Windows command:
  2. ipconfig. Confirm that the default gateway and DNS server values are accurate. ...
  3. ip addr. Windows command:
  4. ipconfig /all. Ping the router and the DNS server. ...
  5. ping <IP> ...
  6. ping <hostname> ...
  7. ipconfig /flushdns.
Jul 14, 2023

How to check network connection with ping Linux? ›

You can use the ping command followed by a target host or IP address. For example: ping www.google.com. If successful, you'll see round-trip time statistics indicating a successful network connection.

How to check if one server can connect to another server Linux? ›

Linux commands for testing connectivity and transfer rates
  1. There are quite a few tools that can help test your connectivity on the Linux command line. ...
  2. $ ping 192.168.0.11 PING 192.168.0.11 (192.168.0.11) 56(84) bytes of data. ...
  3. The traceroute command uses a clever technique to time each hop. ...
  4. $ nc -z -v 192.168.
Apr 20, 2021

What is the netstat command in networking? ›

The network statistics ( netstat ) command is a networking tool used for troubleshooting and configuration, that can also serve as a monitoring tool for connections over the network. Both incoming and outgoing connections, routing tables, port listening, and usage statistics are common uses for this command.

How to check network connection in cmd? ›

To test the network connection: On the client, open an MS-DOS window by clicking Start > Run and then type cmd in the open text box. At the command prompt, type: ping server_hostname . If you see an IP address appear, a computer of that host name has responded.

Top Articles
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 6098

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.