Notes: Networking, Ports, Firewalls
Check your understanding of interfaces, addressing, routing, DNS, ports, firewalls and network troubleshooting.
-
ip addr show -
ifconfig -a -
netstat -i -
route -n
Explanation:
ip addr show(orip a) from iproute2 lists every interface with its IPv4 and IPv6 addresses.ifconfig,netstatandroutecome from the deprecated net-tools package, which many distributions no longer install by default.
Q. Which command lists interfaces with their MAC addresses and UP/DOWN state, but without IP addresses?
-
ip addr show -
ip route show -
ip link show -
ip neigh show
Explanation:
ip linkworks at the link layer: interface names, MAC addresses, MTU and state.ip addradds the IP addresses,ip routeshows routes andip neighshows the ARP/neighbour cache.
-
ip -s link -
ip addr -
ip route -
ss -s
Explanation: The
-sflag adds statistics toip link, showing bytes, packets, errors and drops per interface.ss -ssummarises sockets, not interface counters.
- Removes the IP address from
eth0but keeps the link up - Disables the
eth0interface until it is brought up again - Deletes the
eth0interface from the system permanently - Flushes the ARP cache entries learned on
eth0
Explanation: Setting a link
downstops the interface from sending or receiving traffic;ip link set eth0 upre-enables it. The device and its IPv4 addresses remain configured, and the change does not survive a reboot.
- The service answers on the loopback interface inside the same machine
- Remote clients can reach the server on port 80
- The default gateway is configured correctly
- DNS resolution for the server's hostname works
Explanation:
127.0.0.1is the loopback address: the machine talking to itself throughlo. It confirms the service runs locally but says nothing about routing, DNS or firewalls for remote clients.
Q. A host on 192.168.1.0/24 with default gateway 192.168.1.1 sends a packet to 8.8.8.8 over Ethernet. Which addresses does the outgoing frame carry?
- Destination IP
192.168.1.1and the router's MAC address - Destination IP
8.8.8.8and a MAC address belonging to8.8.8.8 - Destination IP
8.8.8.8and the gateway's MAC address - Destination IP
8.8.8.8and the broadcast MACff:ff:ff:ff:ff:ff
Explanation: IP addresses identify the end points and stay the same along the path, while MAC addresses only matter on the local link. The host learns the gateway's MAC through ARP (
ip neigh) and hands the frame to the router.
-
172.32.10.5 -
192.169.1.5 -
11.0.0.5 -
172.20.10.5
Explanation: The private ranges are
10.0.0.0/8,172.16.0.0/12(172.16.0.0to172.31.255.255) and192.168.0.0/16.172.32.x.xfalls just outside the 172.16/12 block, and192.169.x.xis not192.168.x.x.
- 24
- 254
- 255
- 256
Explanation:
/24means 24 network bits, leaving 8 host bits, or$2^8 = 256$ addresses. The all-zeros network address and the all-ones broadcast address are reserved, leaving$2^8 - 2 = 254$ for hosts.
Q. A host has the address 192.168.1.10/24 and the default gateway 192.168.1.1. How does it deliver packets to 192.168.1.77 and to 192.168.2.20?
-
192.168.1.77directly on the local link;192.168.2.20through192.168.1.1 - Both directly on the local link, because they share the first two octets
- Both through
192.168.1.1, because every packet goes to the gateway -
192.168.2.20directly on the local link;192.168.1.77through192.168.1.1
Explanation: The
/24prefix makes192.168.1.0–192.168.1.255the local network, so.77is reached directly.192.168.2.20is outside it and is sent to the gateway.ip route get ADDRESSshows which case applies.
- Request, Offer, Discover, Acknowledge
- Offer, Discover, Acknowledge, Request
- Discover, Request, Offer, Acknowledge
- Discover, Offer, Request, Acknowledge
Explanation: The client broadcasts DHCPDISCOVER, a server replies with DHCPOFFER, the client asks for that address with DHCPREQUEST, and the server confirms the lease with DHCPACK (often remembered as DORA).
Q. A laptop shows the address 169.254.23.7 on its Ethernet interface. What is the most likely cause?
- The interface was given a static public IP address
- DNS is returning the wrong address for the hostname
- The client failed to get a lease from a DHCP server
- The firewall is blocking outgoing ICMP traffic
Explanation:
169.254.x.xis a link-local address a host assigns itself when DHCP fails. Check the cable or Wi-Fi,ip link,nmcli dev statusand the DHCP server.
Q. A printer must always get the same IP address, but you want to keep managing addresses centrally on the DHCP server. What should you configure?
- A shorter DHCP lease time on the printer
- A static route to the printer via the gateway
- An
/etc/hostsentry on every client - A DHCP reservation for the printer's MAC address
Explanation: A DHCP reservation makes the server always hand the same address to a given MAC address. The address stays stable without manual configuration on the device itself.
-
ip addr show default -
ip route show default -
ip link show default -
ip neigh show default
Explanation:
ip route show defaultprints a line such asdefault via 192.168.1.1 dev eth0, where the address afterviais the default gateway. The default route matches any destination with no more specific route.
default via 192.168.1.1 dev eth0
10.20.0.0/16 via 192.168.1.2 dev eth0
10.20.5.0/24 via 192.168.1.3 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10
-
192.168.1.1, because the default route is listed first -
192.168.1.2, because10.20.0.0/16is the first matching route -
192.168.1.3, because10.20.5.0/24is the longest matching prefix - None; the packet is delivered directly on
eth0
Explanation: Linux picks the most specific route (longest prefix), not the first one listed. Both
/16and/24match10.20.5.9, and/24is longer.10.20.9.9would use192.168.1.2, and10.30.0.1the default route.
Q. Which command prints the route, outgoing interface and source address the kernel would actually use to reach 8.8.8.8?
-
ip route show 8.8.8.8 -
ip neigh show 8.8.8.8 -
traceroute -n 8.8.8.8 -
ip route get 8.8.8.8
Explanation:
ip route getperforms a real route lookup and prints a line such as8.8.8.8 via 192.168.1.1 dev eth0 src 192.168.1.10.ip route show 8.8.8.8lists only routes whose prefix is exactly that address, usually none.
-
sudo ip route add default via 192.168.1.254 -
sudo ip addr add default 192.168.1.254 -
sudo ip link set default via 192.168.1.254 -
sudo ip route add 192.168.1.254 dev default
Explanation: Routes are managed with
ip route, andvianames the next-hop router. Changes made this way are runtime only; make them persistent through NetworkManager, netplan or the distribution's network files.
-
ip route add 192.168.1.1 via 10.20.0.0/16 -
ip addr add 10.20.0.0/16 via 192.168.1.1 -
ip route add 10.20.0.0/16 via 192.168.1.1 -
ip route add 10.20.0.0/16 dev 192.168.1.1
Explanation: The syntax is
ip route add DESTINATION via GATEWAY.devexpects an interface name such aseth0, not an IP address.
- The DNS servers in
/etc/resolv.conf - The MAC address of the local interface
- The entries in
/etc/hosts - The default route and the path beyond the router
Explanation: The local network works, but traffic to other networks does not. Check
ip routefor a correct default gateway and usetraceroute 8.8.8.8to see where packets stop. DNS is not involved when pinging a raw IP.
Q. ping 8.8.8.8 works, but ping google.com fails with "Name or service not known". What is the most likely problem?
- Name resolution (DNS) is broken
- The interface is down
- The default gateway is missing
- ICMP is blocked by the firewall
Explanation: Reaching an IP address proves the interface, routing and ICMP all work. Failing only on a name points to DNS. Inspect
/etc/resolv.conf,resolvectl status, and comparedig google.comwithdig @8.8.8.8 google.com.
Q. Which command queries a specific DNS server, 8.8.8.8, directly and bypasses the system's configured resolver?
-
dig google.com 8.8.8.8 -
dig @8.8.8.8 google.com -
nslookup -server 8.8.8.8 -
host -r 8.8.8.8 google.com
Explanation:
dig @SERVER NAMEsends the query to that server. If this works while plaindig google.comfails, the locally configured resolver is the problem. Without@,digtreats8.8.8.8as a second name to look up.
-
/etc/hosts -
/etc/hostname -
/etc/resolv.conf -
/etc/nsswitch.conf
Explanation:
/etc/resolv.confholdsnameserverandsearchlines. On many modern systems it is generated by NetworkManager or systemd-resolved, so manual edits may be overwritten./etc/nsswitch.confsets lookup order, not servers.
Q. On Ubuntu with systemd-resolved, /etc/resolv.conf contains only nameserver 127.0.0.53. Which command shows the upstream DNS servers actually in use per interface?
-
cat /etc/hostname -
ip route show -
nmcli con reload -
resolvectl status
Explanation:
127.0.0.53is systemd-resolved's local stub resolver, which forwards queries to the real servers.resolvectl statusshows the upstream servers configured for each link.
Q. In /etc/resolv.conf, which directive lists the domains appended to short names, so that ping web1 can resolve web1.corp.example?
-
search -
nameserver -
options -
sortlist
Explanation:
search corp.examplemakes the resolver tryweb1.corp.examplewhen you look up the short nameweb1.nameserversets server addresses andoptionstunes resolver behaviour such as timeouts.
- DNS servers on the network start answering for
myserver.local - Traffic to
192.168.1.50is routed throughmyserver.local - This machine resolves
myserver.locallocally, usually before asking DNS - The machine's own hostname changes to
myserver.local
Explanation:
/etc/hostsis a static, local name-to-IP table. Thehosts:line in/etc/nsswitch.confnormally listsfilesbeforedns, so it is checked first. It affects only the machine where the file is edited.
Q. Which command resolves a name the same way applications do, following /etc/nsswitch.conf and therefore including /etc/hosts?
-
dig myserver.local -
host myserver.local -
nslookup myserver.local -
getent hosts myserver.local
Explanation:
getentgoes through the system's NSS lookup order, likepingorcurl.dig,hostandnslookuponly send DNS queries, so they can disagree with what applications see when/etc/hostsor other NSS sources are involved.
Q. With NetworkManager, which setting stops a connection from using the DNS servers handed out by DHCP?
-
ipv4.method manual -
ipv4.ignore-auto-dns yes -
ipv4.never-default yes -
ipv4.dns-search ""
Explanation: After setting
ipv4.dnswithnmcli con mod,ipv4.ignore-auto-dns yeskeeps the DHCP-supplied servers out.ipv4.method manualwould turn off DHCP addressing entirely, not just DNS.
-
nmcli con reload -
nmcli con up eth0 -
nmcli dev status -
nmcli general hostname
Explanation:
nmcli dev statuslists devices with their type, state and active connection profile.nmcli con showlists saved connection profiles instead.
Q. You change the address of the active profile eth0 with nmcli con mod eth0 ipv4.addresses 192.168.1.20/24, but ip addr still shows the old address. What applies the change?
-
nmcli con reload -
nmcli con show eth0 -
ip link set eth0 up -
nmcli con up eth0
Explanation:
nmcli con modonly saves the profile; re-activating it withnmcli con upapplies the new settings (nmcli dev reapplyalso works).nmcli con reloadonly re-reads profile files edited by hand on disk.
-
ping -w 5 example.com -
ping -c 5 example.com -
traceroute -m 5 example.com -
ping -i 5 example.com
Explanation:
-csets the packet count.-w 5sets a five-second deadline instead,-i 5waits five seconds between packets, andtraceroute -mlimits hop count.
- The bandwidth available on each link to the host
- Only the final destination's IP address
- The open ports on each router along the path
- Each router hop on the path, as numeric IPs without DNS lookups
Explanation:
traceroutelists each hop and its latency, which helps locate where delay or loss begins.-nskips reverse DNS lookups, making the output faster and showing raw IPs.tracepathis a similar tool that needs no root.
Q. Which command shows listening TCP and UDP sockets with numeric ports and the process that owns each one?
-
sudo ss -tulpn -
ss -tan -
ss -s -
sudo ss -tu state established
Explanation:
-tTCP,-uUDP,-llistening only,-powning process (needs root for other users' processes),-nnumeric.ssreplaces the oldernetstat -tulpn.-tanshows all TCP sockets without process names, and-sis a summary.
- Port 5432 is a well-known port that needs root
- The service is listening on UDP instead of TCP
- The service is bound only to loopback, so it accepts local connections only
- The service is listening on every interface, which conflicts with routing
Explanation: A socket bound to
127.0.0.1is reachable only from the same machine. To accept remote clients the service must listen on0.0.0.0,[::]or a specific external address, and the firewall must allow the port.
-
sudo lsof /var/run/80 -
sudo ss -s :80 -
sudo netstat -r :80 -
sudo lsof -i :80
Explanation:
lsof -i :80lists open network files on port 80 with the command, PID and user.ss -sonly prints summary counts, andnetstat -rshows routes.
Q. With OpenBSD netcat (the default nc on Ubuntu), which command checks whether TCP port 443 is open on web01 without sending any data?
-
nc -zv web01 443 -
nc -l web01 443 -
nc -u web01 443 -
nc -p web01 443
Explanation:
-zonly checks for a listening daemon and-vreports the result, such asConnection to web01 443 port [tcp/https] succeeded!.-lmakesnclisten locally,-uswitches to UDP, and-psets the local source port.
- 1024–49151
- 0–1023
- 49152–65535
- 0–65535
Explanation: Ports 0–1023 are well-known (SSH 22, HTTP 80, HTTPS 443). 1024–49151 are registered ports and 49152–65535 are dynamic/private. TCP and UDP each have their own 0–65535 space.
- SMTP
- DHCP
- DNS
- SSH
Explanation: DNS uses UDP 53 for most queries and TCP 53 for large responses and zone transfers. DHCP uses UDP 67/68, SMTP uses TCP 25 and SSH uses TCP 22.
- The kernel assigns a free port from
net.ipv4.ip_local_port_range - The call fails, because port 0 is reserved
- The socket accepts connections on every port
- The socket gets port 1024, the first unprivileged port
Explanation: Port 0 means "pick one for me". The kernel chooses an unused ephemeral port, by default from 32768–60999, which differs from IANA's 49152–65535 range. Check it with
sysctl net.ipv4.ip_local_port_range.
- Nmap cannot tell whether the port is open because packets are being filtered
- A service is accepting connections on the port
- The host is reachable but nothing listens on the port
- The host is down
Explanation:
filteredmeans a firewall or filter dropped the probes, so Nmap got no clear answer.closedmeans the host replied but nothing listens there;openmeans a service accepted the connection.
- A SYN ("half-open") scan, the same as
-sS - A full TCP connect scan, the same as
-sT - A UDP scan of the top ports, the same as
-sU - Host discovery only, the same as
-sn
Explanation: A SYN scan crafts raw packets, which needs root, so unprivileged Nmap falls back to completing normal
connect()calls. With root, the default is a SYN scan.-snskips port scanning entirely.
Q. Which command captures 100 packets on eth0 and saves them to capture.pcap for later analysis in Wireshark?
-
sudo tcpdump -w eth0 -c 100 capture.pcap -
sudo tcpdump -r capture.pcap -i eth0 -c 100 -
sudo tcpdump -i eth0 -c 100 -w capture.pcap -
sudo tcpdump -i eth0 -c 100 > capture.pcap
Explanation:
-iselects the interface,-climits the packet count and-wwrites raw packets to a file.-rreads a capture, and redirecting stdout saves text output, not a pcap file.
-
sudo tcpdump -i eth0 host 192.168.1.20 or port 443 -
sudo tcpdump -i eth0 port 192.168.1.20:443 -
sudo tcpdump -i eth0 -c 443 host 192.168.1.20 -
sudo tcpdump -i eth0 host 192.168.1.20 and port 443
Explanation: Filter primitives combine with
and,orandnot. Usingorwould capture all traffic to that host plus all port 443 traffic.porttakes only a number, and-c 443limits the capture to 443 packets.
Q. Which command enables IPv4 forwarding at runtime, so the machine can route packets between networks?
-
sudo ip link set ip_forward on -
sudo sysctl -w net.ipv4.ip_forward=1 -
sudo iptables -A FORWARD -j ACCEPT -
sudo sysctl -p net.ipv4.ip_forward
Explanation:
sysctl -wchanges the kernel parameter immediately;cat /proc/sys/net/ipv4/ip_forwardthen shows1. A FORWARD rule only filters forwarded packets and does not turn forwarding on.
- Run
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forwardonce - Run
sudo sysctl -w net.ipv4.ip_forward=1once - Put
net.ipv4.ip_forward=1in a file under/etc/sysctl.d/and runsudo sysctl --system - Add
net.ipv4.ip_forward=1to/etc/environmentand log in again
Explanation: Writing to
/procor usingsysctl -wchanges only the running kernel. Files in/etc/sysctl.d/are applied at boot, andsysctl --systemloads them now. Ubuntu also reads/etc/sysctl.confthrough the99-sysctl.confsymlink.
- To resolve hostnames to IP addresses
- To assign IP addresses to devices automatically
- To encrypt traffic leaving the local network
- To let many devices with private addresses share one public IP address
Explanation: Private addresses are not routed on the internet, so the router rewrites their source address to its public IP and tracks connections to map replies back. DNS resolves names and DHCP assigns addresses.
Q. A firewall rule uses DROP instead of REJECT. What does a client trying to connect over TCP experience?
- No response, so the connection attempt hangs until it times out
- An immediate "connection refused" error
- A redirect to another port on the same server
- A successful connection with no data returned
Explanation:
DROPsilently discards the packet.REJECTsends an error back, so the client fails quickly with "connection refused" or "unreachable". In UFW,ufw denydrops andufw rejectrejects.
Q. The INPUT chain contains a LOG rule followed by a DROP rule, both matching TCP port 23. What happens to a packet for port 23?
- It is logged and then accepted, because
LOGends rule processing - It is logged, then processing continues and the
DROPrule discards it - It is dropped without being logged, because
DROPtakes priority - It is logged and sent back to the client as a rejection
Explanation:
LOGis a non-terminating target: it writes a kernel log entry and lets the packet continue to the next rule.ACCEPT,DROPandREJECTare terminating and stop evaluation. Rate-limitLOGrules to avoid flooding logs.
Q. The INPUT chain contains these rules, checked in this order. What happens to a new SSH connection on port 22?
1 DROP all -- 0.0.0.0/0 0.0.0.0/0
2 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
- It is accepted, because the more specific rule wins
- It is accepted, because ACCEPT rules override DROP rules
- It is dropped, because rule 1 matches first
- It falls through to the chain's default policy
Explanation: iptables evaluates rules top to bottom and stops at the first matching terminating rule. Rule 1 matches everything, so the SSH rule is never reached. Insert the ACCEPT above it with
iptables -I INPUT 1 ....
Q. A web server on this host listens on TCP port 80. Which iptables rule lets clients connect to it?
-
sudo iptables -A INPUT -p tcp --sport 80 -j ACCEPT -
sudo iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT -
sudo iptables -A FORWARD -p tcp --dport 80 -j ACCEPT -
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
Explanation: Traffic addressed to this host passes the INPUT chain, and a client's request has destination port 80.
--sport 80would match replies from remote web servers, OUTPUT covers traffic this host sends, and FORWARD handles traffic routed through it.
- The
iptablescommand translates rules into the kernel's nftables framework - The legacy iptables kernel tables are in use and nftables is disabled
-
iptablesrules are ignored, and onlynftrules take effect - The rules are stored by UFW in
/etc/nftables.conf
Explanation: Modern distributions ship
iptables-nft, which keeps the iptables syntax but programs nftables underneath. The same rules then appear insudo nft list ruleset. UFW and firewalld also sit on top of this framework.
Q. You are about to run sudo ufw default deny incoming and sudo ufw enable on a remote server over SSH. What should you do first?
- Run
sudo ufw disable - Run
sudo ufw allow ssh - Run
sudo ufw default deny outgoing - Run
sudo ufw delete allow ssh
Explanation: With incoming traffic denied by default, enabling UFW without an SSH rule can lock you out of the server. Allow SSH (or your custom SSH port) before enabling the firewall, and test a new session before closing the old one.
To Action From
-- ------ ----
[ 1] 22/tcp DENY IN Anywhere
[ 2] 22/tcp ALLOW IN 203.0.113.25
- Yes, because the rule naming a specific source is more specific
- Yes, because ALLOW rules are checked before DENY rules
- No, because UFW applies the first matching rule
- No, because UFW blocks port 22 unless
ufw limit sshis used
Explanation: UFW rules are evaluated in order and the first match wins, so rule 1 drops the connection. Put the specific rule first, for example
sudo ufw insert 1 allow from 203.0.113.25 to any port 22 proto tcp.
Q. After sudo ufw deny 631/tcp, sudo ss -tln still shows 0.0.0.0:631 in LISTEN state. What does this mean?
- The UFW rule failed, because the port is still open
- The firewall filters packets but does not stop the service from listening
- UFW rules apply only after the service is restarted
-
ssshows cached data until the next reboot
Explanation: A firewall rule decides which packets reach the socket; the program keeps listening. Test the block from another host (for example with
nc -zv), and stop or disable the service if it is not needed at all.
Q. You run sudo firewall-cmd --permanent --add-service=http, but HTTP is still blocked. What is missing?
-
sudo firewall-cmd --runtime-to-permanent -
sudo systemctl disable firewalld -
sudo firewall-cmd --add-port=http -
sudo firewall-cmd --reload
Explanation:
--permanentwrites the rule to the saved configuration but does not change the running firewall.--reloadloads the permanent configuration into the runtime.--runtime-to-permanentcopies in the opposite direction.
public (active)
services: ssh https
rich rules:
rule family="ipv4" source address="10.0.0.0/8" service name="https" accept
- Only clients in
10.0.0.0/8, because the rich rule narrows the service - Any client, because the
httpsservice entry already allows every source - No client, because the service entry and the rich rule conflict
- Only clients in
10.0.0.0/8, because rich rules override zone services
Explanation: Zone services and accept rich rules add permissions; neither restricts the other. To limit HTTPS to
10.0.0.0/8, remove the service with--remove-service=httpsand keep only the rich rule, then reload.