07 — Networking¶
Navigation: ← 06 — Process Management | Notes Index | Next → 08-01 — Shell Scripting: Basics Exercise: Exercise 07
Basic Networking Concepts¶
Before diving into commands, here are the key terms you need to understand:
| Term | What it means |
|---|---|
| IP Address | A unique number that identifies a device on a network. Like a home address, but for computers. Example: 192.168.1.10 |
| Subnet Mask | Defines which part of the IP address is the network and which part is the device. Example: /24 or 255.255.255.0 |
| Gateway | The router that connects your local network to the wider internet. Usually ends in .1, e.g., 192.168.1.1 |
| DNS | Translates human-friendly names (like google.com) into IP addresses. Like a phone book for the internet. |
| Port | A number that identifies a specific service on a computer. SSH uses port 22, web servers use port 80 (HTTP) or 443 (HTTPS). |
| Network Interface | The hardware or virtual component that connects your machine to a network (e.g., eth0 = ethernet, wlan0 = WiFi). |
Viewing Your Network Configuration¶
ip addr — What are my IP addresses?¶
Or the short form:
Example output:
1: lo: <LOOPBACK,UP> mtu 65536
link/loopback 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500
link/ether 52:54:00:ab:cd:ef brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
Reading the output:
- lo is the loopback interface — a virtual interface that loops back to the same machine. IP 127.0.0.1 always means "this computer itself".
- eth0 is your actual ethernet network interface.
- inet 192.168.1.10/24 is the IP address. The /24 is the subnet mask.
ip route — What is my default gateway?¶
Example output:
The line starting with default via tells you the default gateway (192.168.1.1 here). All traffic that doesn't match another route goes through this gateway to reach the internet.
Testing Connectivity¶
ping — Is this host reachable?¶
ping sends small packets to the target and measures how long it takes to get a response. It's the first tool to use when troubleshooting network problems.
Example output:
PING google.com (142.250.185.78): 56 data bytes
64 bytes from 142.250.185.78: icmp_seq=0 ttl=118 time=12.4 ms
64 bytes from 142.250.185.78: icmp_seq=1 ttl=118 time=11.8 ms
time=12.4 ms— how long the round trip took. Lower is better.- Press
Ctrl + Cto stop.
-c 4 means "send exactly 4 packets, then stop". Without -c, ping runs forever until you press Ctrl+C.
What if ping fails? - No response → the host might be down, or ICMP is blocked by a firewall - "Name or service not known" → DNS is broken (your computer can't resolve the hostname to an IP)
traceroute — How does traffic get there?¶
Shows each "hop" (router) that your traffic passes through on its way to the destination. Useful for diagnosing where a connection is being slow or dropped.
Example output:
1 192.168.1.1 (gateway) 0.5 ms
2 10.0.0.1 (ISP router) 5.2 ms
3 ...
15 142.250.185.78 (google.com) 12.4 ms
If you see * * * on a line, that router is not responding to traceroute (often normal — many routers block these probes).
nc (netcat) — Test if a port is open¶
Breaking this down:
- nc — netcat, the "Swiss Army knife" of networking
- -z — "zero-I/O mode": just check if the port is open, don't send data
- -v — verbose: print what's happening
- google.com — the host
- 80 — the port to test
Example output:
If you see this, the host is accepting connections on that port. If it says "connection refused" or times out, the port is closed or blocked.
DNS — Translating Names to IPs¶
nslookup — Simple DNS lookup¶
Example output:
Server: 8.8.8.8 ← which DNS server answered
Address: 8.8.8.8#53
Non-authoritative answer:
Name: google.com
Address: 142.250.185.78 ← the IP address for google.com
dig — Detailed DNS query¶
Gives much more detailed DNS information than nslookup. Useful for troubleshooting DNS problems.
A— look up A records (IPv4 addresses only)+short— print only the result, no extra info
Output:
Look up MX records — which mail servers handle email for google.com.
DNS configuration files¶
Shows which DNS server your machine uses. You'll see lines like:
These are Google's public DNS servers.
This file is checked before DNS. You can add entries here to override DNS for specific names — useful for development:
After adding an entry here, you can use mydevserver.local instead of the IP address.
Viewing Open Ports and Connections¶
ss — What ports is this machine listening on?¶
Each flag has a meaning:
- -t — show TCP connections
- -u — show UDP connections
- -l — show only listening ports (servers waiting for connections)
- -n — show numbers instead of names (e.g., show 80 instead of http)
- -p — show which process is using each port
Example output:
Netid State Recv-Q Send-Q Local Address:Port
tcp LISTEN 0 128 0.0.0.0:22 → SSH (port 22)
tcp LISTEN 0 511 0.0.0.0:80 → nginx (port 80)
tcp LISTEN 0 128 127.0.0.1:5432 → PostgreSQL (port 5432, local only)
This tells you exactly which services are accepting connections and on which ports. This is very useful for security auditing.
The Firewall — UFW¶
A firewall controls which network connections are allowed into and out of your machine. UFW (Uncomplicated Firewall) is the beginner-friendly firewall tool on Ubuntu.
Important: Before enabling the firewall, always allow SSH first, or you will lock yourself out of a remote server.
Check firewall status¶
Shows whether the firewall is active and lists all the current rules.
Allow SSH (do this FIRST if remote)¶
This adds a rule to allow SSH connections (port 22). This is equivalent to:
Enable the firewall¶
Activates the firewall with the rules you've set.
Allow web traffic¶
Allow a custom port¶
Allows incoming TCP connections on port 8080.
Block a port¶
Blocks Telnet (port 23) — it should never be exposed anyway.
Allow from a specific IP only¶
Allows SSH connections only from the 192.168.1.x network. Nobody outside that network can SSH in.
View rules with numbers¶
Shows rules with numbers so you can delete them by number:
Downloading Files¶
wget — Download a file from the internet¶
Downloads file.zip to your current directory. You'll see a progress bar.
-O (capital O) specifies the output filename.
curl — Transfer data with a URL¶
curl is more flexible than wget. It can also send data, not just receive it.
Download a file (capital -O saves with the original filename).
Fetch and print the response of an API to the terminal. Very useful for testing web services.
-s means "silent" — no progress output. This particular URL returns your public IP address.
Copying Files Between Machines — SCP¶
scp (Secure Copy) copies files over SSH. It uses the same authentication as SSH.
Breaking this down:
- scp — the command
- file.txt — the file to copy (local)
- jahid@192.168.1.10 — username and IP of the remote machine
- :/home/jahid/ — where to put it on the remote machine
Copy from a remote machine to the current local directory (./).
-r copies a directory recursively.
Navigation: ← 06 — Process Management | Notes Index | Next → 08-01 — Shell Scripting: Basics Exercise: Exercise 07