Linux free the port

How to close an open port in Ubuntu?

I need a command to list all open ports in my PC, and another command to close a port.

I need to close some applications’ port.

7 Answers 7

netstat can be used to see the ports stat.

To list all Listening ports Numbers with the Process responsible on each one. Terminate or kill the process to close port. ( kill , pkill . )

Without process termination, It is not possible! . See Manually closing a port from command line. Other way you may look for a firewall solution (as isolating that port from network)

for closing open port in ubuntu you can use below command

in place of 3000 you can specify your port number

lsof command will give information about file opened by process

-t : This flag specifies that lsof should produce terse output with process identifiers only and no header — e.g., so that the output may be piped to kill(1). This option selects the -w option.

-i : This flag selects the listing of files any of whose Internet address matches the address specified in i. If no address is specified, this option selects the listing of all Internet and x.25 (HP-UX) network files.

Источник

Freeing up a TCP/IP port?

netstat -tulnap shows me what ports are in use. How to free up a port in Linux?

11 Answers 11

As the others have said, you’ll have to kill all processes that are listening on that port. The easiest way to do that would be to use the fuser(1) command. For example, to see all of the processes listening for http requests on port 80 (run as root or use sudo):

If you want to kill them, then just add the -k option.

To a kill a specific port in Linux use below command

replace Port_Number with your occupied port.

In terminal type :

netstat -anp|grep «port_number»

It will show the port details. Go to last column. It will be in this format . For example :- PID/java

kill -9 PID. Worked on Centos5

and then execute :

Worked on Macbook

You can use tcpkill (part of the dsniff package) to kill the connection that’s on the port you need:

To check all ports:

To close an open port:

In both cases you can use the sudo command if needed.

The «netstat —programs» command will give you the process information, assuming you’re the root user. Then you will have to kill the «offending» process which may well start up again just to annoy you.

Depending on what you’re actually trying to achieve, solutions to that problem will vary based on the processes holding those ports. For example, you may need to disable services (assuming they’re unneeded) or configure them to use a different port (if you do need them but you need that port more).

Kill the process that is listening to the port in question. I believe netstat shows you process ids.

Читайте также:  Как восстановить папку system32 windows 10

the last column has the process

If you really want to kill a process immediately, you send it a KILL signal instead of a TERM signal (the latter a request to stop, the first will take effect immediately without any cleanup). It is easy to do:

Be aware however that depending on the program you are stopping, its state may get badly corrupted when doing so. You normally only want to send a KILL signal when normal termination does not work. I’m wondering what the underlying problem is that you try to solve and whether killing is the right solution.

I think the only way will be to stop the process which has opened the port.

sudo killall -9 «process name»

Shutting down the computer always kills the process for me.

Источник

What’s the easiest way to find an unused local port?

What’s the easiest way to find an unused local port?

Currently I’m using something similar to this:

It feels awfully roundabout, so I’m wondering if there’s a more simple path such as a builtin that I’ve missed.

17 Answers 17

My solution is to bind to port 0, which asks the kernel to allocate a port from it’s ip_local_port_range. Then, close the socket and use that port number in your configuration.

This works because the kernel doesn’t seem to reuse port numbers until it absolutely has to. Subsequent binds to port 0 will allocate a different port number. Python code:

This gives just a number of a port, eg. 60123 .

Run this program 10 000 times (you should run these concurrently), and you’ll get 10 000 different port numbers. Therefore, I think it’s pretty safe to use the ports.

If your application supports it, you can try passing port 0 to the application. If your application passes this to the kernel, the port will be dynamically allocated at request time, and is guaranteed not to be in use (allocation will fail if all ports are already in use).

Otherwise, you can do this manually. The script in your answer has a race condition, the only way to avoid it is to atomically check if it is open by trying to open it. If the port is in use, the program should quit with a failure to open the port.

For example, say you’re trying to listen with GNU netcat.

One-liner

I’ve put together a nice one-liner that quickly serves the purpose, allowing to grab an arbitrary number of ports in an arbitrary range (here it’s divided in 4 lines for readability):

Line by line

comm is a utility that compares lines in two files that must appear sorted alphabetically. It outputs three columns: lines that appear only in the first file, lines that only appear in the second one and common lines. By specifying -23 we suppress the latter columns and only keep the first one. We can use this to obtain the difference of two sets, expressed as a sequence of text lines. I learned about comm here.

The first file is the range of ports that we can select from. seq produces a sorted sequence of numbers from $FROM to $TO . The result is sorted alphabetically (instead of numerically, in order to comply with comm s requirement) and piped to comm as the first file using process substitution.

The second file is the sorted list of ports, that we obtain by calling the ss command (with -t meaning TCP ports, -a meaning all — established and listening — and -n numeric — don’t try to resolve, say, 22 to ssh ). We then pick only the fourth column with awk , which contains the local address and port. We use cut to split address and port with the : delimiter and keep only the latter ( -f2 ). We then comply with comm ‘s requirement by sort ing without duplicates -u .

Читайте также:  Linux ubuntu delete file

Now we have a sorted list of open ports, that we can shuf fle to then grab the first «$HOWMANY» ones with head -n .

Example

Grab the three random open ports in the private range (49152-65535)

Источник

How to check if port is in use on Linux or Unix

H ow do I determine if a port is in use under Linux or Unix-like system? How can I verify which ports are listening on Linux server? How do I check if port is in use on Linux operating system using the CLI?

It is important you verify which ports are listening on the server’s network interfaces. You need to pay attention to open ports to detect an intrusion. Apart from an intrusion, for troubleshooting purposes, it may be necessary to check if a port is already in use by a different application on your servers. For example, you may install Apache and Nginx server on the same system. So it is necessary to know if Apache or Nginx is using TCP port # 80/443. This quick tutorial provides steps to use the netstat, nmap and lsof command to check the ports in use and view the application that is utilizing the port.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements lsof, ss, and netstat on Linux
Est. reading time 3 minutes

How to check if port is in use in

To check the listening ports and applications on Linux:

  1. Open a terminal application i.e. shell prompt.
  2. Run any one of the following command on Linux to see open ports:
    sudo lsof -i -P -n | grep LISTEN
    sudo netstat -tulpn | grep LISTEN
    sudo ss -tulpn | grep LISTEN
    sudo lsof -i:22 ## see a specific port such as 22 ##
    sudo nmap -sTU -O IP-address-Here
  3. For the latest version of Linux use the ss command. For example, ss -tulw

Let us see commands and its output in details.

Option #1: lsof command

The syntax is:
$ sudo lsof -i -P -n
$ sudo lsof -i -P -n | grep LISTEN
$ doas lsof -i -P -n | grep LISTEN ### [OpenBSD] ###
Sample outputs:

Fig.01: Check the listening ports and applications with lsof command

Option #2: netstat command

You can check the listening ports and applications with netstat as follows.

Linux netstat syntax

Run netstat command along with grep command to filter out port in LISTEN state:
$ netstat -tulpn | grep LISTEN
The netstat command deprecated for some time on Linux. Therefore, you need to use the ss command as follows:
sudo ss -tulw
sudo ss -tulwn
sudo ss -tulwn | grep LISTEN

Where, ss command options are as follows:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

  • -t : Show only TCP sockets on Linux
  • -u : Display only UDP sockets on Linux
  • -l : Show listening sockets. For example, TCP port 22 is opened by SSHD server.
  • -p : List process name that opened sockets
  • -n : Don’t resolve service names i.e. don’t use DNS

FreeBSD/MacOS X netstat syntax

$ netstat -anp tcp | grep LISTEN
$ netstat -anp udp | grep LISTEN

OpenBSD netstat syntax

$ netstat -na -f inet | grep LISTEN
$ netstat -nat | grep LISTEN

Option #3: nmap command

The syntax is:
$ sudo nmap -sT -O localhost
$ sudo nmap -sU -O 192.168.2.13 ##[ list open UDP ports ]##
$ sudo nmap -sT -O 192.168.2.13 ##[ list open TCP ports ]##
Sample outputs:

Fig.02: Determines which ports are listening for TCP connections using nmap

A note about Windows users

You can check port usage from Windows operating system using following command:
netstat -bano | more
netstat -bano | grep LISTENING
netstat -bano | findstr /R /C:»[LISTEING]»

Conclusion

This page explained command to determining if a port is in use on Linux or Unix-like server. For more information see the nmap command and lsof command page online here

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to check if a certain port is open and unused?

I want to install webserver-apache on a Linux platform which uses port no.80 but I am not sure whether is port is open or not , and being used by some other application or not.

  1. Output of grep 80 /etc/services is:

http 80/tcp www www-http #World Wide web Http

http 80/udp www www-http #Hypertext transfer protocol

2. netstat -an | grep 80 | more :

It gives some IP’s one of which is IP:80 TIME_WAIT

Could you please help and tell how can i find out if port 80 is open and unused so that I can start installation.

7 Answers 7

That should give you pid & name of the process that holds port 80

This can be achieved using the nc command as follows:

It will return TRUE if the port is already in use, or FALSE is it (i.e, available not listening currently).

I don’t recommend lsof or netstat method as it first try to scan all running PIDs to get all bounded ports:

Here 8888 is an unused port. The nc command is

85 times faster in the above example.

If you are trying with a remote IP, it is better to add a timeout to auto-exit if it is not accepting connection for the specified time.

$ nc -w 2 -z 216.58.196.174 81

Its Google’s IP which is not used, so it will timeout after trying for 2 seconds.

this one displays bind addresses of tcp listening endpoint. all other endpoints are free; Also if on Unix and you are not root, then you can’t bind to a ‘privileged’ port number (port number lower than 1024)

explained in more detail:

netstat -tln — all listening tcp ports

tail -n +3 — cut of the header of netstat

awk ‘< print $4 >‘ — print the fourth column that consists of [ip]:[port]

for the general case you still need to care to cut out all irrelevant interfaces; a listening address 0.0.0.0 is listening on all network cards, if there is an IP address than that’s the specific IP of the network car/network interface.

Источник

Читайте также:  Windows типті операциялы ж йелер
Оцените статью