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 😀

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 😀

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 😀

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 😀

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
The 3-4-2-1 Formation: Its Uses, Strengths, and Weaknesses
How to unlock a door using NFC | Doordeck Keyless Access Control
Friskies Tender And Crunchy Recall
Live Basketball Scores Flashscore
9192464227
Produzione mondiale di vino
Santa Clara Valley Medical Center Medical Records
Conduent Connect Feps Login
OSRS Dryness Calculator - GEGCalculators
Pittsburgh Ultra Advanced Stain And Sealant Color Chart
Spartanburg County Detention Facility - Annex I
Playgirl Magazine Cover Template Free
Curtains - Cheap Ready Made Curtains - Deconovo UK
Xxn Abbreviation List 2023
Sport-News heute – Schweiz & International | aktuell im Ticker
25Cc To Tbsp
Voy Boards Miss America
Pekin Soccer Tournament
Officialmilarosee
CDL Rostermania 2023-2024 | News, Rumors & Every Confirmed Roster
Dover Nh Power Outage
Puss In Boots: The Last Wish Showtimes Near Cinépolis Vista
Heart Ring Worth Aj
Raz-Plus Literacy Essentials for PreK-6
Nesb Routing Number
Essence Healthcare Otc 2023 Catalog
Relaxed Sneak Animations
Stockton (California) – Travel guide at Wikivoyage
Mumu Player Pokemon Go
Kokomo Mugshots Busted
Everstart Jump Starter Manual Pdf
Puerto Rico Pictures and Facts
Craigslist In Myrtle Beach
Goodwill Thrift Store & Donation Center Marietta Photos
Pensacola 311 Citizen Support | City of Pensacola, Florida Official Website
Mta Bus Forums
Labyrinth enchantment | PoE Wiki
Trivago Myrtle Beach Hotels
What Does Code 898 Mean On Irs Transcript
Thelemagick Library - The New Comment to Liber AL vel Legis
Wayne State Academica Login
Directions To The Closest Auto Parts Store
VDJdb in 2019: database extension, new analysis infrastructure and a T-cell receptor motif compendium
Powerspec G512
2Nd Corinthians 5 Nlt
The Many Faces of the Craigslist Killer
A rough Sunday for some of the NFL's best teams in 2023 led to the three biggest upsets: Analysis
Aurora Southeast Recreation Center And Fieldhouse Reviews
300 Fort Monroe Industrial Parkway Monroeville Oh
Strange World Showtimes Near Century Federal Way
Southwind Village, Southend Village, Southwood Village, Supervision Of Alcohol Sales In Church And Village Halls
Craigs List Sarasota
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.