- 1 1. Introduction
- 2 2. What is a Port?
- 3 3. How to Check Ports on Ubuntu
- 4 4. Checking Firewall Settings
- 5 5. Practical Example: Checking a Specific Port
- 6 6. Port Security Best Practices
- 7 7. Conclusion
- 8 FAQ: Frequently Asked Questions About Checking Ports on Ubuntu
- 8.1 Q1. What should I do if a port is not open on Ubuntu?
- 8.2 Q2. What is the difference between ss and netstat?
- 8.3 Q3. How can I detect if someone is scanning my ports?
- 8.4 Q4. How can I check which process is using a specific port?
- 8.5 Q5. How do I allow only a specific IP address using ufw?
- 8.6 Q6. How can I change the port number for a service?
- 8.7 Q7. Can I allow multiple ports at once?
1. Introduction
In network management and server administration, accurately understanding the status of ports is crucial. Especially when using Ubuntu, checking open ports and active processes helps enhance security measures and enables quick troubleshooting.
This article provides a detailed explanation of the fundamental commands and tools used to check ports on Ubuntu. Designed for beginners to intermediate users, it offers practical and easy-to-follow steps, so be sure to read until the end.
2. What is a Port?
2.1 Basic Concept of Ports
A port is a virtual communication gateway used by computers and network devices to send and receive data. Specifically, it allows multiple applications to communicate simultaneously on the same IP address by identifying and directing data to the appropriate application.
For example, a web server uses port 80 for HTTP communication. If the same server also allows SSH connections, it uses port 22. Since different services are distinguished by their port numbers, checking port status is essential for network management.
2.2 Types and Roles of Ports
Ports are categorized into three major types:
- Well-Known Ports (0–1023)
- Standardized globally and assigned to widely used services.
- Examples:
- HTTP: 80
- HTTPS: 443
- SSH: 22
- Registered Ports (1024–49151)
- Used by specific applications or organizations.
- Examples:
- MySQL: 3306
- PostgreSQL: 5432
- Dynamic Ports (49152–65535)
- Temporary ports often used for client-side communication.
Understanding these classifications makes it easier to determine how a port is used.

3. How to Check Ports on Ubuntu
Ubuntu offers various tools to check port status. Here, we will explain four practical commands.
3.1 Using the ss
Command
The ss
command is a powerful network management tool in Linux. It is fast and provides detailed connection information.
Basic Command:
sudo ss -ltn
Options Explanation:
- -l: Displays only listening ports.
- -t: Shows only TCP protocol connections.
- -n: Displays addresses and port numbers in numeric format.
Example Output:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
3.2 Using the netstat
Command
The netstat
command is a traditional network management tool. Although it is gradually being replaced by ss
, it is still available on many systems.
Basic Command:
sudo netstat -ltn
Example Output:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
3.3 Using the lsof
Command
The lsof
command is useful for identifying processes using a specific port.
Check a Specific Port:
sudo lsof -i :80
Example Output:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
apache2 1234 www 4u IPv4 12345 0t0 TCP *:http (LISTEN)
3.4 Using the nmap
Command
The nmap
tool specializes in network scanning and security diagnostics.
Scan Localhost:
sudo nmap localhost
Example Output:
Starting Nmap 7.80 ( https://nmap.org ) at 2024-12-21 18:00 JST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00013s latency).
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
Key Points:
- Displays open ports and their associated services.
- Scanning external servers requires prior authorization.
4. Checking Firewall Settings
In Ubuntu, firewalls are commonly used to enhance security. ufw
(Uncomplicated Firewall) is a widely used, simple yet powerful tool. This section explains how to check and modify port permissions using ufw
.
4.1 Checking the Firewall Status
Check Firewall Status:
sudo ufw status verbose
Example Output:
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing)
New profiles: skip
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
Explanation:
- Status: active: Firewall is enabled.
- Logging: on: Logs firewall activity.
- Default: deny (incoming), allow (outgoing): Denies incoming connections by default while allowing outgoing ones.
- ALLOW: Specifies which ports/services are permitted (e.g., SSH and HTTP).
Note: If the firewall is disabled (Status: inactive
), activate it with:
sudo ufw enable
4.2 Allowing or Blocking Ports
Allow a specific port:
sudo ufw allow 22/tcp
Explanation:
- Allows TCP connections on port 22 (SSH).
Block a specific port:
sudo ufw deny 80/tcp
Explanation:
- Blocks access to port 80 (HTTP).
Allow only a specific IP address:
sudo ufw allow from 192.168.1.100 to any port 22 proto tcp
Explanation:
- Allows SSH connections only from IP address
192.168.1.100
.
4.3 Resetting and Reviewing Firewall Settings
To reset the firewall settings and start over, use the following command:
sudo ufw reset
This command clears all rules and restores the firewall to its default state. Always review the rules after making changes.

5. Practical Example: Checking a Specific Port
Here, we will demonstrate how to check the status of SSH (port 22) as an example.
5.1 Checking Port Status
Example command:
sudo ss -ltn | grep ':22'
Example Output:
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
Key Points:
- If the output shows
LISTEN
, the port is open and waiting for connections. 0.0.0.0
means the server accepts connections from any IP address.
5.2 Identifying the Process Using the Port
Example command:
sudo lsof -i :22
Example Output:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1234 root 3u IPv4 56789 0t0 TCP *:ssh (LISTEN)
Key Points:
sshd
is the daemon process managing SSH connections.- The process ID (PID) can be used to stop or restart the service.
To stop the process:
sudo kill 1234
5.3 Troubleshooting Examples
Problem: The port is not open or cannot be accessed.
Steps to fix:
- Check firewall settings:
sudo ufw status verbose
- If the port is blocked, allow it:
sudo ufw allow 22/tcp
- Check and restart the service if necessary:
sudo systemctl restart ssh
6. Port Security Best Practices
Managing ports is directly linked to network security. Below are key points to enhance security.
6.1 Closing Unnecessary Ports
Unused ports should be closed to prevent potential attacks.
Example: Closing port 80
sudo ufw deny 80/tcp
6.2 Preventing Port Scanning
Port scanning is a technique used by attackers to find vulnerabilities in a system. The following measures can help prevent it:
- Strengthening firewall rules:
sudo ufw default deny incoming
- Monitoring logs:
sudo tail -f /var/log/ufw.log
- Using intrusion detection tools:
- Tools like
fail2ban
can automatically block suspicious access attempts.

7. Conclusion
This article provided detailed steps for checking ports on Ubuntu, managing firewall settings using ufw
, and implementing security measures.
7.1 Key Takeaways
- Understanding Ports: Ports act as communication gateways and are categorized into well-known, registered, and dynamic ports.
- Using Commands to Check Ports:
- Commands like
ss
,netstat
,lsof
, andnmap
help check port status and running processes. - Firewall Management:
ufw
can be used to allow or block specific ports, enhancing security.- Security Measures:
- Closing unnecessary ports, monitoring logs, and using security tools help maintain a safe network environment.
7.2 Future Applications
Port management is a fundamental aspect of network security. Apply the knowledge from this guide to maintain a secure server environment.
FAQ: Frequently Asked Questions About Checking Ports on Ubuntu
Q1. What should I do if a port is not open on Ubuntu?
A:
If a port is not open, follow these steps:
- Check firewall settings:
sudo ufw status verbose
If the firewall is blocking the port, allow it with the following command:
sudo ufw allow [port number]/tcp
- Verify that the corresponding service is running:
sudo systemctl status [service name]
Example: For SSH, use:
sudo systemctl status ssh
If the service is not running, restart it:
sudo systemctl restart [service name]
- Check if the service is using the correct port:
Verify the configuration file (e.g., for SSH, check /etc/ssh/sshd_config
) to ensure the correct port is set.
Q2. What is the difference between ss
and netstat
?
A:ss
and netstat
are both tools for checking network connections, but they have key differences:
ss
:- Recommended for modern Linux systems.
- Faster and provides more detailed information.
- Example command:
sudo ss -ltn
netstat
:- An older tool that is gradually being deprecated.
- Still available on many systems for compatibility.
- Example command:
sudo netstat -ltn
For newer systems, ss
is the preferred choice.
Q3. How can I detect if someone is scanning my ports?
A:
To detect port scanning, try the following methods:
- Check firewall logs:
sudo tail -f /var/log/ufw.log
Look for unusual IP addresses or repeated connection attempts.
- Use an intrusion detection system (IDS):
- Install and configure tools like
fail2ban
orSnort
to automatically block malicious access.
- Scan your own server using
nmap
:
sudo nmap localhost
Check for open ports and close any that are unnecessary.
Q4. How can I check which process is using a specific port?
A:
Use the lsof
command to identify processes using a specific port:
sudo lsof -i :[port number]
Example: To check port 80:
sudo lsof -i :80
Example Output:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
apache2 1234 www 4u IPv4 12345 0t0 TCP *:http (LISTEN)
Q5. How do I allow only a specific IP address using ufw
?
A:
To allow access from only a specific IP address, use the following command:
sudo ufw allow from [IP address] to any port [port number] proto tcp
Example: Allow SSH access (port 22) only from IP address 192.168.1.100
:
sudo ufw allow from 192.168.1.100 to any port 22 proto tcp
Q6. How can I change the port number for a service?
A:
To change a service’s port number, edit its configuration file. Here’s an example for SSH:
- Open the configuration file:
sudo nano /etc/ssh/sshd_config
- Find the
Port
setting and change the port number:
Port 2222
- Restart the SSH service:
sudo systemctl restart ssh
- Allow the new port in the firewall:
sudo ufw allow 2222/tcp
Q7. Can I allow multiple ports at once?
A:
Yes, you can allow multiple ports at once using the following methods:
- Allow a range of ports:
sudo ufw allow 1000:2000/tcp
Explanation: Allows TCP connections on ports 1000 to 2000.
- Allow multiple specific ports:
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp