Which command should you use to scan for open tcp ports on your linux system?

A Port is a logical number assigned to a process running on Linux. Port can be defined as an integer number between 0 to 65535. Ports with numbers 0–1023 are called system or well-known ports, while ports with numbers 49152-65535 are called dynamic, private, or ephemeral ports. If you are a Linux system administrator then you should have the knowledge to find out which ports are open and running on a remote system. There are several ways to find an open port on Linux.

In this post, we will show the most reliable ways to find an open port on a remote Linux system.

Prerequisites

  • A server running Linux on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing any Linux operating system with at least 1GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Find Open Port Using Netcat Command

Netcat is a Linux command-line utility used to read and write data across network connections using TCP or UDP protocol. It also allows us to find open ports on a remote Linux system.

By default, the Netcat utility is not included in the Linux system. You will need to install it manually using the package manager.

For Ubuntu and Debian-based operating systems, install the Netcat utility using the following command:

apt-get install netcat -y

For RHEL, CentOS, Fedora, and Rocky Linux operating systems, install the Netcat utility using the following command:

dnf install nc -y

The basic syntax to use the Netcat command is shown below:

nc [-options] [host_name or ip] [port_number]

For example, to check if port 80 is open on the remote host 172.20.10.2, run the following command:

nc -zv 172.20.10.2 80

If port 80 is open on a remote host, you will get the following output:

Connection to 172.20.10.2 80 port [tcp/http] succeeded!

Where:

  • -z : Sets nc to simply scan for listening daemons.
  • -v : Show output in verbose mode.

You can also specify a range of ports to be scanned:

nc -zv 172.20.10.2 40-80

The above command will scan for all ports between 40 and 80.

Also Read

Netstat Command Line Tips and Tricks

Find Open Port Using Nmap Command

Nmap is a powerful and very popular command-line utility used to perform network-related tasks. By default, the Nmap utility is not included in the Linux system. You will need to install it manually using the package manager.

For Ubuntu and Debian-based operating systems, install the Nmap utility using the following command:

apt-get install namp -y

For RHEL, CentOS, Fedora, and Rocky Linux operating systems, install the Nmap utility using the following command:

dnf install nmap -y

The basic syntax to use the Nmap command is shown below:

nmap [-options] [HostName or IP] [-p] [PortNumber]

For example, to get a list of all open ports on a remote host 172.20.10.2, run the following command:

nmap 172.20.10.2

You should see all open ports in the following output:

dnf install nc -y
0

If you want to check if a particular port is open on the remote host, use the -p option:

dnf install nc -y
1

You will get the following output:

dnf install nc -y
2

Find Open Port Using Telnet Command

Telnet is another network protocol used to virtually access a remote computer. It also allows you to find open ports on remote systems. You can install it manually using the package manager.

For Ubuntu and Debian-based operating systems, install the Telnet utility using the following command:

dnf install nc -y
3

For RHEL, CentOS, Fedora, and Rocky Linux operating systems, install the Telnet utility using the following command:

dnf install nc -y
4

The basic syntax to use the Telnet command is shown below:

dnf install nc -y
5

For example, to check if port 22 is open on a remote host 172.20.10.2, run the following command:

dnf install nc -y
6

If port 22 is open, you will get the following output:

dnf install nc -y
7

If port 22 is not open, you will get the following output:

dnf install nc -y
8

Conclusion

In this guide, we have shown how to find an open port on a remote machine using different commands. You can now choose your preferred tool to scan the remote system and find an open port. Try it on VPS hosting from Atlantic.Net!

One of Nmap's primary functions is conducting port scans. In this walkthrough, learn how to launch a default scan, along with other options that affect Nmap port scan behavior.

  • Share this item with your network:

By

  • Sharon Shea, Executive Editor
  • Packt Publishing

The Nmap network reconnaissance and security auditing tool, released in 1997, is one of the most basic and most used cybersecurity tools today. From its beginnings as an advanced port scanner, it evolved into a multifunctional tool with a family of useful projects that can discover weak passwords, scan IPv6 addresses, perform IP address geolocation, detect vulnerabilities and more.

The open source tool helps security pros, networking teams, sys admins and other IT personnel scan hosts, networks, applications, mainframes, Unix and Windows environments, supervisory control and data acquisition systems, and industrial control systems.

Paulino Calderon, co-founder of Websec and part-time Nmap developer, wrote Nmap Network Exploration and Security Auditing Cookbook, Third Edition, published by Packt, to offer firsthand insights into using the multifaceted tool.

In this excerpt from Chapter 1, "Nmap Fundamentals," Calderon shares a recipe on how to use Nmap to find open ports. Follow along to learn how to perform the quintessential Nmap task, and review Calderon's tips on port scanning techniques, options that affect the scan behavior of Nmap and more. Download a PDF of Chapter 1 to read more.

Listing open ports on a target

This recipe describes how to use Nmap to determine the port states of a target, a process used to identify running services commonly referred to as port scanning. This is one of the tasks Nmap excels at, so it is important to learn about the essential Nmap options related to port scanning.

How to do it...

To launch a default scan, the bare minimum you need is a target. A target can be an IP address, a hostname, or a network range:

$ nmap scanme.nmap.org

The scan results will show all the host information obtained, such as the IPv4 (and IPv6 if available) address, reverse DNS name, and interesting ports with service names. All listed ports have a state. Ports marked as open or filtered are of special interest as they represent services running on the target host:

Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up (0.16s latency).
Other addresses for scanme.nmap.org (not scanned):
2600:3c01::f03c:91ff:fe18:bb2f
Not shown: 995 closed ports PORT STATE SERVICE
22/tcp open ssh 25/tcp filtered smtp 80/tcp open http
9929/tcp open nping-echo 31337/tcp open Elite
Nmap done: 1 IP address (1 host up) scanned in 333.35 seconds

How it works...

The default Nmap scan returns a list of ports. In addition, it returns a service name from a database distributed with Nmap and the port state for each of the listed ports.

Which command should you use to scan for open tcp ports on your linux system?
Learn more about Calderon's
Nmap cookbook, published
by Packt.

Nmap categorizes ports into the following states:

  • Open: Open indicates that a service is listening for connections on this port.
  • Closed: Closed indicates that the probes were received, but it was concluded that there was no service running on this port.
  • Filtered: Filtered indicates that there were no signs that the probes were received and the state could not be established. This could indicate that the probes are being dropped by some kind of filtering.
  • Unfiltered: Unfiltered indicates that the probes were received but a state could not be established.
  • Open/Filtered: This indicates that the port was filtered or open, but the state could not be established.
  • Closed/Filtered: This indicates that the port was filtered or closed but the state could not be established.

Even for this simple port scan, Nmap does many things in the background that can be configured as well. Nmap begins by converting the hostname to an IPv4 address using DNS name resolution. If you wish to use a different DNS server, use --dns-servers <serv1[,serv2],...>, or use -n if you wish to skip this step, as follows:

$ nmap --dns-servers 8.8.8.8,8.8.4.4 scanme.nmap.org

Afterward, it performs the host discovery process to check whether the target is online (see the Finding online hosts recipe). To skip this step, use the no ping option, -Pn:

$ nmap -Pn scanme.nmap.org

Nmap then converts the IPv4 or IPv6 address back to a hostname using a reverse DNS query. Use -n to skip this step as well if you do not need that information:

$ nmap -n scanme.nmap.org

The previous command will launch either a SYN stealth scan or a TCP connect scan depending on the privileges of the user running Nmap.

There's more...

Port scanning is one of the most powerful features available, and it is important that we understand the different techniques and options that affect the scan behavior of Nmap.

Privileged versus unprivileged

Running the simplest port scan command, nmap <target>, as a privileged user by default launches a SYN stealth scan, whereas unprivileged users that cannot create raw packets use the TCP connect scan technique. The difference between these two techniques is that a TCP connect scan uses the high-level connect() system call to obtain the port state information, meaning that each TCP connection is fully completed and therefore slower. SYN stealth scans use raw packets to send specially crafted TCP packets to detect port states with a technique known as half-open.

Scanning specific port ranges

Setting port ranges correctly during your scans is a task you often need to do when running Nmap scans. You can also use this to filter machines that run a service on a specific port, for example, finding all the SMB servers open in port 445. Narrowing down the port list also optimizes performance, which is very important when scanning multiple targets.

There are several ways of using the Nmap -p option:

  • Port list separated by commas: $ nmap -p80,443 localhost
  • Port range denoted with hyphens: $ nmap -p1-100 localhost
  • Alias for all ports from 1 to 65535: # nmap -p- localhost
  • Specific ports by protocol: # nmap -pT:25,U:53 <target>
  • Service name: # nmap -p smtp <target>
  • Service name with wildcards: # nmap -p smtp* <target>
  • Only ports registered in the Nmap services database: # nmap -p[1-65535] <target>

Selecting a network interface

Nmap attempts to automatically detect your active network interface; however, there are some situations where it will fail or perhaps you will need to select a different interface in order to test networking issues. To force Nmap to scan using a different network interface, use the -e argument:

#nmap -e <interface> <target>
#nmap -e eth2 scanme.nmap.org

This is only necessary if you have problems with broadcast scripts or see the WARNING: Unable to find appropriate interface for system route to message.

More port scanning techniques

In this recipe, we talked about the two default scanning methods used in Nmap: SYN stealth scan and TCP connect scan. However, Nmap supports several more advanced port scanning techniques. Use nmap -h or visit https://nmap.org/book/man-portscanning-techniques.html to learn more about them as Fyodor has done a fantastic job describing how they work in depth.

Target specification

Nmap supports several target formats that allow users to work with IP address ranges. The most common type is when we specify the target's IP or host, but it also supports the reading of targets from files and ranges, and we can even generate a list of random targets as we will see later.

Any arguments that are not valid options are read as targets by Nmap. This means that we can tell Nmap to scan more than one range in a single command, as shown in the following command:

# nmap -p25,80 -O -T4 192.168.1.1/24 scanme.nmap.org/24

There are several ways that we can handle IP ranges in Nmap:

  • Multiple host specification
  • Octet range addressing (they also support wildcards)
  • CIDR notation

To scan the 192.168.1.1, 192.168.1.2, and 192.168.1.3 IP addresses, the following command can be used:

$ nmap 192.168.1.1 192.168.1.2 192.168.1.3

We can also specify octet ranges using -. For example, to scan hosts 192.168.1.1, 192.168.1.2, and 192.168.1.3, we could use the expression 192.168.1.1-3, as shown in the following command:

$ nmap 192.168.1.1-3

Octet range notation also supports wildcards, so we could scan from 192.168.1.0 to 192.168.1.255 with the expression 192.168.1.*:

$ nmap 192.168.1.*

Excluding hosts from scans

In addition, you may exclude hosts from the ranges by specifying the --exclude option, as shown next:

Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up (0.16s latency).
Other addresses for scanme.nmap.org (not scanned):
2600:3c01::f03c:91ff:fe18:bb2f
Not shown: 995 closed ports PORT STATE SERVICE
22/tcp open ssh 25/tcp filtered smtp 80/tcp open http
9929/tcp open nping-echo 31337/tcp open Elite
Nmap done: 1 IP address (1 host up) scanned in 333.35 seconds
0

Otherwise, you can write your exclusion list in a file using the --exclude-file option:

Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up (0.16s latency).
Other addresses for scanme.nmap.org (not scanned):
2600:3c01::f03c:91ff:fe18:bb2f
Not shown: 995 closed ports PORT STATE SERVICE
22/tcp open ssh 25/tcp filtered smtp 80/tcp open http
9929/tcp open nping-echo 31337/tcp open Elite
Nmap done: 1 IP address (1 host up) scanned in 333.35 seconds
1

CIDR notation for targets

The CIDR notation (pronounced cider) is a compact method for specifying IP addresses and their routing suffixes. This notation gained popularity due to its granularity when compared with classful addressing because it allows subnet masks of variable length.

The CIDR notation is specified by an IP address and network suffix. The network or IP suffix represents the number of network bits. IPv4 addresses are 32-bit, so the network can be between 0 and 32. The most common suffixes are /8, /16, /24, and /32.

To visualize it, take a look at the following CIDR-to-netmask conversions:

  • /8: 255.0.0.0
  • /16: 255.255.0.0
  • /24: 255.255.255.0
  • /32: 255.255.255.255

For example, 192.168.1.0/24 represents the 256 IP addresses from 192.168.1.0 to 192.168.1.25550.116.1.121/8 represents all the IP addresses between 50.0-255.0-255.0-255. The /32 network suffix is also valid and represents a single IP address.

The CIDR notation can also be used when specifying targets. To scan the 256 hosts in 192.168.1.0-255 using the CIDR notation, you will need the /24 suffix:

Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up (0.16s latency).
Other addresses for scanme.nmap.org (not scanned):
2600:3c01::f03c:91ff:fe18:bb2f
Not shown: 995 closed ports PORT STATE SERVICE
22/tcp open ssh 25/tcp filtered smtp 80/tcp open http
9929/tcp open nping-echo 31337/tcp open Elite
Nmap done: 1 IP address (1 host up) scanned in 333.35 seconds
2

Working with target lists

Many times, we will need to work with multiple targets, but having to type a list of targets in the command line is not very practical. Fortunately, Nmap supports the loading of targets from an external file. Enter the list of targets into a file, each separated by a new line, tab, or space(s):

Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up (0.16s latency).
Other addresses for scanme.nmap.org (not scanned):
2600:3c01::f03c:91ff:fe18:bb2f
Not shown: 995 closed ports PORT STATE SERVICE
22/tcp open ssh 25/tcp filtered smtp 80/tcp open http
9929/tcp open nping-echo 31337/tcp open Elite
Nmap done: 1 IP address (1 host up) scanned in 333.35 seconds
3

To load the targets from the targets.txt file, use the Nmap -iL <filename> option:

Nmap scan report for scanme.nmap.org (45.33.32.156)
Host is up (0.16s latency).
Other addresses for scanme.nmap.org (not scanned):
2600:3c01::f03c:91ff:fe18:bb2f
Not shown: 995 closed ports PORT STATE SERVICE
22/tcp open ssh 25/tcp filtered smtp 80/tcp open http
9929/tcp open nping-echo 31337/tcp open Elite
Nmap done: 1 IP address (1 host up) scanned in 333.35 seconds
4

Important note

This feature can be combined with any scan option or method, except for exclusion rules set by --exclude or --exclude-file. The --exclude and --exclude-file options will be ignored when -iL is used.

You can also use different target formats in the same file. In the following file, we specify an IP address and an IP range inside the same file:

How to check open ports in Linux?

Use ss command to display all open TCP and UDP ports in Linux. Another option is to use the netstat command to list all ports in Linux. Apart from ss / netstat one can use the lsof command to list open files and ports on Linux based system.

What is the netstat command?

The netstat command generates displays that show network status and protocol statistics. You can display the status of TCP and UDP endpoints in table format, routing table information, and interface information. netstat displays various types of network data depending on the command line option selected.

Which of the following commands can be used to see a list of open ports on a local system?

The netstat command is a CLI tool for network statistics. It gives an overview of network activities and displays which ports are open or have established connections. The netstat tool is essential for discovering network problems.

Which of the following is the command used to display currently open ports on a machine running a Windows operating system?

The command used to display the ports currently open on a Windows operating system is netstat -r. This is an all-in-one solution that integrates a wide range of security features into one appliance.