Managing port security
Ports of Call
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
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.