How to properly close a port? (2024)

Sylwia Vargas

Posted on • Updated on

How to properly close aport? (3) How to properly close aport? (4) How to properly close aport? (5) How to properly close aport? (6) How to properly close aport? (7)

#beginners #codenewbie #todayisearched

This is a thing I need to google every now and then so here's a simple recipe for closing neglected ports on MacOS, Windows and Linux.

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:
How to properly close aport? (8)

2. Kill the process

First, try this (replace PID with the number you copied above):

kill PID

Now, test if it's closed by connecting to the port (replace portNumber with the actual port number):

nc localhost portNumber

If it returns immediately with no output, the port isn't open. However, if it returns some input, try to kill it with:

kill -9 PID

Again, try to connect. If it's still running, try this:

sudo kill -9 PID

Here are the steps for Windows:

1. Find the process ID (PID) of the port (replace the 'portNumber' with the number)

netstat -ano | findstr :portNumber

Copy the PID number for the next step.

2. Kill the process

First, try this (replace typeyourPIDhere with the number you copied above):

taskkill /PID typeyourPIDhere /F

Run the first command again to see if it's closed.

Here are the steps for Linux (courtesy of mayankjoshi)

1. Get a list of all open processes

$top

2. Kill a process

kill pid kills the process by process id
killall pname kills the process by name
-9 for forceful killing in both kill and killall
Use sudo if it's a root process.

Top comments (23)

Subscribe

CapSel

CapSel

Nothing worth mentioning upfront.

  • Location

    Poland

  • Work

    Do I need a title? at I don't want to give a company name

  • Joined

Mar 31 '20

  • Copy link

Do not use lsof on live Linux (and possibly FreeBSD) servers. In very rare conditions it can cause entire server to hang - hardware reboot needed.

Instead use ss (fast!) and if you really have to then netstat (sloooww, cpu hog).

Sylwia Vargas

Sylwia Vargas

I'm a tech writer and educator advocating for code newbies ✨ I'm also a Developer Relations Lead + front-end dev at @inngest

  • Location

    New York

  • Pronouns

    she/her

  • Joined

Mar 31 '20

  • Copy link

Thank you! I don't think I propose it in the post 🤔
What do you think about the steps proposed by mayankjoshi that I integrated into the blog post:

  1. Get a list of all open processes$top
  2. Kill a processkill pid kills the process by process idkillall pname kills the process by name-9 for forceful killing in both kill and killallUse sudo if it's a root process.

Would you add anything?

CapSel

CapSel

Nothing worth mentioning upfront.

  • Location

    Poland

  • Work

    Do I need a title? at I don't want to give a company name

  • Joined

Mar 31 '20

  • Copy link

ss -tnlp and netstat -tnlp shows pid of processes, their names and their open/listening (aka server) ports. There are tons of tutorials @google about these two commands. top on the other hand does not show open ports. Depending on the OS it can have some shortcuts to kill.

As to root and sudo I would be very careful. You may end up with a surprise ;) You can kill some system service like X server, print spooler, OS upgrade process.

kill (pid) and killall (matching a name) sends signals to processes. Without a name of a signal given default one is used - SIGTERM. This is just "asking" process to exit - similar to alt+f4. The -9 signal is a SIGKILL signal - usually just called kill. It cannot be ignored by processes.

After killing process that had opened TCP port it make take a while before this port is closed. It hangs in OS in special state - only thing you can do is wait or reboot.

Sooner or later you're going to need kill some process to free some port. It's a good idea to glance some docs/manuals (man ss, man netstat) to have some vague memories about what each of these commands can do. Every command is useful. Everyone has their favourite set. Do an experiment - but before you do save your files.

Tisha Murvihill

Tisha Murvihill

  • Joined

Oct 22 '22

  • Copy link

just wanted to say thanks for the info :)

I always have an issue with port 80. The macOS just doesn't want to let it go for non-root users.

Do you have any further suggestions as your script doesn't seem to work for me.

Sylwia Vargas

Sylwia Vargas

I'm a tech writer and educator advocating for code newbies ✨ I'm also a Developer Relations Lead + front-end dev at @inngest

  • Location

    New York

  • Pronouns

    she/her

  • Joined

Mar 30 '20

  • Copy link

Yeah. I had a port 80 problem when Dropbox was running in the background. As soon as I changed that (i.e. now Dropbox only runs when I open it instead of when I switch the laptop on), my problem was fixed. You can see what occupies your port 80 by running sudo lsof -i :YourPortNumber.
changing your root permissions? unix.stackexchange.com/questions/1...

Richard Herbert

Richard Herbert

  • Joined

Mar 30 '20

  • Copy link

Yes, I've tried the DropBox trick but still macOS won't let my process use port 80 unless I start that process as root.

Not sure how that link you offered helps?

Sylwia Vargas

Sylwia Vargas

I'm a tech writer and educator advocating for code newbies ✨ I'm also a Developer Relations Lead + front-end dev at @inngest

  • Location

    New York

  • Pronouns

    she/her

  • Joined

Mar 30 '20

  • Copy link

Eh how annoying. Well you could add root permissions to other users and then you should be able to close the port.

Richard Herbert

Richard Herbert

  • Joined

Mar 30 '20

  • Copy link

Ah, is that what that link was about?

Sylwia Vargas

Sylwia Vargas

I'm a tech writer and educator advocating for code newbies ✨ I'm also a Developer Relations Lead + front-end dev at @inngest

  • Location

    New York

  • Pronouns

    she/her

  • Joined

Mar 30 '20

  • Copy link

Exactly :)

Richard Herbert

Richard Herbert

  • Joined

Mar 30 '20

  • Copy link

Okay, I’ll take a closer look and let you know how I get on.

Thanks for your help.

Richard Herbert

Richard Herbert

  • Joined

Mar 31 '20

  • Copy link

@sylwia - That link seemed to be about file permissions rather than process permissions.

See my reply to Mateusz where I use the insight provided and find my solution,

Mateusz Jarzyna

Mateusz Jarzyna

  • Location

    Poland

  • Joined

Mar 31 '20

  • Copy link

I’s standard, security behavior. You cannot open port <1024 as standard user, you need root permision on both macOS and Linux systemy. Because of security.

When someone will hack into your server, the hacker cannot kill your HTTP server and run phishing site on you domain because he need root privilages.

Richard Herbert

Richard Herbert

  • Joined

Mar 31 '20

  • Copy link

Well, that makes sense.

Armed with this knowledge I researched port forwarding and discovered pf (packet filtering) which lead me to salferrarello.com/mac-pfctl-port-f... which was the answer to my issue.

Thanks for the insight!

mayank joshi

mayank joshi

I love system design and most of the time I find myself learning or designing one of them........I am a moderator at Dev.to 😀

  • Email

    mayank@nlogn.in

  • Location

    Global

  • Education

    NIT Bhopal

  • Joined

Mar 31 '20

  • Copy link

In Linux

$top to see the list of open process

Killing a process

kill pid

  • kills the process by process id

killall pname

  • kills the process by name

-9 for forceful killing in both kill and killall

Use sudo if it's a root process.

Sylwia Vargas

Sylwia Vargas

I'm a tech writer and educator advocating for code newbies ✨ I'm also a Developer Relations Lead + front-end dev at @inngest

  • Location

    New York

  • Pronouns

    she/her

  • Joined

Mar 31 '20

  • Copy link

Thank you! I'll add it right away!

mayank joshi

mayank joshi

I love system design and most of the time I find myself learning or designing one of them........I am a moderator at Dev.to 😀

  • Email

    mayank@nlogn.in

  • Location

    Global

  • Education

    NIT Bhopal

  • Joined

Mar 31 '20

  • Copy link

Actually the URL to my profile is incorrect.😅😅

Sylwia Vargas

Sylwia Vargas

I'm a tech writer and educator advocating for code newbies ✨ I'm also a Developer Relations Lead + front-end dev at @inngest

  • Location

    New York

  • Pronouns

    she/her

  • Joined

Mar 31 '20

  • Copy link

🤦‍♀️corrected!

Lajos Koszti

Lajos Koszti

  • Joined

Mar 31 '20

  • Copy link

It's not closing a port, but stopping a process. You close ports using firewall usually.

mayank joshi

mayank joshi

I love system design and most of the time I find myself learning or designing one of them........I am a moderator at Dev.to 😀

  • Email

    mayank@nlogn.in

  • Location

    Global

  • Education

    NIT Bhopal

  • Joined

Mar 31 '20

  • Copy link

When a process is killed, the ports are automatically Freed.

When I was doing TCP up connection I Freed same port for reuse using this method itself.

Lajos Koszti

Lajos Koszti

  • Joined

Mar 31 '20 • Edited on Mar 31 • Edited

  • Copy link

Luckily, if you stop the application, the port will be released. The title says How to properly close a port?, not how to kill a process. What if you want the keep the process running but don't want to listen on that port anymore?

mayank joshi

mayank joshi

I love system design and most of the time I find myself learning or designing one of them........I am a moderator at Dev.to 😀

  • Email

    mayank@nlogn.in

  • Location

    Global

  • Education

    NIT Bhopal

  • Joined

Mar 31 '20

  • Copy link

I don't think it is possible to free a port held by a process without killing the same process.

Sylwia Vargas

Sylwia Vargas

I'm a tech writer and educator advocating for code newbies ✨ I'm also a Developer Relations Lead + front-end dev at @inngest

  • Location

    New York

  • Pronouns

    she/her

  • Joined

Mar 31 '20

  • Copy link

Sure. I decided for this title because that's what my students google — the post is in my budding series primarily for my students at a coding bootcamp ❤️

View full discussion (23 comments)

For further actions, you may consider blocking this person and/or reporting abuse

How to properly close a port? (2024)
Top Articles
How to Tell If Someone Is Actually Rich
Apple Credit Card Reviews (September 2024)
Funny Roblox Id Codes 2023
Star Wars Mongol Heleer
55Th And Kedzie Elite Staffing
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Jazmen Jafar Linkedin
Atvs For Sale By Owner Craigslist
Gameday Red Sox
Big Y Digital Coupon App
Strange World Showtimes Near Amc Braintree 10
Savage X Fenty Wiki
Turning the System On or Off
Hca Florida Middleburg Emergency Reviews
Shreveport Active 911
Wizard Build Season 28
Bend Pets Craigslist
Me Cojo A Mama Borracha
Prosser Dam Fish Count
E22 Ultipro Desktop Version
Vigoro Mulch Safe For Dogs
Persona 4 Golden Taotie Fusion Calculator
20 Different Cat Sounds and What They Mean
Poe Str Stacking
Miltank Gamepress
Www Craigslist Madison Wi
Employee Health Upmc
Buying Cars from Craigslist: Tips for a Safe and Smart Purchase
Defending The Broken Isles
پنل کاربری سایت همسریابی هلو
Wonder Film Wiki
Bolly2Tolly Maari 2
Studentvue Calexico
Jailfunds Send Message
Shiny Flower Belinda
By.association.only - Watsonville - Book Online - Prices, Reviews, Photos
His Only Son Showtimes Near Marquee Cinemas - Wakefield 12
Nurofen 400mg Tabletten (24 stuks) | De Online Drogist
Chadrad Swap Shop
Elizaveta Viktorovna Bout
The Transformation Of Vanessa Ray From Childhood To Blue Bloods - Looper
Labyrinth enchantment | PoE Wiki
National Insider Threat Awareness Month - 2024 DCSA Conference For Insider Threat Virtual Registration Still Available
1v1.LOL Game [Unblocked] | Play Online
Weather Underground Corvallis
Charli D'amelio Bj
Rs3 Nature Spirit Quick Guide
Sc Pick 3 Past 30 Days Midday
Dlnet Deltanet
Bellin Employee Portal
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 6650

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.