This brief tutorial shows students and new users how to find all ports in use or listening ports when using Ubuntu Linux systems.
The steps below should be handy if you’re a server administrator or web admin and want to ensure that only approved ports are opened on your Ubuntu Linux server.
Most servers built for public access will have services that listen to their assigned ports for communication. In some cases, ports that are not in use will stay open, which may lead to them being exploited.
Ubuntu comes with some default commands that can be used to scan your servers for open ports. The steps below will show you how to use some of these commands to identify listening ports and how to find them.
Since you can’t have two services listening on the same port, there’s a good chance that you may have ports you’re probably not using, and you’ll want to close them.
A network port is identified by its number, the associated IP address, and the type of communication protocol, such as TCP or UDP.
To identify listening ports on Ubuntu, follow the steps below:
Use the netstat Command
netstat is a command-line tool that can provide information about network connections, including IP addresses, ports, and services communicating on these ports.
If you don’t already have the Netstat tool installed, use the commands below to install it.
sudo apt install net-tools
If you want to list all ports available on a server, you run the commands below:
sudo netstat -tunlp
For detailed command options, view the bullet below:
- -t Show TCP ports.
- -u Show UDP ports.
- -n Show numerical addresses instead of resolving hosts.
- -l Show only listening ports.
- -p Show the PID and name of the listener’s process.
When you run the Command above with the options, you should see similar lines as below:
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 486/systemd-resolve tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 2851/cupsd
That should give you a lot of information.
However, if you only want to see a specific service name or port, you can use the netstat Command with the option above with grep.
The example below shows you to scan for open ports and only list port 22.
sudo netstat -tnlp | grep :22
You should see a similar line below:
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 25538/sshd tcp6 0 0 :::22 :::* LISTEN 25538/sshd
The Command above using grep shows port 22 only, and sshd service is listening on that port.
Use the ss Command
netstat is not installed on Ubuntu by default. The ss command is installed as a replacement for netstat. As with netstat, the ss command displays network information on Linux systems.
netstat and ss commands share almost the same command options. So, if you’re used to netstat, the ss command should work almost the same.
To view all listening ports on Ubuntu using the ss command, run the Command below:
sudo ss -tunlp
You should see a similar screen as shown below:
NetidState Recv-Q Send-Q Local Address:Port Peer Address:Port udp UNCONN 0 0 127.0.0.53%lo:53 0.0.0.0:* users:(("systemd-resolve",pid=486,fd=12)) udp UNCONN 0 0 0.0.0.0:68 0.0.0.0:* users:(("dhclient",pid=782,fd=6)) tcp LISTEN 0 5 [::1]:631 [::]:* users:(("cupsd",pid=2851,fd=6))
The output above is similar to the netstat Command we ran previously.
Use the lsof Command
The lsof Command is another powerful utility available to Linux systems that allows you to display networking information.
To list all listening TCP ports using the lsof Command, run it with the options below:
sudo lsof -nP -iTCP -sTCP:LISTEN
You should see similar lines as shown below:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME systemd-r 486 systemd-resolve 13u IPv4 15733 0t0 TCP 127.0.0.53:53 (LISTEN) cupsd 2851 root 6u IPv6 36958 0t0 TCP [::1]:631 (LISTEN) cupsd 2851 root 7u IPv4 36959 0t0 TCP 127.0.0.1:631 (LISTEN) sshd 25538 root 3u IPv4 77978 0t0 TCP *:22 (LISTEN) sshd 25538 root 4u IPv6 77980 0t0 TCP *:22 (LISTEN)
That should list open ports as well.
Now you know how to list listing ports on Ubuntu; you can use any of the commands above to find ports that are not in use and turn off services to them.
Leave a Reply to SkyKapp Cancel reply