Managing port security

Ports of Call

© Photo by Jamie O'Sullivan on Unsplash

© Photo by Jamie O'Sullivan on Unsplash

Article from Issue 258/2022
Author(s):

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.

Figure 1: The start of a list of ports using less.

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).

Figure 2: Open ports displayed using netstat.

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).

Figure 3: Querying sockets to find open ports.

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).

Figure 4: Using lsof to find open ports.

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
Figure 5: Finding port numbers by grepping /etc/service.

Several related results may display, one for each protocol in use.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • System Alert

    The unhide forensics tool scans your system for inconsistencies to uncover hidden processes.

  • Nmap Methods

    How does the popular Nmap scanner identify holes in network security? In this article, we examine some Nmap analysis techniques.

  • Single-Packet Port Knocking

    If you are looking for an extra layer of remote access security, try single-packet port knocking.

  • Nmap Scripting

    Nmap is rolling out a new scripting engine to automatically investigate vulnerabilities that turn up in a security scan. We’ll show you how to protect your network with Nmap and NSE.

  • Customizing PortSentry

    PortSentry monitors your ports and lets you know when they’ve been scanned.

comments powered by Disqus
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.

Learn More

News