Please enable JavaScript to use CodeHS

Cybersecurity AP Level

Network Commands for Security

When a site will not load, three things could be broken: your machine, your network, or the server. A defender does not guess. Eight commands typed into a terminal answer the question in about a minute, and the same commands reveal when something on your machine is talking to the internet that should not be.

12 min read AP & High School
01

How a Request Actually Travels

Where this fits

This is the security-focused follow-on to Network Management, which introduces what a network is and covers ping, traceroute, and nslookup at a beginner level. Here we go further: reading what those results actually mean, auditing the ports your own machine is listening on, and hunting down a connection that should not be there. If networks are brand new to you, start there first.

Before any command makes sense, you need a picture of what happens when you type an address into a browser and press Enter. It feels instant. It is not. Your request takes a specific route through specific machines, and every command in this tutorial inspects one stop on that route.

First, two words you will need on every line below.

A packet is a small chunk of data with an address label on the front. Networks do not send a whole web page in one piece. They chop it into hundreds of packets, send each one separately, and reassemble them at the far end. Think of mailing a book one page at a time in numbered envelopes. If a few envelopes get lost, you notice a gap, and the sender mails those pages again.

A router is a machine whose only job is to look at a packet's address label and pass it one step closer to its destination. The box in your house is a router. So are the far larger machines your internet provider owns. No single router knows the whole path. Each one only knows the next step, which is why the internet keeps working when pieces of it break.

Here is the trip, stop by stop.

  1. 1

    Your machine

    Your laptop builds the request and stamps it with its own address on the local network, something like 192.168.1.24. If the destination is not on the local network, it hands the packet to the router.

  2. 2

    Your router, also called the gateway

    The gateway is the doorway between your local network and everything else. Every packet leaving your house goes through it. If you cannot reach your own gateway, nothing else on this list matters yet.

  3. 3

    A DNS lookup, which happens first

    Your machine does not know what library.example means. It asks a DNS server, which is the internet's phone book, to turn that name into a number it can actually route to. Only after the name resolves does the real request leave.

  4. 4

    Your ISP and the wider internet

    Your internet service provider takes the packet from your router and hands it router to router across the internet. This is usually the longest stretch of the trip and the part you have the least control over.

  5. 5

    The destination server

    The server receives the request, builds a response, and sends it back along a similar path. Then your browser draws the page.

Five stops. When something breaks, it breaks at one of them, and the whole skill of network diagnosis is walking the list in order until you find the stop where the trip ends.

Why this matters for security

Every one of these stops is a place an attacker would love to sit. Someone controlling your DNS can send you to a fake login page while the address bar still looks right. Someone on your local network can watch unencrypted traffic. Knowing the route is what lets you check the route.

02

Addresses, MACs, and Ports

Networks use three different kinds of labels, and people mix them up constantly. Each one answers a different question.

The IP address: which machine, on which network

An IP address (Internet Protocol address) identifies a machine on a network. The version you will see most often is IPv4, written as four numbers separated by dots. Each number is called an octet and can be anything from 0 to 255, because each one is stored in exactly 8 bits.

192 Octet 1
168 Octet 2
1 Octet 3
24 Octet 4

Four octets, 8 bits each, 32 bits total. That is roughly 4.3 billion possible addresses, which turned out not to be nearly enough. IPv6 exists to fix that, and writes addresses in hexadecimal instead.

Some addresses are public, meaning they are unique across the entire internet and anyone can route to them. Others are private, meaning they are reserved for use inside a home, school, or office network. Private addresses are reused everywhere, so millions of machines share the address 192.168.1.24 at this moment. That is fine, because packets addressed to a private range never travel across the open internet. Your router translates them on the way out.

Range Kind Where you see it
10.0.0.0 to 10.255.255.255 Private Large office, school, and campus networks. Lots of room.
172.16.0.0 to 172.31.255.255 Private Common in corporate networks and in Docker containers.
192.168.0.0 to 192.168.255.255 Private Almost every home router hands out addresses from here.
127.0.0.0 to 127.255.255.255 Loopback This machine, and only this machine. See below.
Everything else Public Real destinations on the open internet.

The address 127.0.0.1 deserves its own paragraph. It is called localhost, or the loopback address, and it always means "the machine I am typing on." A packet sent to 127.0.0.1 never touches a cable or a radio. It goes down the network stack and comes straight back up. That makes it the perfect first test: if you cannot ping localhost, the problem is not the network at all, it is the networking software on your own machine.

The MAC address: which physical network card

A MAC address (Media Access Control address) identifies a specific piece of network hardware, like the Wi-Fi chip or Ethernet port inside your laptop. It is written as six pairs of hexadecimal digits, for example 52:54:00:9d:1a:7c. It is burned in at the factory and, unlike an IP address, it normally does not change when you move to a different network.

The difference is worth holding onto: an IP address is where you are, and a MAC address is who you are. Your IP changes when you carry your laptop from home to school. Your MAC generally does not. That is exactly why phones now randomize their MAC address per network, because a fixed hardware ID is a very convenient way to track a person from place to place.

Ports: which program on that machine

An IP address gets a packet to the right machine. A port gets it to the right program on that machine. A port is just a number from 0 to 65535 written after the address, so 192.0.2.10:443 means "the program listening on port 443 at 192.0.2.10."

If the IP address is a street address for an apartment building, the port is the apartment number. One server machine can run a web server, a mail server, and a remote login service at the same time because each one waits on a different port. A handful of port numbers are worth memorizing.

22

SSH

Encrypted remote login to another machine's command line.

53

DNS

Name lookups. Turns library.example into a number.

80

HTTP

Web traffic with no encryption. Anyone in between can read it.

443

HTTPS

Web traffic wrapped in TLS encryption. The one you want.

A program that is waiting for connections on a port is said to be listening. Knowing which ports are listening on a machine is one of the most useful things a defender can know, and we get to that in section 06.

Your laptop shows the address 192.168.1.24. What does that tell you?

Anything starting with 192.168 is private. Millions of home networks hand out that exact address at the same moment, and it is your router that translates it to a public address on the way out. Loopback is 127.0.0.1, and MAC addresses use hexadecimal pairs separated by colons.

03

Find Your Own Address

Every investigation starts with the same question: where am I? Two commands answer it.

On modern Linux, the command is ip addr (often shortened to ip a). On macOS and older Linux systems, it is ifconfig. On Windows, the equivalent is ipconfig. They all print the same basic facts: the network interfaces your machine has, and the addresses attached to each one.

An interface is one network connection on your machine. A laptop typically has at least two: lo, the loopback interface that only talks to itself, and something like eth0 or wlan0 for the real Ethernet or Wi-Fi connection.

student@lab-pc
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 state UNKNOWN
    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 state UP
    link/ether 52:54:00:9d:1a:7c brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.24/24 brd 192.168.1.255 scope global eth0

Three things to pull out of that output.

  • Your IP address is 192.168.1.24. It starts with 192.168, so you are on a private network behind a router.
  • The /24 is the subnet mask. It means the first 24 bits (the first three octets) identify the network and the last 8 bits identify the machine. In plain terms: everything from 192.168.1.1 to 192.168.1.254 is a neighbor you can reach directly, and everything else has to go through the router.
  • Your MAC address is 52:54:00:9d:1a:7c. That is the link/ether line, the hardware ID of the network card itself.

If eth0 shows state DOWN, or shows no inet line at all, you have found your problem without touching the rest of the internet. The interface is off, the cable is out, or nothing handed you an address.

Guess the gateway

If your address is 192.168.1.24 with a /24 mask, your router is almost always at .1 or .254 on that same network, so 192.168.1.1 is the first thing to try. On Linux you can confirm it exactly with ip route, which prints a line beginning with "default via".

04

Is It Reachable? Ping and Traceroute

ping: is anything home?

The ping command sends a few tiny packets to an address and waits for each one to bounce back. It is the network equivalent of shouting across a canyon and listening for the echo. Two numbers come back, and both mean something specific.

student@lab-pc
$ ping -c 4 library.example
PING library.example (192.0.2.10) 56(84) bytes of data.
64 bytes from 192.0.2.10: icmp_seq=1 ttl=60 time=18.4 ms
64 bytes from 192.0.2.10: icmp_seq=2 ttl=60 time=18.1 ms
64 bytes from 192.0.2.10: icmp_seq=3 ttl=60 time=19.0 ms
64 bytes from 192.0.2.10: icmp_seq=4 ttl=60 time=18.3 ms
 
--- library.example ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms

Round-trip time is the "time=" number: how many milliseconds a packet took to get there and back. Under 30 ms is a healthy trip across a country. Over 300 ms and video calls start to feel broken. A number that swings wildly from packet to packet, say 20 ms then 340 ms then 25 ms, is called jitter, and it usually points at a congested or overloaded link rather than a dead one.

Packet loss is the percentage of packets that never came back. Zero percent is what you want. A steady 100 percent means the host is unreachable, powered off, or is deliberately ignoring you. The genuinely nasty case is the middle: 10 or 20 percent loss. The site technically loads, but slowly and unpredictably, and people report it as "the internet is being weird" rather than as an outage.

No reply does not always mean down

Plenty of well-run servers are configured to silently drop ping packets, because responding to strangers gives away that the machine exists. So a failed ping is evidence, not proof. Confirm with something else, like curl against the actual service port, before you declare a server dead.

traceroute: where exactly does the trip end?

Ping tells you whether the far end answered. It does not tell you where a failed packet died. That is what traceroute is for (it is spelled tracert on Windows).

Traceroute uses a clever trick. Every packet carries a counter called TTL, short for time to live, which drops by one at each router it passes through. When the counter hits zero, the router that zeroed it throws the packet away and mails back an error saying so. Traceroute sends a packet with TTL 1, then TTL 2, then TTL 3, and collects the complaint from each router in turn. The result is an ordered list of every machine between you and the destination.

student@lab-pc
$ traceroute library.example
traceroute to library.example (192.0.2.10), 30 hops max
 1  gateway.lab (192.168.1.1)  1.204 ms
 2  isp-edge-1.isp.example (10.64.0.1)  8.512 ms
 3  isp-core-3.isp.example (198.51.100.1)  14.233 ms
 4  edge-a.example (192.0.2.1)  17.008 ms
 5  library.example (192.0.2.10)  18.412 ms

Read it as a story. Hop 1 is your own router, so your local network is fine. Hop 2 is the first machine your ISP owns, so your connection to the outside world is up. Hops 3 and 4 are the internet in between. Hop 5 is the destination answering, so the whole path works.

Now read a broken one. If hops 1 through 4 answer and then hops 5, 6, and 7 print nothing but * * *, the last machine that answered is your best clue. The trip made it as far as hop 4 and died after that, which means the failure is at or beyond the destination network, not on your desk. That single distinction saves hours: it is the difference between "restart my Wi-Fi" and "the service is down, wait for their status page."

One more thing to notice, and it is why the simulator below is worth playing with: two destinations on the same network share the same early hops. If library.example and dns.example both route through hops 1 through 4 and only differ at the last one, they live behind the same edge router. When both go down at once, you have learned something about where the problem is.

Stars are normal in the middle

A few asterisks partway through a trace usually just mean one router declines to send TTL error messages. That is a configuration choice, not a fault. It only matters when the stars start and never stop.

05

Turning Names into Numbers: DNS

DNS stands for Domain Name System, and it is the internet's phone book. Humans remember names. Routers only understand numbers. DNS is the service that converts one into the other, and it runs on port 53.

Two commands look up a name by hand. nslookup is short and available almost everywhere, including Windows. dig is more detailed and is what most people who do this for a living reach for.

student@lab-pc
$ nslookup library.example
Server:  192.168.1.1
Address: 192.168.1.1#53
 
Non-authoritative answer:
Name:    library.example
Address: 192.0.2.10

Read the top two lines before the answer. They tell you which DNS server answered you. That is the security-relevant part, and most people skip right past it.

"Non-authoritative answer" means the reply came from a cache rather than from the server that officially owns that name. That is normal and it is what makes the web fast, but it also means the answer you got is only as trustworthy as the machine that cached it.

Why DNS is a chokepoint worth guarding

Think about what a DNS server actually does: it decides which number your machine connects to when you type a name. If an attacker controls that answer, your browser will happily go to their server while the address bar still shows the name you typed. The connection is not hijacked in transit, it is aimed at the wrong place from the very first packet. This is called DNS spoofing or DNS poisoning, and it is a favorite because it is quiet.

So when you look up a name and get an answer that surprises you, ask three questions in this order.

  • Which server answered? If your machine is supposed to use your school's DNS server and it is answering from an address nobody recognizes, that alone is worth escalating.
  • Does the address make sense? A well-known site suddenly resolving to an address in a range you have never seen is a red flag.
  • Does a second source agree? Asking a different resolver and getting a different number for the same name is a strong signal something is wrong.

DNS failures also explain a very common confusing symptom. If ping library.example fails with "Name or service not known" but ping 192.0.2.10 works fine, your network is healthy and only your name lookups are broken. That is a DNS problem, not an internet problem, and it needs a completely different fix.

Pinging shop.example returns "Name or service not known," but pinging 198.51.100.42 gets replies with 0% packet loss. What is broken?

Packets reach the numeric address and come back cleanly, so your machine, your router, your ISP, and the destination server all work. The only step that failed was turning the name into a number. Check which DNS server your machine is using and whether it is answering at all.

06

What Is My Machine Talking To?

Everything so far pointed outward. This section points at your own machine, and it is the part that turns these commands from IT troubleshooting into security work.

Two commands list network activity on the local machine. netstat is the classic one and exists on almost every operating system. ss (socket statistics) is its faster modern replacement on Linux. The flags mean the same thing on both.

Flag What it does
-t Show TCP, the connection-based protocol used by web, email, and SSH.
-u Show UDP, the fire-and-forget protocol used by DNS, video, and games.
-l Show only sockets that are listening, meaning waiting for someone to connect.
-n Print numbers instead of names, so you see 443 rather than "https". Faster and more honest.
-p Also show which program owns each socket. Usually needs administrator rights.

Put them together and you get the single most useful command in this tutorial: netstat -tuln, or ss -tuln.

$ netstat -tuln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:4444            0.0.0.0:*               LISTEN
udp        0      0 127.0.0.53:53           0.0.0.0:*
udp        0      0 0.0.0.0:5353            0.0.0.0:*

Read the Local Address column carefully, because the part before the colon matters as much as the port.

  • 127.0.0.1:631 is listening on loopback only. Nothing outside this machine can reach it. That is the safe configuration.
  • 0.0.0.0:22 means "listening on every interface." Anyone who can route to this machine can knock on port 22. Sometimes that is exactly what you want. Sometimes it is a mistake nobody noticed.

Now the security question, and it is a simple one: for every listening port, can you name the program and say why it should be there? Port 22 is SSH, port 80 is a web server, port 53 on loopback is the local DNS resolver, port 5353 is service discovery on the local network. Fine, fine, fine, fine.

Port 4444 is not fine. It is not a standard service, nothing on a normal desktop listens there, and it is listening on every interface. That is the shape of a backdoor, a program someone installed so they can connect back into the machine later. Add -p to find out what it is, and drop the -l to see whether it is currently connected to anything.

curl: ask the server directly

curl makes a real HTTP request from the command line, with no browser in the way. Used with -I (a capital i, for "headers only") it fetches just the response headers, which is often all you need.

$ curl -I https://library.example
HTTP/2 200
server: nginx/1.24.0
content-type: text/html; charset=UTF-8
strict-transport-security: max-age=31536000

That first line is the one that answers your question. 200 means the server received the request and answered normally, so the service itself is alive even if the page looks wrong in your browser. A 301 or 302 means you are being redirected somewhere, and the location header tells you where, which is a useful thing to check before you click a suspicious link. 403 means the server knows who you are and is refusing. 500 means the server is broken on its own end, which is very much not your problem to fix.

If curl cannot connect at all, the error text names the failing step for you: "Could not resolve host" is DNS, "Connection refused" means the machine answered but nothing is listening on that port, and "Connection timed out" means nothing answered at all.

Only run these against machines you own or are authorized to test

Everything in this tutorial is defensive: you are diagnosing your own machine and your own network. Pointing scanning or probing tools at networks, servers, or devices you do not own and do not have written permission to test is unauthorized access, and in the United States and many other countries it is a crime, not a prank. If you want to practice against a live target, use the simulator on this page, a lab your teacher set up, or a machine you built yourself. The addresses used throughout this tutorial (192.0.2.x, 198.51.100.x, 203.0.113.x) are reserved by the standards bodies for documentation and belong to nobody.

07

Try It: The Network Toolkit

Below is a simulated Linux machine called lab-pc, sitting on a small fictional network. Every command from this tutorial works, the output is consistent (the same host really does sit behind the same routers every time), and one of the hosts really is down.

The map lights up as you go, so you can see which part of the trip each command inspects. Start with help, or click one of the examples.

Interactive: Network Command Simulator

Request path map

Request path map A request leaves your machine at 192.168.1.24, passes through the router at 192.168.1.1, crosses the ISP network at 10.64.0.1, and arrives at the web server at 192.0.2.10. A DNS server at 192.0.2.53 branches off the router, because the name lookup happens before the request is sent. Each stop is labelled with the command that inspects it. ip addr Your machine 192.168.1.24 ping 192.168.1.1 Router (gateway) 192.168.1.1 traceroute ISP network 10.64.0.1 curl -I Web server 192.0.2.10 DNS server 192.0.2.53 nslookup / dig
student@lab-pc: ~

Up and Down arrows walk back through your command history. Type help for the full command list, or clear to wipe the screen.

Guided investigation: something is listening that should not be

A teacher hands you this machine and says only: "It feels slow, and I did not install anything." Your job is to find out whether something on it is talking to the internet without permission. Work through these in the simulator above.

  1. Run netstat -tuln and list every listening port. For each one, name the service it should be. One of them will not have an answer.

  2. You cannot accuse a port number. Run ss -tulnp to attach a program name and a process ID to the port you could not explain.

  3. Listening is suspicious. Connected is worse. Run netstat -tan to drop the "listening only" filter and see whether that program currently has a live connection, and to what address.

  4. Run nslookup on the remote address it is connected to. Does the name mean anything? Would you expect this machine to talk to it?

  5. Finally, run traceroute to that same address. Is the traffic staying inside the school network, or is it leaving for the open internet?

The odd port is 4444/tcp, listening on 0.0.0.0. Ports 22, 80, 53, 631, and 5353 all map to services a normal machine runs. Port 4444 is not a standard service, and listening on 0.0.0.0 means it accepts connections from any interface, not just from this machine.

The program is sysupd8, process ID 4812. The name is built to be skimmed past. It looks like "system update" if you are not reading carefully, but no real system service is called that.

It has a live outbound connection to 203.0.113.77 on port 9001. That is the part that turns a suspicion into a finding. The program is not just waiting, it is currently talking to something.

That address resolves to sync-node-7.example, and traceroute shows five hops leaving the network entirely. The traffic is going out through your router, through your ISP, and across the internet. A legitimate local service would never need to do that on an unregistered port.

What a defender does next: do not just kill the process. Write down what you saw first, because the process ID, the port, the remote address, and the timestamps are the evidence. Then disconnect the machine from the network and report it to whoever runs the network. Turning it off destroys the very information the next person needs.

08

The Troubleshooting Ladder

Here is the whole tutorial compressed into a procedure. Every rung tests exactly one stop on the journey from section 01, in order, from closest to you to furthest away. Climb until something fails. The rung that fails is where the problem is, and everything above it is already proven healthy.

The order is the point. Most people start at the top by reloading the page and blaming the website, which is the last rung, so they learn nothing when it does not work.

  1. 1

    Is my own networking software alive?

    Ping yourself. If loopback fails, nothing else you try will mean anything, because the problem is on this machine before any cable is involved.

    ping 127.0.0.1
  2. 2

    Do I have an address at all?

    Check that your interface is up and holds a real IP address. No address means nothing handed you one, so start with the cable, the Wi-Fi, or the router.

    ip addr
  3. 3

    Can I reach my own router?

    This is the boundary between "my problem" and "somebody else's problem." If the gateway does not answer, the failure is inside your building.

    ping 192.168.1.1
  4. 4

    Can I reach the internet by number?

    Ping a known outside address directly, skipping names entirely. Success here proves routing works and quietly rules out DNS as the cause.

    ping 192.0.2.10
  5. 5

    Can I reach the internet by name?

    If rung 4 worked and this one does not, you have isolated the fault to name resolution. Note which server answered, because that is a security question as well as a connectivity one.

    nslookup library.example
  6. 6

    Where along the path does it die?

    Trace the route and find the last machine that answered. That hop tells you whether the fault is near you, inside your ISP, or out at the destination network.

    traceroute library.example
  7. 7

    Is the actual service responding?

    The machine can be perfectly reachable while the web server on it is broken. Ask the service itself and read the status code it returns.

    curl -I https://library.example
  8. 8

    And the security question, asked routinely

    Separate from any outage: what is listening on this machine, and can I justify every entry? Run it when things are fine, so you know what normal looks like when they are not.

    ss -tulnp

That last rung is the habit that matters most. Troubleshooting is reactive, and you only do it when something is already broken. Knowing your own machine's normal set of listening ports is proactive, and it is the only way you will ever notice the day an extra one appears.

None of these commands are exotic. They ship with the operating system, they run in a second, and they turn "the internet is broken" into a specific, answerable question. That is the entire job.

Go Deeper Than One Tutorial

CodeHS cybersecurity courses take these commands further, into packet analysis, hardening, and defending a real network, with autograded exercises and teacher-ready lesson plans.