MENU
Language

To check port number availability

To check the availability of port numbers, list the ports that are currently in use and see if the port you want is in use. You can check the status of the port in the following ways:


1. lsof Using Commands

Check specific ports

sudo lsof -i :<ポート番号>

Example: To check port 80 usage

sudo lsof -i :80

Example output:

COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx    1234   root   6u   IPv4  12345      0t0  TCP *:http (LISTEN)

COMMAND: The name of the program that is using the port.

PID: Process ID.

NAME: The port in use (e.g., ). *:http

If port 80 is not used, nothing is shown.


2. netstat Using Commands

View all listening ports

sudo netstat -tuln

Example output:

Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN

Local Address: The IP address and port number used (e.g., 0.0.0.0:80).

State: LISTEN Indicates that the port is in a listening state (waiting).

Checking Specific Ports

sudo netstat -tuln | grep :<ポート番号>

Example: Check port 443

sudo netstat -tuln | grep :443

3. ss Using Commands

ss netstat provides faster and more detailed information than .

View all listening ports

sudo ss -tuln
  • Example output:
Netid  State    Recv-Q Send-Q Local Address:Port  Peer Address:Port
tcp    LISTEN   0      128    0.0.0.0:80         0.0.0.0:*
tcp    LISTEN   0      128    127.0.0.1:3306     0.0.0.0:*

Checking Specific Ports

sudo ss -tuln | grep :<ポート番号>

Example: Checking Port 22 (SSH)

sudo ss -tuln | grep :22

4. If the port you want to check is not in use

If a particular port is not listed, it is currently free and available.


5. How to stop a process that is opening up ports

Identify the Process ID (PID)

lsof netstat or get the PID from the result.

Stop the process

If it is operating as a service:

sudo systemctl stop <サービス名>

If you want to terminate the process directly:

sudo kill <PID>

reconfirmation

After the process ends, check again lsof with the or ss command.


6. Check the impact of the firewall

Even if the port is free, the firewall may prevent you from connecting from the outside.

Checking the configuration of UFW (Uncomplicated Firewall)

sudo ufw status
  • Check if the port is allowed.
  • Allow ports as needed:
sudo ufw allow <ポート番号>

Summary

It is important to check your firewall settings and also investigate if there are any connection restrictions.

lsofYou can check the port usage status by using the , netstat, ss command.

If the port is free, nothing is displayed.

To open a port in use, stop the process.

Let's share this post !

Author of this article

AIアーティスト | エンジニア | ライター | 最新のAI技術やトレンド、注目のモデル解説、そして実践に役立つ豊富なリソースまで、幅広い内容を記事にしています。フォローしてねヾ(^^)ノ

Comments

To comment

目次