Skip to content

Security Operations / Host hardening · Step 4 of 10

Lesson: Listening ServicesSwitch
← Security Operations

Security investigations

Listening Services

See which programs accept connections, on which addresses, and close what is not needed.

A listening service waits for others to connect. Every one is a possible way in, so you should be able to name each and say why it is there.

List listeners

$ sudo ss -tulpn
Netid State  Local Address:Port  Process
tcp   LISTEN      0.0.0.0:22     users:(("sshd",pid=801,fd=3))
tcp   LISTEN    127.0.0.1:5432   users:(("postgres",pid=912,fd=6))

Some columns are trimmed. The options are -t TCP, -u UDP, -l listening, -p process and -n numbers instead of names. Without root you may not see other users' processes.

Read the address

The local address tells you who could reach it.

  • 127.0.0.1 or [::1]: only programs on this machine.
  • 0.0.0.0: all IPv4 addresses of the host.
  • [::]: all IPv6 addresses, and on many Linux systems IPv4 too.
  • A specific address such as 10.0.0.5: connections to that address.

Listening on all addresses does not prove the internet can reach it. A firewall or cloud rule may block it. The reverse also matters: check IPv6 as well as IPv4.

Find the owner and fix

$ ps -o pid,user,cmd -p 912
$ systemctl status 912

Given a process ID, systemctl status shows the unit it belongs to. Then decide:

  • Not needed: sudo systemctl disable --now name.service.
  • Needed only locally: set the program's listen address to localhost and restart. The setting name differs for each program, so read its documentation.
  • Needed remotely: allow only the sources that need it with a firewall.

Some ports are held by systemd itself through a socket unit. List them with systemctl list-sockets and disable the .socket unit too, or the service may start again on the next connection.

Do not just kill the process. It may be restarted, and the cause stays. After changes, run ss again and confirm that legitimate users can still connect.

Try it

Run ss -tuln on your own machine and mark each line as local only or reachable from elsewhere. Try the networking lab to get used to these tools.

Check your understanding

Listening Services

All 3 sections · no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.