If you’re a server administrator or webmaster and want to make sure only approved ports are opened on your Ubuntu Linux server, the steps below should come in handy. Most servers built for public access will have services on them which 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, it’s a good chance that you may have ports that 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 netstat tool installed, use the commands below to install it. If you want to list all ports available on a server, you run the commands below: 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: 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. You should see a similar line below: 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 is used to display 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: You should see a similar screen as shown below: 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: You should see similar lines as shown below: 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 disable services to them.