Freeing up a TCP/IP Port on Linux (2024)

Freeing up a TCP/IP Port on Linux (1)

  • Trending Categories
  • Data Structure
  • Networking
  • RDBMS
  • Operating System
  • Java
  • MS Excel
  • iOS
  • HTML
  • CSS
  • Android
  • Python
  • C Programming
  • C++
  • C#
  • MongoDB
  • MySQL
  • Javascript
  • PHP
  • Physics
  • Chemistry
  • Biology
  • Mathematics
  • English
  • Economics
  • Psychology
  • Social Studies
  • Fashion Studies
  • Legal Studies
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

LinuxOperating SystemOpen Source

';

Introduction

TCP/IP ports are used to connect and communicate with different devices and applications on a network. In Linux, sometimes a specific port may be in use by a particular application or process, preventing other applications from using it. In such cases, it is necessary to free up the port by stopping the process or application using it.

In this article, we will discuss how to free up a TCP/IP port on a Linux system. We will also discuss some common tools and commands that can be used to identify the process or application using a particular port and terminate it.

Identifying the Process or Application Using a Port

Before we can free up a port, we need to identify the process or application using it. Thereare several tools and commands that can be used to do this.

netstat tool

A netstat command is a useful tool for displaying network statistics and the status of network connections. It can be used to list all active TCP/IP connections, including the ports they are using.

To list all active TCP/IP connections, use the following command -

$ netstat -atn

This will display a list of all active TCP connections, along with the local and remote addresses, the state of the connection, and the PID (process ID) of the process using the connection.

To list only the ports being used by a specific process, use the following command -

$ netstat -atn | grep <process name>

For example, to list the ports being used by the Apache web server, use the following command

$ netstat -atn | grep apache

lsof tool

The lsof command (short for "list open files") is another useful tool for identifying the process or application using a particular port. It can be used to list all open files on a system, including network sockets and ports.

To list all open network sockets and their associated PIDs, use the following command -

$ lsof -i

To list only the ports being used by a specific process, use the following command -

$ lsof -i | grep <process name>

For example, to list the ports being used by the Apache web server, use the following command

$ lsof -i | grep apache

Freeing up the port

Once we have identified the process or application using a particular port, we can stop it to free up the port. There are several ways to do this, depending on the process or application.

Terminating the Process

If the process or application using the port is a standalone process, we can simply terminate it to free up the port. To do this, we need to find the PID of the process using the port, and then use the kill command to terminate it.

For example, if the Apache web server is using port 80, we can use the netstat command to find its PID

$ netstat -atn | grep apachetcp 0 0 :::80 :::* LISTEN 1785/httpd

In this case, the PID of the Apache process is 1485. To terminate the process, use the following command

$ kill 1485

Stopping the Service

If the process or application using the port is a service, we can stop the service to free up the port. To do this, we can use the systemctl command.

For example, to stop the Apache web server service, use the following command

$ systemctl stop httpd

Closing the Connection

In some cases, the process or application using the port may be a network connection, rather than a standalone process or service. In such cases, we can use the netstat command to close the connection and free up the port. For example, to close a connection using port 80, use the following command

For example, to close a connection using port 80, use the following command -

$ netstat -atn | grep :80tcp 0 0 192.168.0.43:80 192.168.0.44:56632 ESTABLISHED 1485/httpd

In this case, the connection is being used by the Apache web server (PID 1485). To close the connection, use the following command

$ netstat -atn -f inet -w 1 | grep :80 | grep ESTABLISHED | awk '{print $7}' |awk -F '/' '{print $1}' | xargs kill -9

This command will close all connections using port 80 that are in the ESTABLISHED state.

Conclusion

In this article, we have discussed how to free up a TCP/IP port on a Linux system. We have alsodiscussed some common tools and commands that can be used to identify the process or applicationusing a particular port, and terminate it. By following these steps, you can easily free up a port andallow other applications or processes to use it.

Pradeep Jhuriya

Updated on: 04-Jan-2023

4K+ Views

  • Related Articles
  • TCP/IP port in SAP HANA to access Index server
  • TCP/IP port in SAP HANA to access Statistics server
  • TCP Port
  • TCP/IP port in SAP HANA to access SAP HANA XS Engine
  • The TCP/IP Reference Model
  • TCP/IP in Computer Networking
  • OSI vs. TCP/IP Reference Model
  • The Transport Layer in TCP/IP Model
  • The Application Layer in TCP/IP Model
  • What is the TCP/IP Reference Model?
  • Telnet with IP address and Port in Python
  • The Internet Layer in the TCP/IP Model
  • Advantages and Disadvantages of the TCP/IP Model
  • Difference between OSI and TCP/IP Reference Model
  • What is application layer protocols in TCP/IP?
Kickstart Your Career

Get certified by completing the course

Get Started

Freeing up a TCP/IP Port on Linux (31)

Advertisem*nts

';

Freeing up a TCP/IP Port on Linux (2024)

FAQs

Freeing up a TCP/IP Port on Linux? ›

Using fuser to Free up the Port

How to free a port in Linux command? ›

Here are the steps:
  1. Find the process ID (PID) of the port (replace the 'portNumber' with the number) sudo lsof -i :portNumber. This will give you a response as follows — copy the PID number for the next step:
  2. Kill the process. First, try this (replace PID with the number you copied above): kill PID.
Mar 30, 2020

How to release TCP port? ›

If the process or application using the port is a standalone process, we can simply terminate it to free up the port. To do this, we need to find the PID of the process using the port, and then use the kill command to terminate it.

How to release port in Linux? ›

Linux
  1. Step 1: Get a List of All Open Processes. top.
  2. Step 2: Kill a Process. kill pid. Use killall pname to kill by name. Add -9 for forceful killing. Use sudo if needed. Remember, terminating processes forcefully ( -9 ) should be a last resort as it can cause data loss or system instability.
Feb 5, 2024

How do I force TCP disconnect in Linux? ›

Use the DRop/-D command to terminate an individual TCP connection when you do not want to terminate the server itself, but want only to drop an individual connection with that server. Use the DROP/-D command to terminate old TCP connections if they prevent a server from being restarted.

How do I free a running port? ›

How to kill the process currently using a port on localhost in...
  1. Run command-line as an Administrator. Then run the below mention command. netstat -ano | findstr : port number. ...
  2. Then you execute this command after identify the PID. taskkill /PID typeyourPIDhere /F.
Oct 16, 2019

How to free up a port in use? ›

If the port is in LISTEN or ESTABLISHED state, we free it up by killing the process using it. We should take into account that we'll lose ESTABLISHED connections. Sometimes there isn't any process using the port and, even so, the system tells us the port is busy or in use.

How to change TCP port in Linux? ›

If you are using UFW on a Ubuntu/Debian Linux, type:
  1. $ sudo ufw allow 2222/tcp. See how to open TCP port 22 using the ufw for more info. ...
  2. $ sudo /sbin/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 2222 -j ACCEPT. ...
  3. $ sudo firewall-cmd --permanent --zone=public --add-port=2222/tcp.
Apr 19, 2024

How do I release a port in Unix? ›

How to Close an Open Port in Unix
  1. Type the following command to list all ports listening for a connection lsof -i -P -n | grep LISTEN.
  2. Find the process you would like to terminate.

How to check open TCP ports in Linux? ›

Use the netstat command to list all open ports, including TCP and UDP, which are the most common protocols for packet transmission in the network layer. This will print: all listening sockets ( -l ) the port number ( -n )

How do I disconnect TCP immediately? ›

The common way of terminating a TCP connection is by using the TCP header's FIN flag. This mechanism allows each host to release its own side of the connection individually.

How do I close my TCP port? ›

Close the Port:

Once you've identified the open port, you can use the firewall-cmd command to remove it from the list of open ports. Replace PORT_NUMBER with the actual port number. This command will close the specified TCP port. If you want to close a UDP port, use udp instead of tcp.

How to block TCP port in Linux? ›

Block a Port Using Firewalld

First of all, 'firewall-cmd' is the command for Firewalld; as Firewalld by itself runs as a background listener daemon. Secondly, 22 is the port to be blocked and 'tcp' and 'udp' are the transport level protocol to be blocked on that port.

How to free port 22 in Linux? ›

Checking and Configuring the Firewall

One of the reasons for this error may be that the SSH port is blocked by the firewall. Hence, we need to identify the firewall tool that controls access to the Linux system. Common firewall tools include ufw, iptables, and firewalld. The commands will vary based on the tool in use.

How to free port 53 on Linux? ›

As root, edit the /etc/systemd/resolved. conf with your text editor of choice. Now, you'll want to uncomment (remove the #) the DNS= and DNSStubListener= lines. Now, save your changes and exit the editor.

How to clear a used port in Linux? ›

Method 1: Using the lsof command
  1. Open a terminal window.
  2. Run the following command to identify the process ID (PID) of the process running on the specific port: lsof -i :<port_number> lsof -i :<port_number> ...
  3. Once you have identified the PID, you can use the kill command to stop the process. Run the following command:
Oct 16, 2023

How to open port in Linux command? ›

Ubuntu
  1. $ sudo apt install ufw.
  2. $ sudo systemctl status ufw.
  3. $ sudo ufw enable.
  4. $ sudo ufw allow (port)/tcp.
  5. $ sudo ufw allow 53/tcp.
  6. $ sudo ufw allow https.
  7. $ sudo ufw allow 21.
  8. $ sudo ufw allow from 190.34.21.113 to any port.

Top Articles
ICT Trading Mentorship Customer Reviews
Basics of Battle
Diario Las Americas Rentas Hialeah
Warren Ohio Craigslist
Skyward Houston County
PRISMA Technik 7-10 Baden-Württemberg
Watch Mashle 2nd Season Anime Free on Gogoanime
Why Is Stemtox So Expensive
Mid90S Common Sense Media
Walthampatch
Industry Talk: Im Gespräch mit den Machern von Magicseaweed
Spartanburg County Detention Facility - Annex I
Munich residents spend the most online for food
Aldi Süd Prospekt ᐅ Aktuelle Angebote online blättern
G Switch Unblocked Tyrone
Ally Joann
Adt Residential Sales Representative Salary
Dallas Mavericks 110-120 Golden State Warriors: Thompson leads Warriors to Finals, summary score, stats, highlights | Game 5 Western Conference Finals
Bjerrum difference plots - Big Chemical Encyclopedia
Surplus property Definition: 397 Samples | Law Insider
Boxer Puppies For Sale In Amish Country Ohio
Craigslist Panama City Beach Fl Pets
Craigslist Rome Ny
Is Light Raid Hard
Garden Grove Classlink
Frequently Asked Questions - Hy-Vee PERKS
Learn4Good Job Posting
Red Sox Starting Pitcher Tonight
Half Inning In Which The Home Team Bats Crossword
Ourhotwifes
Most popular Indian web series of 2022 (so far) as per IMDb: Rocket Boys, Panchayat, Mai in top 10
Garrison Blacksmith's Bench
Royals op zondag - "Een advertentie voor Center Parcs" of wat moeten we denken van de laatste video van prinses Kate?
Does Iherb Accept Ebt
Directions To 401 East Chestnut Street Louisville Kentucky
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
Stranahan Theater Dress Code
Thotsbook Com
Grand Valley State University Library Hours
Yourcuteelena
Pickwick Electric Power Outage
San Diego Padres Box Scores
Brutus Bites Back Answer Key
Rubmaps H
2487872771
Tommy Gold Lpsg
Festival Gas Rewards Log In
Duffield Regional Jail Mugshots 2023
Affidea ExpressCare - Affidea Ireland
Comenity/Banter
Latest Posts
Article information

Author: Eusebia Nader

Last Updated:

Views: 6097

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Eusebia Nader

Birthday: 1994-11-11

Address: Apt. 721 977 Ebert Meadows, Jereville, GA 73618-6603

Phone: +2316203969400

Job: International Farming Consultant

Hobby: Reading, Photography, Shooting, Singing, Magic, Kayaking, Mushroom hunting

Introduction: My name is Eusebia Nader, I am a encouraging, brainy, lively, nice, famous, healthy, clever person who loves writing and wants to share my knowledge and understanding with you.