Managing port security
Ports of Call

© Photo by Jamie O'Sullivan on Unsplash
A few basic commands for working with ports can help you make your small network or standalone system more secure.
Ports are a core feature of modern computing. I'm not talking about transferring versions of applications to another architecture or operating system. Instead, I mean the kind of port that is an address for a virtual connection point to or from a computer and another device or server, including the Internet. Ports direct external traffic to the correct application, and this function makes them important for troubleshooting and security. How do you find which ports are open or listening (i.e., currently in use) when there is no need for them to be? What ports are associated with which application or server? How do you know whether any ports are hidden and being used by an intruder? Even if you are working on a standalone computer, knowing how to answer these questions is a basic administrative skill.
Ports can be either hardware or software. Either way, they are treated similarly. Most ports are managed by two protocols: Transmission Control Protocol (TCP) and User Datagram Protocol (UDP). Both TCP and UDP have a range of port numbers, divided into three categories:
- System ports (numbers 0-1023): These are the most common ports and essential to external communication. Sometimes called well-known ports, system ports can only be used by root or privileged users. For example,
ssh
is port 22, and the Network Time Protocol (NTP) is port 123. While these ports can be changed, the changed ports can only communicate with other machines that have also reassigned their ports accordingly. - Registered ports (numbers 1024-49151): These ports are available for processes and applications run by ordinary users. Registered ports are sometimes called user ports.
- Dynamic ports (numbers 49152-65535): These ports can be used by any processes or applications as needed, and they are assigned on the fly. Dynamic ports are also called private or ephemeral ports.
For the sake of thoroughness, a port may have both a TCP and a UDP address assigned by the Internet Assigned Numbers Authority (IANA). As you might expect, each open port is potentially a vulnerability that in theory can be maliciously attacked. That possibility is why security-conscious distributions install a minimal amount of software. It is also why checking for open ports is a basic security precaution. If an application or service is not necessary or currently in use, closing that port reduces the security risk.
Getting a List of Ports
Ports are usually assigned when a package is installed. The quickest way to get a complete list of ports is to log in as root and enter:
less /etc/services
You will need the less
command because there are hundreds of ports on the typical computer. In the output from less
, the left-hand column shows the name of the service, and the second column from the left shows the port number and whether it uses the TCP or UDP protocol (Figure 1). Services may be abbreviated, so the third column may give the full name. The fourth column gives any explanatory notes. If you look at the output for another Linux computer, you will find most – perhaps all – the system ports are the same. As the name implies, many of the registered ports will also be the same on different computers.
Checking for Open Ports
Security is always balanced against user convenience. However, each distribution sets that balance differently. For this reason, immediately after installation, users may want to check which ports are open and consider which ones they might want to close. Users should also check the open ports periodically and investigate any ports that they do not recognize or are uncertain about. Several tools can be used, including netstat
as follows:
netstat -lntu | less
The -l
option displays only listening or open sockets. With the -n
option, the port number is shown, while -t
includes TCP ports and -u
includes UDP ports (Figure 2).
An alternative is to query the sockets using:
ss -lntu | less
The options are the same as for netstat
. The relevant information in the output is the State
column second to the left, and the Local Address:Port
in the fifth column from the left (Figure 3).
Perhaps the best choice is to pipe lsof
through grep
:
lsof -i -P -n | grep
In this command, -i lists matching files with the current Internet address. For network files, option -P
suppresses the conversion of port numbers to port names, and -n
suppresses the conversion of network numbers to hostnames. The result is a concise presentation of relevant information. Depending on your needs, you might want to omit some of these options. When managing a network, you can also use -i
to specify an Internet address (Figure 4).
Finding a Port Number
Computer ports are too numerous for anyone to remember them all. At best, an administrator might memorize a few system and registered ports. Yet troubleshooting and security often require you to know a specific port. I know of at least one online crib sheet [1], but it may be quicker to look up ports on your own system. You can use grep
to search /etc/services
. You can even search for a specific service (Figure 5) using:
grep SERVICE /etc/service
Several related results may display, one for each protocol in use.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
News
-
The GNU Project Celebrates Its 40th Birthday
September 27 marks the 40th anniversary of the GNU Project, and it was celebrated with a hacker meeting in Biel/Bienne, Switzerland.
-
Linux Kernel Reducing Long-Term Support
LTS support for the Linux kernel is about to undergo some serious changes that will have a considerable impact on the future.
-
Fedora 39 Beta Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.
-
UbuntuDDE 23.04 Now Available
A new version of the UbuntuDDE remix has finally arrived with all the updates from the Deepin desktop and everything that comes with the Ubuntu 23.04 base.