Check Which Ports Are Open Linux
Ever wondered if your Linux machine is playing nice with the world? Like, is it all 'hello, I'm open for business' or is it more of a 'leave me alone, I'm busy' kind of vibe?...
Ever wondered if your Linux machine is playing nice with the world? Like, is it all 'hello, I'm open for business' or is it more of a 'leave me alone, I'm busy' kind of vibe? Let's find out, shall we?
Why Should You Care?
Ports are like the doormen of your Linux machine. They control who comes in and out. Some are always open, like the friendly bouncer at your favorite pub. Others are closed, like the grumpy neighbor who never wants to chat. Knowing which is which can help you troubleshoot issues, secure your system, or even set up a secret speakeasy on your home server. Intrigued? Let's dive in!
Meet the Tools
We're going to use two command-line tools to check open ports: ss and netstat. They're like the Sherlock Holmes and Dr. Watson of port investigation. Let's start with ss because it's a bit more modern and fancy.
Must Read
ss stands for 'socket statistics'. It's a bit like the bartender at our metaphorical pub, keeping track of all the comings and goings. Here's how you use it:
ss -tln
Let's break that down. -t means we're looking at TCP ports. -l shows us listening ports (the ones that are open and ready to receive connections). -n means we want numbers, not names (because who wants to wait for DNS lookups?).
Now, let's take a look at netstat. It's an old-timer, but it's still got some useful tricks up its sleeve. Here's how you use it to see open ports:
Kiểm tra cổng đang mở trên Linux: Hướng dẫn chi tiết và công cụ hữu ích
netstat -tuna
Again, let's break it down. -t is for TCP, -u is for UDP (another type of port), -n gives us numbers, and -a shows us all ports, both listening and established.
What Do Those Numbers Mean?
So, you've run the commands and you're looking at a list of numbers. What do they mean? Well, each number represents a port. The most well-known ones are in the well-known ports range (0-1023). For example, HTTP (the language of the web) uses port 80, and HTTPS (the secure version) uses 443.
Ports 1024 to 49151 are called registered ports. They're used by services that have been registered with the Internet Assigned Numbers Authority (IANA). Above 49152 are dynamic or private ports. They're used by regular folks like you and me for all sorts of things.
How to check open ports in Linux using the CLI - nixCraft
What If a Port's Closed?
If a port's closed, you might see it listed as 'LISTEN' or 'CLOSE_WAIT' or something similar. Don't worry, it's not stuck in an awkward social situation. It just means it's not currently accepting connections. It could be because the service that uses that port is turned off, or it could be because it's already connected to something else.
But Wait, There's More!
You can use ss and netstat to do all sorts of fun things. You can check which processes are using which ports, you can see where your traffic is coming from and going to, you can even use them to diagnose network issues. But that's a story for another time.
For now, go forth and explore the world of open ports! Just remember, with great knowledge comes great responsibility. Don't go messing with ports you don't understand. And always, always, be careful when changing settings on your Linux machine. You don't want to turn your friendly bouncer into a grumpy neighbor!