Network Commands
Chapters and transcript
Network Commands Security work is full of network questions. What is my IP address? Can I reach that server? What does this domain point to? And which ports is my own machine exposing? Linux has a short command that answers each one. Let us learn them, one by one.
Addresses and ping First, i p a shows my network interfaces. L o is the loopback. One two seven dot zero dot zero dot one always means this machine. E t h zero is the real network card, with the private address ten dot zero dot zero dot twelve. Next, ping dash c three sends three test messages. Each reply shows the round trip time. But no reply does not always mean the host is down. Many firewalls block ping.
The path Trace route shows each hop, meaning each router, on the way to a target. Our traffic passes the first router, ten dot zero dot zero dot one, then a second router, and finally reaches the target. Each hop shows its time, so you can see where a connection gets slow. If a hop does not reply, you see three stars.
DNS lookups Dig asks DNS for the records of a domain. You can ask for different record types. A records are IPv4 addresses. M X records name the mail servers. T X T records hold text, often used for email security settings. Adding plus short prints only the answer. There is also an older tool, n s lookup, that also exists on Windows.
DNS and web headers Let me try it. Dig chai corp dot example, plus short, gives one IP address. Asking for M X shows the mail server. Now curl talks to web servers from the terminal. With dash capital I, it asks only for the headers. Look at the Server header. It reveals the web server and its version. Testers write this down, and defenders often hide the version number.
Listening ports A program that waits for connections is listening on a port. To see every listening port on your machine, use s s dash t u l p n. Each letter is an option. T is TCP, and u is UDP. L shows only listening ports. P shows the program behind each port, and needs sudo to see all of them. N shows numbers, like port twenty-two, instead of names.
Listening ports Here it is with sudo. Three programs are listening. S s h d on port twenty-two, my s q l on port thirty-three oh six, and n ginx on port eighty. Now look closely at the Local Address column. SSH and the web server listen on zero dot zero dot zero dot zero. The database listens on one two seven dot zero dot zero dot one. That difference matters a lot.
0.0.0.0 and 127.0.0.1 Watch what that means from the outside. SSH listens on zero dot zero dot zero dot zero, so it accepts connections from any network. That is expected for SSH and the web server. The database listens on one two seven dot zero dot zero dot one, the loopback, so the outside cannot reach it at all. Only the local web app can. That is good. A database on zero dot zero dot zero dot zero with no firewall would be a serious problem.
Quick check Quick check. On another server, s s shows my s q l listening on zero dot zero dot zero dot zero, port thirty-three oh six, and there is no firewall. What does that mean? Take a moment, then choose.
Firewalls A firewall decides which traffic is allowed in. On Ubuntu, u f w manages it. First, deny all incoming connections by default. Then allow only SSH on port twenty-two, and HTTPS on four four three. Now a database connection is blocked. Careful: on a remote server, allow SSH before you enable the firewall, or you lock yourself out. And only use these commands on your own machines.
Putting it together Let us put it together. I p a shows your addresses. Ping and trace route test the path to a host. Dig looks up DNS, and curl dash capital I shows web headers. S s dash t u l p n lists your listening ports, and the local address tells you who can reach them. U f w allows only what must be public. Next, you will see ports from the outside, with n map.
Security work is full of network questions. What is my IP address? Can I reach that server? What does this domain point to? Which ports is my machine exposing? This lesson covers the Linux commands that answer them.
Your addresses: ip a
ip a (short for ip address) shows your network interfaces and their IP addresses. An interface is a network connection, such as a network card or Wi-Fi.
ali@server:~$ ip a
1: lo: <LOOPBACK,UP> mtu 65536
inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP> mtu 1500
inet 10.0.0.12/24 brd 10.0.0.255 scope global eth0
lois the loopback interface.127.0.0.1always means "this machine".eth0is the real network card. Here its address is10.0.0.12, a private address.
You may also see the older command ifconfig in guides. It does a similar job but is no longer installed by default on many systems.
Is it reachable? ping
ping sends small test messages and waits for replies. It tells you if a host answers and how long the round trip takes.
ali@server:~$ ping -c 3 192.0.2.10
64 bytes from 192.0.2.10: icmp_seq=1 ttl=56 time=21.4 ms
64 bytes from 192.0.2.10: icmp_seq=2 ttl=56 time=20.9 ms
64 bytes from 192.0.2.10: icmp_seq=3 ttl=56 time=21.1 ms
-c 3 means send three messages and stop. Without it, Linux keeps pinging until you press Ctrl+C.
No reply does not always mean the host is down. Many firewalls block ping on purpose.
The path: traceroute
traceroute shows each hop, each router, that your traffic passes through on the way to a target.
ali@server:~$ traceroute 192.0.2.10
1 10.0.0.1 1.2 ms
2 198.51.100.1 8.7 ms
3 192.0.2.10 21.3 ms
It helps you see where a connection is slow or where it stops. A line of * * * means that hop did not reply.
DNS lookups: dig and nslookup
dig asks DNS for the records of a domain.
ali@server:~$ dig chaicorp.example +short
203.0.113.20
The +short option prints only the answer. You can ask for other record types too:
| Command | What it finds |
|---|---|
dig chaicorp.example A |
IPv4 addresses |
dig chaicorp.example MX |
Mail servers |
dig chaicorp.example TXT |
Text records, often used for email security settings |
nslookup chaicorp.example is an older tool that does a similar job and also exists on Windows.
Talking to web servers: curl -I
curl sends web requests from the terminal. With -I, it asks only for the headers, the information the server sends before the page itself.
ali@server:~$ curl -I https://chaicorp.example
HTTP/1.1 200 OK
Server: nginx/1.18.0
Content-Type: text/html
The Server header can reveal which web server and version is running. Testers note this, and defenders often hide the version number.
Listening ports: ss -tulpn
A program that waits for connections is listening on a port. ss -tulpn shows every listening port on your machine.
| Option | Meaning |
|---|---|
-t |
TCP ports |
-u |
UDP ports |
-l |
Only listening ports |
-p |
Show the program that owns each port (needs sudo to see all) |
-n |
Show numbers, not names (port 22, not ssh) |
ali@server:~$ sudo ss -tulpn
Netid State Local Address:Port Process
tcp LISTEN 0.0.0.0:22 users:(("sshd",pid=812))
tcp LISTEN 127.0.0.1:3306 users:(("mysqld",pid=930))
tcp LISTEN 0.0.0.0:80 users:(("nginx",pid=1440))
The older tool netstat -tulpn shows similar information. You will still see it in many guides.
0.0.0.0 versus 127.0.0.1
The Local Address column tells you who can reach a service. This matters a lot for security.
| Listening on | Who can connect |
|---|---|
127.0.0.1 |
Only programs on the same machine |
0.0.0.0 |
Any network the machine is connected to, possibly the whole internet |
A specific address, such as 10.0.0.12 |
Only through that one interface |
In the example above, MySQL listens on 127.0.0.1, so only the local web application can talk to it. That is good. SSH and the web server listen on 0.0.0.0, so they are reachable from outside. That is expected for those services. A database listening on 0.0.0.0 with no firewall would be a serious problem.
Firewalls: ufw
A firewall decides which network traffic is allowed in and out. On Ubuntu, a simple tool called ufw (uncomplicated firewall) manages it.
sudo ufw default deny incoming
sudo ufw allow 22/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status
This blocks all incoming connections, then allows only SSH and HTTPS. Be careful when you set up a firewall on a remote server: if you block SSH before allowing it, you can lock yourself out.
Use these commands on your own machines. Testing networks or servers that belong to others needs their written permission.
Next, you will learn how testers use nmap to see a machine's open ports from the outside.
Check your understanding
Answer every question correctly to complete this lesson. If you miss one, try the quiz again.
1. Which command shows the ports your Linux machine is listening on?
2. A database is listening on 127.0.0.1:3306. Who can connect to it?
3. What does curl -I show?
Locked: answer every quiz question above correctly to complete this lesson.
Spotted a mistake or something unclear? Report a mistake