Start stop and check the status of network services
Learn to manage network services on Linux by starting, stopping, and checking their status using commands like ss and systemctl.
In this article, you will learn to manage network services on Linux by starting, stopping, and checking their status. Most servers run multiple services handling network connections. A common example is the SSH daemon (sshd), which continuously runs in the background and listens for remote login connections.
Begin by inspecting the active programs and their waiting connections. Two useful utilities for this purpose are ss and netstat. While ss is the modern choice, netstat has been widely used historically and might be phased out on some distributions.
The ss command is effective for viewing programs that are ready to accept incoming connections. Use the following flags with ss:
-l: List listening sockets.
-t: Display TCP connections.
-u: Display UDP connections.
-n: Show numeric values instead of resolving service names.
-p: Display the process using each socket.
Using the -p flag requires root privileges to reveal processes that are owned by root. Prepend your command with sudo.
A helpful mnemonic for these options is “l-t-u-n-p” (listening, TCP, UDP, numeric, process) or simply remember “tunnel P.” Execute the command below to see what is listening on your network ports:
In the output above, the “Local Address:Port” column indicates which services are listening and their respective ports. For instance, the SSH daemon listens on port 22, while chronyd is bound to port 323. An address like “127.0.0.1” (localhost) means the service accepts only local connections, whereas “0.0.0.0” signifies that the service accepts external connections.Once you have the process details and PID, you can further inspect the process using the ps command or examine open files with lsof.
The systemctl command allows you to check a service’s status, stop it, or manage its startup behavior. To check the status of a service (for example, chronyd or sshd), use:
Copy
Ask AI
$ sudo systemctl status chronyd.service$ sudo systemctl status sshd.service
If you need to stop a service, such as chronyd, run:
Copy
Ask AI
$ sudo systemctl stop chronyd.service
After stopping the service, you can verify that it is no longer listening on its designated port (e.g., port 323) by checking again with ss:
Copy
Ask AI
$ sudo ss -ltunpNetid State Recv-Q Send-Q Local Address:Port Peer Address:Port Processtcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1031,fd=5))tcp LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1031,fd=7))
You can also disable a service from starting at boot using systemctl disable and later re-enable it with systemctl enable and systemctl start.
Netstat provides similar functionality with a slightly different output format. Note that netstat might not be installed by default on all systems. Use the command below for similar information:
With these techniques, you can confidently manage the network services running on your Linux system. Use ss for a modern approach, netstat for a familiar format, and systemctl for service management. Start practicing these commands to ensure your network services are configured correctly and securely.For additional information on Linux service management, check out the following resources: