Skip to content
Tutorials Center
My learning

Topics

Lesson 53 of 95 ยท Processes and the System

Networking Basics from the Shell

Learn Linux networking from the shell: IP addresses, ports, loopback, /etc/hosts, DNS, ss, curl, ping, and a TCP client in plain Bash with /dev/tcp.

Almost every program you will run on a server talks to another program: a web server answers a browser, a script downloads a file, an app connects to a database. The Linux command line gives you a small toolbox to see what is listening, to test whether you can reach a machine, to look up names, and to speak simple protocols by hand. Learning that toolbox turns "the site is down" from a mystery into a series of questions you can answer in a minute.

In this lesson you will learn how addresses and ports identify a conversation, why localhost is special, how names become addresses (/etc/hosts and DNS), which commands inspect and test the network (ss, ip, ping, curl, nc, dig), and how to talk TCP using nothing but Bash and the pseudo-device /dev/tcp. Our sandbox has no network access and none of the usual network tools, so those commands are shown read-only; everything runnable happens on the loopback interface inside the sandbox, which is enough to see real connections. It builds on Pipes and Pipelines and Processes.

Addresses, ports and protocols

A network conversation between two programs is identified by four things:

TEXT
   client                                          server
   192.168.1.20 : 51734  ---------TCP---------->  93.184.216.34 : 443
   (address)    (port)                              (address)      (port)
  • An IP address names a network interface on a machine. IPv4 addresses are four numbers 0 to 255 (192.168.1.20), IPv6 addresses are eight groups of hexadecimal digits (2001:db8::1).
  • A port (0 to 65535) names a program on that machine. A server listens on a well-known port; the client picks a random high port for itself (the ephemeral port).
  • A protocol defines the rules of the conversation. TCP gives a reliable, ordered byte stream (web, SSH, databases). UDP sends independent datagrams without guarantees (DNS queries, video, games).

Ports below 1024 are privileged: on Linux only root may listen on them. Some famous ones are in /etc/services, which the getent command can query:

Bash Well-known ports from the system database

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

You need to know a short list by heart: 22 SSH, 53 DNS, 80 HTTP, 443 HTTPS, 25 and 587 SMTP, 3306 MySQL, 5432 PostgreSQL, 6379 Redis. Software that runs on a non-standard port, such as a development server on 3000 or 8080, has to be given the port explicitly in the URL (http://localhost:3000).

Working with IPv4 addresses in Bash

An IPv4 address is really one 32-bit number written as four bytes. Treating it as a number makes questions like "is this address inside that network?" simple arithmetic, a good way to understand CIDR notation: 10.0.0.0/24 means "the first 24 bits are the network, the last 8 bits are free for hosts".

Bash IPv4 addresses and CIDR networks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Three ranges are private (not routed on the public internet; your home router hands them out): 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Another is link-local (169.254.0.0/16, self-assigned when DHCP fails). And one is special for every machine, as you will see next.

Loopback and localhost

The address 127.0.0.1 (and the whole 127.0.0.0/8 block) is the loopback interface: traffic to it never leaves the machine, the kernel hands it straight back to a local program. The name localhost points to it (for IPv6 the address is ::1). Loopback is how you test a server you are developing without exposing it to anyone, and how programs on the same computer talk to each other (an app and its local database, for example).

A server chooses which addresses to accept connections on, by the address it binds to:

Bind address Meaning
127.0.0.1 Only programs on this machine can connect (safe default for development and databases)
0.0.0.0 All IPv4 interfaces: reachable from the network, if a firewall allows it
:: All IPv6 (and often IPv4) interfaces
192.168.1.20 Only through that one interface

A database or cache bound to 0.0.0.0 without a password or firewall rule is one of the most common ways servers get hacked. If a service only needs local clients, bind it to 127.0.0.1 and check the result with ss -tln as shown below.

How names become addresses

People use names; the network uses addresses. Turning one into the other is name resolution, and the system tries sources in an order configured in /etc/nsswitch.conf (the hosts: line, typically files dns):

  1. /etc/hosts, a plain text file with address name [aliases...] lines, checked first. Perfect for localhost and for quick local overrides during development.
  2. DNS, the distributed phone book. The resolver library asks the servers listed in /etc/resolv.conf, which may point to your router, a company resolver or a public one.
Bash The hosts file and getent hosts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

getent hosts goes through exactly the same lookup path as a normal program, so if it fails, so will your app. Our sandbox has no DNS, so a public name is reported as not found with status 2; on a connected machine you would see an address instead. Tools that talk to DNS directly show more detail, but they are not installed here, so the following is read-only:

CONSOLE
$ dig +short example.com                 # just the address(es)
93.184.216.34
$ dig example.com MX +short              # mail servers
$ dig @1.1.1.1 example.com               # ask a specific resolver
$ dig -x 93.184.216.34 +short            # reverse lookup
$ nslookup example.com                   # older, similar tool
$ host example.com                       # shortest output
$ resolvectl status                      # systemd-resolved: which DNS servers are used

A frequent diagnosis: "the site does not load" can mean DNS is broken (name does not resolve), the network is down (address unreachable), or the server is not listening (connection refused). The tools below separate these three cases.

The network toolbox

All of these are standard on servers, but need a real network and sometimes extra packages, so they are read-only in this lesson:

CONSOLE
# --- my own interfaces and addresses
$ ip addr                         # all interfaces and IP addresses (replaces ifconfig)
$ ip -brief addr                  # one short line per interface
$ ip route                        # the routing table; "default via ..." is your gateway

# --- who is listening / connected (replaces netstat)
$ ss -tln                         # TCP, listening, numeric ports
$ ss -tlnp                        # plus the owning process (root shows all)
$ ss -s                           # summary of sockets
$ ss -tn state established        # current TCP connections

# --- can I reach it?
$ ping -c 3 example.com           # ICMP echo: is the host alive, how long does it take
$ traceroute example.com          # the path (or mtr, for a live view)

# --- talk to a service
$ curl -I https://example.com     # headers only
$ curl -sS -o page.html https://example.com
$ curl -s https://api.example.com/items | head
$ curl -v http://localhost:8080/  # -v shows the whole conversation
$ wget https://example.com/file.tgz
$ nc -zv example.com 443          # is the port open? (nc = netcat)
$ nc -l 9000                      # listen on a port: a throw-away server

Reading ss -tln is a core skill. A line like LISTEN 0 4096 127.0.0.1:5432 0.0.0.0:* says "a program is waiting for TCP connections on port 5432, but only from this machine". The same port shown as 0.0.0.0:5432 or *:5432 is open to the world.

A few more idioms: curl -fsS URL fails with a non-zero exit status on HTTP errors (use it in scripts instead of plain curl), curl -L follows redirects, curl -X POST -d 'name=ann' URL sends data, and curl --max-time 5 stops waiting after five seconds.

ifconfig, netstat and route belong to the old net-tools package, which many modern distributions no longer install. ip and ss are their replacements and are the tools to learn first.

Talking TCP with Bash itself: /dev/tcp

Even when nc and curl are missing (this happens on minimal servers and containers), Bash can open a TCP connection. If you redirect to or from the pseudo-path /dev/tcp/HOST/PORT, Bash itself creates the connection. No such file exists on disk; it is a feature of the shell, available in the standard Debian and Ubuntu build of Bash.

We first try a port where nobody is listening:

Bash Connecting to a closed port (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The message is connect: Connection refused: the machine answered, but no program listens on that port. This is different from a timeout (no answer at all, usually a firewall or a wrong address) and from Name or service not known (DNS failed). Learning to tell these apart is half of network debugging.

To see a connection succeed we need a server. Bash cannot listen on a port, so for this demonstration only, we start the small web server that comes with Python as a stand-in. It is not part of what you are learning, it just gives us something to talk to. It binds to 127.0.0.1 and port 0, which means "pick any free port", so the example never clashes with anything.

Then we speak HTTP by hand. An HTTP/1.0 request is just lines of text ending with an empty line, and the response comes back as text:

Bash A hand-written HTTP request over /dev/tcp

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

What happened step by step:

  1. exec 3<> /dev/tcp/... makes Bash call connect() and attach the socket to file descriptor 3 for reading and writing (<>).
  2. printf ... >&3 sends the request bytes. HTTP lines end in CR LF (\r\n), and an empty line means "that is the whole request".
  3. cat <&3 reads the reply until the server closes the connection, which HTTP/1.0 does after every response.
  4. exec 3<&- closes the descriptor.

The first line of the response, HTTP/1.0 200 OK, carries the status code: 2xx success, 3xx redirect, 4xx client error (404 not found), 5xx server error. Then come headers, an empty line, and the body.

Building a port checker

A tiny function that answers "is something listening?" is very useful in deploy scripts. Wrap the connection in timeout, so that a black-holed address cannot hang the script (see Jobs and Background Processes):

Bash A reusable port_open function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The classic use of this retry loop: a deploy script starts the new version of a server and then waits until its port answers before it switches traffic. Remember that an open port only proves that a program accepts connections, not that the application works; a real health check requests a URL and tests the HTTP status.

What ss would show: reading /proc/net/tcp

The kernel keeps the list of sockets in plain text files under /proc/net, and tools like ss and netstat just format them. Reading the file shows what those tools really do, and works even when they are not installed. In /proc/net/tcp each line has the local address and port as hexadecimal numbers (the IPv4 address is written byte-reversed), and a state code; 0A means LISTEN.

Bash A tiny ss -tln built from /proc/net/tcp

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

You could do the same for the table of open connections (states other than 0A) or for UDP in /proc/net/udp. In daily work, use ss, but this exercise shows that the "network commands" are thin layers over information the kernel exposes as files, one more example of Linux's "everything is a file" idea (see The Linux Filesystem).

A debugging recipe: where is the problem?

When a connection to app.example.com:443 fails, go from the bottom of the stack to the top:

  1. Is my own network up? ip -brief addr shows an address; ip route shows a default gateway; ping -c 2 1.1.1.1 tests the internet by number.
  2. Does the name resolve? getent hosts app.example.com or dig +short app.example.com. No answer means DNS (or /etc/hosts) is the problem.
  3. Can I reach the host? ping -c 2 ADDRESS (many servers block ping, so a failure here is only a hint).
  4. Is the port open? nc -zv ADDRESS 443, or (exec 3<>/dev/tcp/ADDRESS/443) && echo open. "Connection refused" means the host answered but nothing listens; a long timeout means a firewall drops the packets.
  5. Does the application answer correctly? curl -v https://app.example.com/ shows the TLS handshake, the status code and the headers.
  6. On the server itself: ss -tlnp to see whether the program listens, on which address, and which process owns the port; the logs (Packages, Services and systemd) tell the rest.

Good habits

  • Bind to loopback by default and expose a service only when you must; confirm with ss -tln.
  • Use numbers when debugging: ss -n, ping 1.1.1.1, curl --resolve, so that DNS does not hide the real problem.
  • Set timeouts on everything that touches the network: curl --max-time, timeout, ssh -o ConnectTimeout=5. A script without timeouts will eventually hang forever.
  • Never pipe a download into a shell (curl ... | bash) without reading it; download first, inspect, then run. See SSH, scp and Working Remotely for the secure way to reach other machines.
  • Do not paste secrets into command lines: they stay in the shell history and in ps output. Read tokens from files or environment variables.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does 127.0.0.1 mean?

    127.0.0.1 (the 127.0.0.0/8 block) is the loopback interface. Packets sent to it never leave the computer, which makes it ideal for testing servers and for local-only services.
  2. 2. A connection attempt to a port ends immediately with Connection refused. What does it tell you?

    Refused means the target machine replied that nothing accepts connections there. A firewall that drops packets usually produces a long timeout instead, and DNS problems give a name-resolution error.
  3. 3. Which line shows a service that only local programs can reach?

    A service bound to 127.0.0.1 accepts connections only from the same machine. 0.0.0.0, * and [::] mean all interfaces.
  4. 4. How can Bash open a TCP connection to port 80 of a host when nc and curl are not installed?

    Bash treats /dev/tcp/HOST/PORT specially in redirections: it connects a socket and connects it to the chosen file descriptor. The path does not exist as a real file.
  5. 5. Which source does the system resolver consult first by default, before asking DNS?

    The hosts: line in /etc/nsswitch.conf is usually 'files dns', so /etc/hosts is read first. resolv.conf only lists the DNS servers that are asked afterwards.

Summary

  • A connection is identified by addresses and ports; servers listen on well-known ports, clients use ephemeral ones. getent services and /etc/services list the standard names.
  • 127.0.0.1 / localhost is loopback; services should bind to it unless they must be reachable from outside. 0.0.0.0 means all interfaces.
  • Names resolve through /etc/hosts first and then DNS; getent hosts NAME follows the same path as your programs.
  • Use ip for addresses and routes, ss -tln for listeners, ping, nc, curl and dig for reachability, services and DNS. They are read-only in our sandbox because it has no network.
  • Connection refused means nothing listens; a timeout points to a firewall or wrong address; a resolution error points to DNS.
  • Bash can open sockets itself with exec 3<> /dev/tcp/HOST/PORT, which is enough for a port check or a hand-written HTTP request. Always add timeouts.
  • Tools like ss only format information from /proc/net.

Next: Disks, Mounts, Logs and Monitoring