---
tags:
  - "#Linux/Networking"
parents: "[[Linux Wflow]]"
date/time: 2024-10-15T22:10:00
---


use case:
- check for ports in use - frequent issue is port in use and you forget this and try to use it in another context

# Open ports

## Method 1

If you are logged into a system, either directly or via SSH, you can use the lsof command to check its ports.

```
sudo lsof -i -P -n
```

This [lsof command](https://linuxhandbook.com/lsof-command/) is used to find the files and processes used by a user. The options used here are:

- -i: If no IP address is specified, this option selects the listing of all network files
- -P: inhibits the conversion of port numbers to port names for network files
- -n: inhibits the conversion of network numbers to host names for network files

This way, it will list open ports in the Linux terminal:

![Checking open ports in Linux](https://linuxhandbook.com/content/images/2022/03/check-port-linux.png)

But, this also shows us a lot of extra ports that the computer does not actually listen to.

You can list the listening ports by [piping this output](https://linuxhandbook.com/pipe-redirection/) to the [grep command](https://linuxhandbook.com/grep-command-examples/) and matching the pattern "LISTEN", like this:

```bash
sudo lsof -i -P -n | grep LISTEN
```

This will only show the ports our computer is actively listening to and which service is using said open port.

Now, let's see another method to check open ports in bash shell.

## Method 2: Checking ports on any remote Linux server using the netcat command

[nc (Netcat) is a command line utility](https://linuxhandbook.com/nc-command/) that reads and writes data between computers over the network using the TCP and UDP protocols.

Given below is the syntax for `nc` command:

```bash
nc [options] host port
```

This utility has a nifty `-z` flag. When used, it will make `nc` scan for listening daemons without actually sending any data to the port.

Combine this with the `-v` flag, enabling verbosity, you can get a detailed output.

Below is the command you can use to scan for open ports using the `nc` command:

```bash
nc -z -v <IP-ADDRESS> 1-65535 2>&1 | grep -v 'Connection refused'
```

Replace `IP-ADDRESS` with the [IP address of the Linux system](https://linuxhandbook.com/find-ip-address/) you are checking the ports for.

As for why I selected values `1` to `65535`, that is because the port range starts from `1` and ends at `65535`.

Finally, pipe the output to the `grep` command. Using the `-v` option excludes any line that has "Connection refused" as a matched pattern.

This will show all the ports that are open on the computer which are accessible by another machine on the network.


#  Restart network interface

Use the following command to find out if you are using **systemd networkd service**, or **networking** or **NetworkManager** as service name with the systemd:  

`systemctl list-unit-files --type=service --state=enabled`


- The command to restart depends upon your version of Debian Linux. The latest version of Debian or Ubuntu Linux distro with **systemd networkd service** can use the following command:  

	`sudo systemctl restart systemd-networkd.service`
	`sudo systemctl status systemd-networkd.service`
	
- Debian/Ubuntu Linux with **networking** services, run:  

	`sudo systemctl restart networking   `
	`sudo systemctl status networking`
- The latest version of Ubuntu or Debian service name changed to NetworkManager. Hence, use the systemctl command as follows:  
- 
	`sudo systemctl restart NetworkManager   `
	`sudo systemctl status NetworkManager`
