how to check if a port is open in linux
To check whether a port in Linux is open, 1. Use nc -zv [host] [port] to quickly test connectivity; 2. Use nmap -p [port] to obtain detailed status; 3. Use ss -tuln or netstat -tuln to view the local listening port. These methods are suitable for simple testing, comprehensive scanning and local service inspection, respectively.

Checking whether a port in the Linux system is open is a common operation when troubleshooting network problems, debugging services, or configuring firewalls. The most direct way is to use some command line tools to detect the local or remote port status.

1. Quickly test port connectivity using nc (Netcat)
nc is a very light and powerful network debugging tool that can be used to quickly determine whether a port is open.
Usage example:

nc -zv [host] [port]
For example, if you want to check whether the 80 port of this machine is open:
nc -zv localhost 80
If you see a message similar to succeeded! it means that the port is open; otherwise it may be filtered or closed.

Note: If
ncis not installed, you can install it through a package manager, such as runningsudo apt install netcaton Ubuntu.
2. Use nmap for detailed scanning
If you need more detailed port information, or want to batch scan multiple ports, nmap is a good choice.
Basic commands:
nmap -p [port] [host]
For example, check the 22 port of the remote host 192.168.1.100:
nmap -p 22 192.168.1.100
The output result will clearly show that the port is open, closed, or filtered.
- If you just want to scan the machine, change the IP to
127.0.0.1 - Multiple ports can be specified at the same time, such as
-p 22,80,443
Tip: Some systems do not install nmap by default, so they can be installed using
sudo apt install nmapor the corresponding system package management command.
3. Use ss or netstat to view the local listening port
If you want to confirm which ports are listening on the machine, instead of connecting to the external host, you can use the following commands:
Use the ss command:
ss -tuln | grep [port]
For example, check whether there is any listening 3306:
ss -tuln | grep 3306
Or use netstat (if installed):
netstat -tuln | grep 3306
These two commands will list all TCP/UDP listening ports, and can quickly locate whether a specific port is running with grep .
Basically these are the methods. Different tools can be selected in different scenarios: use nc for simple test, use nmap for comprehensive scan, and use ss or netstat for local service status. These commands are combined enough to meet most port detection requirements.
The above is the detailed content of how to check if a port is open in linux. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
How to install software on Linux using the terminal?
Aug 02, 2025 pm 12:58 PM
There are three main ways to install software on Linux: 1. Use a package manager, such as apt, dnf or pacman, and then execute the install command after updating the source, such as sudoaptininstallcurl; 2. For .deb or .rpm files, use dpkg or rpm commands to install, and repair dependencies when needed; 3. Use snap or flatpak to install applications across platforms, such as sudosnapinstall software name, which is suitable for users who are pursuing version updates. It is recommended to use the system's own package manager for better compatibility and performance.
The Ultimate Guide to High-Performance Gaming on Linux
Aug 03, 2025 am 05:51 AM
ChoosePop!_OS,Ubuntu,NobaraLinux,orArchLinuxforoptimalgamingperformancewithminimaloverhead.2.InstallofficialNVIDIAproprietarydriversforNVIDIAGPUs,ensureup-to-dateMesaandkernelversionsforAMDandIntelGPUs.3.EnabletheperformanceCPUgovernor,usealow-latenc
What are the main pros and cons of Linux vs. Windows?
Aug 03, 2025 am 02:56 AM
Linux is suitable for old hardware, has high security and is customizable, but has weak software compatibility; Windows software is rich and easy to use, but has high resource utilization. 1. In terms of performance, Linux is lightweight and efficient, suitable for old devices; Windows has high hardware requirements. 2. In terms of software, Windows has wider compatibility, especially professional tools and games; Linux needs to use tools to run some software. 3. In terms of security, Linux permission management is stricter and updates are convenient; although Windows is protected, it is still vulnerable to attacks. 4. In terms of difficulty of use, the Linux learning curve is steep; Windows operation is intuitive. Choose according to requirements: choose Linux with performance and security, and choose Windows with compatibility and ease of use.
Understanding RAID Configurations on a Linux Server
Aug 05, 2025 am 11:50 AM
RAIDimprovesstorageperformanceandreliabilityonLinuxserversthroughvariousconfigurations;RAID0offersspeedbutnoredundancy;RAID1providesmirroringforcriticaldatawith50�pacityloss;RAID5supportssingle-drivefailuretoleranceusingparityandrequiresatleastthre
Linux how to enable and disable services at boot
Aug 08, 2025 am 10:23 AM
To manage the startup of Linux services, use the systemctl command. 1. Check the service status: systemctlstatus can check whether the service is running, enabled or disabled. 2. Enable the service startup: sudosystemctlenable, such as sudosystemctlenablenginx. If it is started at the same time, use sudosystemctlenable--nownginx. 3. Disable the service startup: sudosystemctldisable, such as sudosystemctldisablecups. If it is stopped at the same time, use sudosystemctldisabl
Linux how to list all running processes
Aug 08, 2025 am 06:42 AM
Usepsauxforacompletesnapshotofallrunningprocesses,showingdetailedinformationlikeUSER,PID,CPU,andmemoryusage.2.Usetoporhtopforreal-timemonitoringofprocesseswithdynamicupdates,wherehtopoffersamoreintuitiveinterface.3.UsepgreporpidoftoquicklyfindthePIDs
How to clean up your Linux system
Aug 22, 2025 am 07:42 AM
Removeunusedpackagesanddependencieswithsudoaptautoremove,cleanpackagecacheusingsudoaptcleanorautoclean,andremoveoldkernelsviasudoaptautoremove--purge.2.Clearsystemlogswithsudojournalctl--vacuum-time=7d,deletearchivedlogsin/var/log,andempty/tmpand/var
Linux how to view the contents of a file
Aug 19, 2025 pm 06:44 PM
ToviewfilecontentsinLinux,usedifferentcommandsbasedonyourneeds:1.Forsmallfiles,usecattodisplaytheentirecontentatonce,withcat-ntoshowlinenumbers.2.Forlargefiles,uselesstoscrollpagebypageorlinebyline,searchwith/search_term,andquitwithq.3.Usemoreforbasi


