<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Linux and the command line 101 · Network Doodles</title><link>https://networkdoodles.com/lessons/linux-cli-101/</link><description>The Linux command line from zero, for network engineers: the shell and paths, files, pipes and grep, permissions and sudo, processes and PATH, SSH keys, a little Bash, and the network commands. Eight lessons, each with a hands-on lab chapter.</description><language>en-us</language><managingEditor>David Henderson</managingEditor><lastBuildDate>Mon, 16 Nov 2026 09:00:00 -0500</lastBuildDate><atom:link href="https://networkdoodles.com/lessons/linux-cli-101/index.xml" rel="self" type="application/rss+xml"/><item><title>The Network from the Command Line: ip route get, ss, curl -v, dig and tcpdump (Linux CLI 101, Lesson 8)</title><link>https://networkdoodles.com/lessons/linux-cli-101/network-commands/</link><pubDate>Mon, 16 Nov 2026 09:00:00 -0500</pubDate><dc:creator>David Henderson</dc:creator><guid>https://networkdoodles.com/lessons/linux-cli-101/network-commands/</guid><category>linux</category><category>networking-basics</category><category>troubleshooting</category><category>packet-capture</category><category>tcp</category><description>Troubleshoot from a Linux host: ip route get, dig, traceroute, what ss says about listening addresses, curl -v from DNS to HTTP, and packets with tcpdump.</description><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<p>When a web service works on the server but not from another host, work through the path in order. Does the
name resolve to the right address (<code>dig</code>)? Which way will the packet go (<code>ip route get</code>)? Is anything on the
way answering (<code>ping</code>, <code>traceroute</code>)? Is the service listening <strong>on an address the other host can reach</strong>
(<code>ss -tlnp</code>)? Then let <code>curl -v</code> show where the conversation stops, and <code>tcpdump</code> show what&rsquo;s on the wire.</p>
<p>A common answer to &ldquo;works locally, not remotely&rdquo; is the listening address. A program listening on
<code>127.0.0.1</code> only accepts connections from its own machine. <code>0.0.0.0</code> means every address the machine has.</p>
<p>You met most of these commands in the <a href="/labs/networking-101-lab/task-3-3/">Networking 101 lab</a>. This lesson is
about the options and output that matter when you&rsquo;re troubleshooting. Everything below ran on the lab&rsquo;s <code>ws1</code>
(Debian 13: iproute2 6.15.0, iputils 20240905, traceroute 2.1.6, curl 8.14.1, DiG 9.20.30, tcpdump 4.99.5),
except the TLS example, which ran in the codespace.</p>
<hr>
<h2 id="how-do-i-see-a-hosts-addresses-at-a-glance">How do I see a host&rsquo;s addresses at a glance?</h2>
<p><code>ip addr</code> prints a screenful per interface. <code>-br</code> (<em>brief</em>) prints one line each, and <code>-4</code> drops IPv6:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ip -br -4 addr
</span></span><span class="line"><span class="cl">lo               UNKNOWN        127.0.0.1/8
</span></span><span class="line"><span class="cl">eth0@if76        UP             172.80.102.11/24
</span></span><span class="line"><span class="cl">eth1@if80        UP             192.168.10.10/24
</span></span></code></pre></div><p>Name, state, addresses. On ws1, <code>eth0</code> is the lab&rsquo;s management network (the way the codespace reaches it), and
<code>eth1</code> is its cable to the router, r1. <code>lo</code> is the <strong>loopback</strong> interface: a virtual interface that only ever
talks to the machine itself, with <code>127.0.0.1</code> on it. The <code>@if76</code> part says the interface is one end of a
virtual cable whose other end is interface 76 somewhere else, which is how containers are wired. Without
<code>-4</code>, <code>ip -br addr</code> also lists half a dozen tunnel interfaces that are <code>DOWN</code> and can be ignored.</p>
<hr>
<h2 id="which-way-will-a-packet-go">Which way will a packet go?</h2>
<p><code>ip route</code> shows the routing table. <code>ip route get</code> answers the question you actually have: <em>for this one
address, which route wins?</em> It asks the kernel&rsquo;s routing decision directly and sends nothing
(<a href="https://man7.org/linux/man-pages/man8/ip-route.8.html">ip-route(8)</a>).</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ip route get 172.80.102.12
</span></span><span class="line"><span class="cl">172.80.102.12 dev eth0 src 172.80.102.11 uid 1000
</span></span><span class="line"><span class="cl">    cache
</span></span><span class="line"><span class="cl">$ ip route get 8.8.8.8
</span></span><span class="line"><span class="cl">8.8.8.8 via 192.168.10.1 dev eth1 src 192.168.10.10 uid 1000
</span></span><span class="line"><span class="cl">    cache
</span></span></code></pre></div><table>
  <thead>
      <tr>
          <th>Word</th>
          <th>Means</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>via 192.168.10.1</code></td>
          <td>send it to this router. No <code>via</code> means the address is on a directly connected network</td>
      </tr>
      <tr>
          <td><code>dev eth1</code></td>
          <td>out of this interface</td>
      </tr>
      <tr>
          <td><code>src 192.168.10.10</code></td>
          <td>with this as the source address</td>
      </tr>
      <tr>
          <td><code>uid 1000</code></td>
          <td>the user who asked. Ignore it</td>
      </tr>
  </tbody>
</table>
<p>The first address is on the management network, so it goes straight out of <code>eth0</code>. The second matches only the
default route, so it goes to r1. On a host with several interfaces, this is the quickest way to find out
whether replies leave by the interface you expect. The <code>src</code> field matters too: it&rsquo;s the address the far end
sees, and the address its firewall rules have to allow.</p>
<hr>
<h2 id="which-ping-and-traceroute-options-matter">Which ping and traceroute options matter?</h2>
<p>Linux <code>ping</code> runs until you press Ctrl-C. In anything you&rsquo;ll script or paste, give it a count:</p>
<table>
  <thead>
      <tr>
          <th>Option</th>
          <th>Does</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>ping -c 3</code></td>
          <td>send 3 and stop, with a summary</td>
      </tr>
      <tr>
          <td><code>ping -W 1</code></td>
          <td>wait at most 1 second for each reply</td>
      </tr>
      <tr>
          <td><code>ping -n</code></td>
          <td>numbers only, no reverse lookups</td>
      </tr>
      <tr>
          <td><code>traceroute -n</code></td>
          <td>numbers only, no names</td>
      </tr>
      <tr>
          <td><code>traceroute -q 1</code></td>
          <td>one probe per hop instead of three</td>
      </tr>
      <tr>
          <td><code>traceroute -w 1</code></td>
          <td>wait at most 1 second per probe</td>
      </tr>
      <tr>
          <td><code>traceroute -m 5</code></td>
          <td>give up after 5 hops instead of 30</td>
      </tr>
  </tbody>
</table>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ping -c 3 -n srv1.lab
</span></span><span class="line"><span class="cl">PING srv1.lab (192.168.20.10) 56(84) bytes of data.
</span></span><span class="line"><span class="cl">64 bytes from 192.168.20.10: icmp_seq=1 ttl=63 time=0.074 ms
</span></span><span class="line"><span class="cl">64 bytes from 192.168.20.10: icmp_seq=2 ttl=63 time=0.252 ms
</span></span><span class="line"><span class="cl">64 bytes from 192.168.20.10: icmp_seq=3 ttl=63 time=0.075 ms
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">--- srv1.lab ping statistics ---
</span></span><span class="line"><span class="cl">3 packets transmitted, 3 received, 0% packet loss, time 2041ms
</span></span><span class="line"><span class="cl">rtt min/avg/max/mdev = 0.074/0.133/0.252/0.083 ms
</span></span></code></pre></div><p><code>ttl=63</code> is a free hop count. Linux sends with a TTL of 64, and each router on the way takes one off, so 63
means one router in between. The exit code also matters here: <code>ping</code> exits 0 if replies came back and 1 if none
did, which is what <a href="/lessons/linux-cli-101/bash-scripting-basics/">lesson 7</a>&rsquo;s scripts read.</p>
<p>Without <code>-n</code>, traceroute looks up a name for every hop. In the lab that&rsquo;s quick, because srv1&rsquo;s DNS server
knows the names. On a real network, a reverse lookup that nobody answers can add seconds per hop:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ traceroute 192.168.20.10
</span></span><span class="line"><span class="cl">traceroute to 192.168.20.10 (192.168.20.10), 30 hops max, 60 byte packets
</span></span><span class="line"><span class="cl"> 1  r1.lab (192.168.10.1)  1.073 ms  0.124 ms  0.057 ms
</span></span><span class="line"><span class="cl"> 2  srv1.lab (192.168.20.10)  4.811 ms  0.082 ms  0.045 ms
</span></span></code></pre></div><p>Stars don&rsquo;t always mean a fault. Running <code>traceroute -n</code> straight after that one gave this:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">traceroute to 192.168.20.10 (192.168.20.10), 30 hops max, 60 byte packets
</span></span><span class="line"><span class="cl"> 1  192.168.10.1  0.799 ms  0.035 ms  0.031 ms
</span></span><span class="line"><span class="cl"> 2  * * *
</span></span><span class="line"><span class="cl"> 3  * * *
</span></span><span class="line"><span class="cl"> 4  * * *
</span></span><span class="line"><span class="cl"> 5  * * *
</span></span><span class="line"><span class="cl"> 6  * * *
</span></span><span class="line"><span class="cl"> 7  * 192.168.20.10  0.461 ms  0.107 ms
</span></span></code></pre></div><p>Same two-hop path, a few seconds later, run again: back to two lines. Linux traceroute sends many probes at
once, and Linux limits how fast it sends the ICMP errors traceroute depends on (<code>net.ipv4.icmp_ratelimit</code>,
1000 ms on these hosts), so a burst of probes can get only some answers. If a count looks wrong, wait a moment
and run it again with <code>-q 1</code>.</p>
<hr>
<h2 id="what-does-a-listening-address-mean">What does a listening address mean?</h2>
<p>A server program <strong>listens</strong> on a port: it asks the operating system for every new connection to that port. It
also picks <strong>which of the machine&rsquo;s addresses</strong> to listen on. <code>ss -tlnp</code> shows both: TCP (<code>t</code>), listening
(<code>l</code>), numbers (<code>n</code>), and the process (<code>p</code>) that owns each socket
(<a href="https://man7.org/linux/man-pages/man8/ss.8.html">ss(8)</a>).</p>
<p>To see it, I started Python&rsquo;s built-in web server on ws1, listening only on loopback:
<code>python3 -m http.server --bind 127.0.0.1 9999</code>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ss -tln
</span></span><span class="line"><span class="cl">State  Recv-Q Send-Q Local Address:Port  Peer Address:Port
</span></span><span class="line"><span class="cl">LISTEN 0      5          127.0.0.1:9999       0.0.0.0:*
</span></span><span class="line"><span class="cl">LISTEN 0      128          0.0.0.0:22         0.0.0.0:*
</span></span><span class="line"><span class="cl">LISTEN 0      4096      127.0.0.11:35819      0.0.0.0:*
</span></span><span class="line"><span class="cl">LISTEN 0      128             [::]:22            [::]:*
</span></span></code></pre></div><p>Two programs, two very different promises. SSH on <code>0.0.0.0:22</code> takes connections on any IPv4 address ws1 has
(<code>[::]:22</code> is the same for IPv6). The web server on <code>127.0.0.1:9999</code> takes them only on loopback. The
<code>127.0.0.11</code> line is Docker&rsquo;s own DNS helper inside the container.</p>
<p><code>-p</code> adds the process, but only for sockets you own, unless you use <code>sudo</code>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ sudo ss -tlnp &#34;sport = :9999&#34;
</span></span><span class="line"><span class="cl">State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
</span></span><span class="line"><span class="cl">LISTEN 0      5          127.0.0.1:9999      0.0.0.0:*    users:((&#34;python3&#34;,pid=467,fd=3))
</span></span></code></pre></div><p>The <code>&quot;sport = :9999&quot;</code> part is a filter: only sockets whose source port is 9999. Then I tried the same server
listening three ways, and connected to it by both addresses:</p>
<table>
  <thead>
      <tr>
          <th>Listening on</th>
          <th><code>curl 127.0.0.1:9999</code></th>
          <th><code>curl 192.168.10.10:9999</code></th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>127.0.0.1</code></td>
          <td>the page</td>
          <td>refused</td>
      </tr>
      <tr>
          <td><code>192.168.10.10</code></td>
          <td>refused</td>
          <td>the page</td>
      </tr>
      <tr>
          <td><code>0.0.0.0</code></td>
          <td>the page</td>
          <td>the page</td>
      </tr>
  </tbody>
</table>
<p><code>127.0.0.1</code> is the &ldquo;works on the server, nowhere else&rdquo; case. Loopback traffic never leaves the machine, so no
other host can ever reach it, whatever the firewall says. Plenty of software listens there by default, on
purpose, until you tell it otherwise. A specific address, such as <code>192.168.10.10</code>, accepts only connections sent
to that address. <code>0.0.0.0</code> (or <code>*</code> in some tools) means all of them. When <code>ss</code> says the right port but the wrong
address, the fix is in the program&rsquo;s configuration, not the network.</p>
<hr>
<h2 id="what-does-curl--v-show">What does curl -v show?</h2>
<p><code>curl -v</code> (<em>verbose</em>) narrates the whole request: lines starting <code>*</code> are curl&rsquo;s notes, <code>&gt;</code> is what it sent,
<code>&lt;</code> is what came back. Here it is against the same test server on <code>0.0.0.0</code>, by name:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ curl -sv http://ws1.lab:9999/
</span></span><span class="line"><span class="cl">* Host ws1.lab:9999 was resolved.
</span></span><span class="line"><span class="cl">* IPv6: (none)
</span></span><span class="line"><span class="cl">* IPv4: 192.168.10.10
</span></span><span class="line"><span class="cl">*   Trying 192.168.10.10:9999...
</span></span><span class="line"><span class="cl">* Connected to ws1.lab (192.168.10.10) port 9999
</span></span><span class="line"><span class="cl">* using HTTP/1.x
</span></span><span class="line"><span class="cl">&gt; GET / HTTP/1.1
</span></span><span class="line"><span class="cl">&gt; Host: ws1.lab:9999
</span></span><span class="line"><span class="cl">&gt; User-Agent: curl/8.14.1
</span></span><span class="line"><span class="cl">&gt; Accept: */*
</span></span><span class="line"><span class="cl">&gt;
</span></span><span class="line"><span class="cl">* Request completely sent off
</span></span><span class="line"><span class="cl">* HTTP 1.0, assume close after body
</span></span><span class="line"><span class="cl">&lt; HTTP/1.0 200 OK
</span></span><span class="line"><span class="cl">&lt; Server: SimpleHTTP/0.6 Python/3.13.5
</span></span><span class="line"><span class="cl">&lt; Date: Wed, 11 Nov 2026 05:57:33 GMT
</span></span><span class="line"><span class="cl">&lt; Content-type: text/html
</span></span><span class="line"><span class="cl">&lt; Content-Length: 26
</span></span><span class="line"><span class="cl">&lt; Last-Modified: Wed, 11 Nov 2026 05:57:12 GMT
</span></span><span class="line"><span class="cl">&lt;
</span></span><span class="line"><span class="cl">{ [26 bytes data]
</span></span><span class="line"><span class="cl">* shutting down connection #0
</span></span><span class="line"><span class="cl">Hello from ws1, port 9999
</span></span></code></pre></div><p>Read it as layers, top down. The first block is <strong>DNS</strong>: the name became <code>192.168.10.10</code>. <code>Trying</code> then
<code>Connected</code> is <strong>TCP</strong>: the handshake worked. The <code>&gt;</code> lines are the <strong>HTTP</strong> request, and <code>&lt; HTTP/1.0 200 OK</code>
is the answer. (<code>-s</code> only hides the progress meter.) Wherever it stops tells you which layer failed:</p>
<table>
  <thead>
      <tr>
          <th>curl says</th>
          <th>Layer</th>
          <th>Usual cause</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>Could not resolve host: nosuch.lab</code> (exit 6)</td>
          <td>DNS</td>
          <td>wrong name, or the DNS server doesn&rsquo;t know it</td>
      </tr>
      <tr>
          <td><code>connect to ... failed: Connection refused</code> (exit 7)</td>
          <td>TCP</td>
          <td>the host answered, but nothing listens on that address and port</td>
      </tr>
      <tr>
          <td><code>connect to ... failed: No route to host</code> (exit 7)</td>
          <td>IP</td>
          <td>nobody has that address, or no route to it</td>
      </tr>
      <tr>
          <td><code>Connection timed out</code> or <code>Operation timed out</code> (exit 28)</td>
          <td>IP or firewall</td>
          <td>something dropped the packets silently</td>
      </tr>
      <tr>
          <td><code>&lt; HTTP/1.1 404 Not Found</code></td>
          <td>HTTP</td>
          <td>the server works, the path doesn&rsquo;t</td>
      </tr>
  </tbody>
</table>
<p>The two <code>connect</code> failures, from the lab: a port nobody listened on, and an address nobody had.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">* connect to 192.168.10.10 port 9999 from 192.168.10.10 port 38612 failed: Connection refused
</span></span><span class="line"><span class="cl">* connect to 192.168.20.99 port 80 from 192.168.10.10 port 39970 failed: No route to host
</span></span></code></pre></div><p>Refused came back in 5 ms. No route to host took about 3 seconds: most likely the time r1 spent asking for <code>192.168.20.99</code> with ARP
before it gave up and told ws1. <code>-m 5</code> caps the whole request at 5 seconds, which you want in a script.</p>
<p>For HTTPS, <code>-v</code> adds the <strong>TLS</strong> layer between TCP and HTTP. Trimmed, from the codespace&rsquo;s curl 7.88.1 against
this site:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">* Connected to networkdoodles.com (35.185.44.232) port 443 (#0)
</span></span><span class="line"><span class="cl">* SSL connection using TLSv1.3 / TLS_AES_128_GCM_SHA256
</span></span><span class="line"><span class="cl">* ALPN: server accepted h2
</span></span><span class="line"><span class="cl">* Server certificate:
</span></span><span class="line"><span class="cl">*  subject: CN=networkdoodles.com
</span></span><span class="line"><span class="cl">*  expire date: Dec 11 12:42:43 2026 GMT
</span></span><span class="line"><span class="cl">*  issuer: C=US; O=Let&#39;s Encrypt; CN=YR1
</span></span><span class="line"><span class="cl">*  SSL certificate verify ok.
</span></span><span class="line"><span class="cl">&gt; GET / HTTP/2
</span></span><span class="line"><span class="cl">&lt; HTTP/2 200
</span></span></code></pre></div><p>That&rsquo;s the certificate&rsquo;s name, expiry and issuer without opening a browser. The
<a href="https://curl.se/docs/manpage.html">curl man page</a> lists every exit code.</p>
<hr>
<h2 id="when-do-i-want-digs-full-answer-and-when-short">When do I want dig&rsquo;s full answer, and when +short?</h2>
<p><code>dig +short</code> prints only the answer, which is what a script wants:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ dig +short srv1.lab
</span></span><span class="line"><span class="cl">192.168.20.10
</span></span></code></pre></div><p>The full answer is what you want when something&rsquo;s wrong:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ dig srv1.lab
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">; &lt;&lt;&gt;&gt; DiG 9.20.30-1~deb13u1-Debian &lt;&lt;&gt;&gt; srv1.lab
</span></span><span class="line"><span class="cl">;; global options: +cmd
</span></span><span class="line"><span class="cl">;; Got answer:
</span></span><span class="line"><span class="cl">;; -&gt;&gt;HEADER&lt;&lt;- opcode: QUERY, status: NOERROR, id: 51819
</span></span><span class="line"><span class="cl">;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">;; OPT PSEUDOSECTION:
</span></span><span class="line"><span class="cl">; EDNS: version: 0, flags:; udp: 1232
</span></span><span class="line"><span class="cl">;; QUESTION SECTION:
</span></span><span class="line"><span class="cl">;srv1.lab.			IN	A
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">;; ANSWER SECTION:
</span></span><span class="line"><span class="cl">srv1.lab.		60	IN	A	192.168.20.10
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">;; Query time: 2 msec
</span></span><span class="line"><span class="cl">;; SERVER: 192.168.20.10#53(192.168.20.10) (UDP)
</span></span><span class="line"><span class="cl">;; WHEN: Wed Nov 11 05:57:44 UTC 2026
</span></span><span class="line"><span class="cl">;; MSG SIZE  rcvd: 53
</span></span></code></pre></div><table>
  <thead>
      <tr>
          <th>Part</th>
          <th>Tells you</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>status: NOERROR</code></td>
          <td>the name exists. <code>NXDOMAIN</code> means it doesn&rsquo;t, <code>REFUSED</code> means this server won&rsquo;t answer</td>
      </tr>
      <tr>
          <td><code>flags: ... aa</code></td>
          <td><em>authoritative answer</em>: this server owns the name, it didn&rsquo;t ask anyone else</td>
      </tr>
      <tr>
          <td><code>60</code> in the answer</td>
          <td>the TTL: how many seconds clients may cache it</td>
      </tr>
      <tr>
          <td><code>SERVER:</code></td>
          <td>which DNS server actually answered</td>
      </tr>
  </tbody>
</table>
<p><code>+short</code> hides the status, and that matters. A name that doesn&rsquo;t exist prints <strong>nothing</strong>, and <code>dig</code> still
exits 0, because it did get an answer: <em>no such name</em>. The full output shows <code>status: NXDOMAIN</code>.</p>
<p>One more trap: <code>dig</code> ignores the <code>search lab</code> line in <code>/etc/resolv.conf</code>. <code>curl http://srv1</code> works, because it
uses the system&rsquo;s resolver, which adds <code>.lab</code>. <code>dig srv1</code> asks for exactly <code>srv1</code>, and the lab&rsquo;s DNS server
answers <code>status: REFUSED</code>. Use the full name, or <code>dig +search srv1</code>.</p>
<hr>
<h2 id="how-do-i-capture-packets-with-tcpdump">How do I capture packets with tcpdump?</h2>
<p><code>tcpdump</code> prints, or saves, the packets crossing an interface. It needs root, because seeing other programs'
traffic is a privilege:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ tcpdump -n -i eth1 -c 1
</span></span><span class="line"><span class="cl">tcpdump: eth1: You don&#39;t have permission to perform this capture on that device
</span></span><span class="line"><span class="cl">(socket: Operation not permitted)
</span></span></code></pre></div><p>With <code>sudo</code>, and a <strong>capture filter</strong> that keeps only DNS, while <code>dig +short srv1.lab</code> ran in another terminal:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ sudo tcpdump -n -i eth1 udp port 53
</span></span><span class="line"><span class="cl">tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
</span></span><span class="line"><span class="cl">listening on eth1, link-type EN10MB (Ethernet), snapshot length 262144 bytes
</span></span><span class="line"><span class="cl">05:59:01.773866 IP 192.168.10.10.33468 &gt; 192.168.20.10.53: 35944+ [1au] A? srv1.lab. (49)
</span></span><span class="line"><span class="cl">05:59:01.782984 IP 192.168.20.10.53 &gt; 192.168.10.10.33468: 35944* 1/0/1 A 192.168.20.10 (53)
</span></span></code></pre></div><p>The question (<code>A? srv1.lab.</code>) and the answer (<code>A 192.168.20.10</code>), with each address written as
<code>address.port</code>.</p>
<table>
  <thead>
      <tr>
          <th>Option or filter</th>
          <th>Does</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>-i eth1</code></td>
          <td>capture on this interface. Pick the one <code>ip route get</code> named</td>
      </tr>
      <tr>
          <td><code>-n</code></td>
          <td>don&rsquo;t turn addresses and ports into names (and don&rsquo;t send DNS queries of its own)</td>
      </tr>
      <tr>
          <td><code>-c 10</code></td>
          <td>stop after 10 packets</td>
      </tr>
      <tr>
          <td><code>-w file.pcap</code></td>
          <td>save raw packets to a file instead of printing them</td>
      </tr>
      <tr>
          <td><code>-r file.pcap</code></td>
          <td>read a saved file back</td>
      </tr>
      <tr>
          <td><code>host 192.168.20.10</code></td>
          <td>only packets to or from this address</td>
      </tr>
      <tr>
          <td><code>port 8080</code>, <code>tcp port 22</code></td>
          <td>only this port</td>
      </tr>
      <tr>
          <td><code>and</code>, <code>or</code>, <code>not</code></td>
          <td>combine them: <code>host 192.168.20.10 and tcp port 22</code></td>
      </tr>
  </tbody>
</table>
<p>The filter language is libpcap&rsquo;s, documented in <a href="https://www.tcpdump.org/manpages/pcap-filter.7.html">pcap-filter(7)</a>,
and the options in <a href="https://www.tcpdump.org/manpages/tcpdump.1.html">tcpdump(1)</a>. A capture filter decides what&rsquo;s
kept at all: anything it drops is gone. Wireshark 101&rsquo;s lesson on
<a href="/lessons/wireshark-101/capture-vs-display-filters/">capture filters vs display filters</a> covers the difference.</p>
<p>Saving with <code>-w</code> and reading back with <code>-r</code>: a test connection from ws1 to srv1&rsquo;s SSH port, written to a file
and then read back:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ sudo tcpdump -n -i eth1 -w /tmp/lesson8-ssh.pcap host 192.168.20.10 and tcp port 22
</span></span><span class="line"><span class="cl">tcpdump: listening on eth1, link-type EN10MB (Ethernet), snapshot length 262144 bytes
</span></span><span class="line"><span class="cl">6 packets captured
</span></span><span class="line"><span class="cl">$ tcpdump -n -r /tmp/lesson8-ssh.pcap
</span></span><span class="line"><span class="cl">reading from file /tmp/lesson8-ssh.pcap, link-type EN10MB (Ethernet), snapshot length 262144
</span></span><span class="line"><span class="cl">05:59:05.387230 IP 192.168.10.10.60292 &gt; 192.168.20.10.22: Flags [S], seq 3914208904, win 64240, options [mss 1460,sackOK,TS val 1308834635 ecr 0,nop,wscale 7], length 0
</span></span><span class="line"><span class="cl">05:59:05.387361 IP 192.168.20.10.22 &gt; 192.168.10.10.60292: Flags [S.], seq 1686286914, ack 3914208905, win 65160, options [mss 1460,sackOK,TS val 590197084 ecr 1308834635,nop,wscale 7], length 0
</span></span><span class="line"><span class="cl">05:59:05.387369 IP 192.168.10.10.60292 &gt; 192.168.20.10.22: Flags [.], ack 1, win 502, options [nop,nop,TS val 1308834635 ecr 590197084], length 0
</span></span><span class="line"><span class="cl">05:59:05.420529 IP 192.168.20.10.22 &gt; 192.168.10.10.60292: Flags [P.], seq 1:42, ack 1, win 510, options [nop,nop,TS val 590197117 ecr 1308834635], length 41: SSH: SSH-2.0-OpenSSH_10.0p2 Debian-7+deb13u4
</span></span><span class="line"><span class="cl">05:59:05.420549 IP 192.168.10.10.60292 &gt; 192.168.20.10.22: Flags [.], ack 42, win 502, options [nop,nop,TS val 1308834668 ecr 590197117], length 0
</span></span><span class="line"><span class="cl">05:59:05.420966 IP 192.168.10.10.60292 &gt; 192.168.20.10.22: Flags [R.], seq 1, ack 42, win 502, options [nop,nop,TS val 1308834668 ecr 590197117], length 0
</span></span></code></pre></div><p>Reading the file back needs no <code>sudo</code>. The first three lines are the TCP handshake: <code>[S]</code> (SYN), <code>[S.]</code>
(SYN-ACK) and <code>[.]</code> (ACK). Then srv1 sends its SSH banner, and my test hung up abruptly, so the last packet is a
reset, <code>[R.]</code>. <code>60292</code> is ws1&rsquo;s temporary port for this one connection. A <strong>pcap</strong> file like this opens in
Wireshark, which is the better place to read anything longer than a handful of lines:
<a href="/lessons/wireshark-101/tcp/">TCP in Wireshark</a> reads the same flags field by field.</p>
<hr>
<h2 id="what-i-see-go-wrong">What I see go wrong</h2>
<p><strong>&ldquo;It works on the server.&rdquo;</strong> <code>curl localhost</code> on the server proves the program runs. It doesn&rsquo;t prove anyone
else can reach it. Check the address in <code>ss -tlnp</code> before you look at the network.</p>
<p><strong>Reading <code>0.0.0.0</code> in the Peer column as a problem.</strong> On a <code>LISTEN</code> line, <code>0.0.0.0:*</code> under Peer Address just
means nobody&rsquo;s connected yet. The address that matters is the Local one.</p>
<p><strong><code>dig</code> and the application disagree.</strong> <code>dig</code> asks the DNS server directly and skips <code>/etc/hosts</code> and the
search list. The application uses the system resolver, which reads both. <code>getent hosts srv1</code> asks the way the
application does.</p>
<p><strong><code>dig +short</code> printing nothing.</strong> That&rsquo;s an answer: the name doesn&rsquo;t exist (or has no record of that type).
Run it without <code>+short</code> and read the status.</p>
<p><strong>Capturing on the wrong interface.</strong> ws1 has two. Traffic to srv1 leaves by <code>eth1</code>; a capture on <code>eth0</code>
shows nothing and looks like &ldquo;no traffic&rdquo;. <code>ip route get</code> says which one.</p>
<p><strong>Forgetting <code>-n</code> in tcpdump.</strong> Name lookups slow the output down, and the DNS queries tcpdump sends for them
can end up in your own capture.</p>
<p><strong>A <code>-w</code> file that&rsquo;s short or empty.</strong> tcpdump buffers what it writes. Stop it with Ctrl-C, or let <code>-c</code> stop it,
so it finishes writing the file. Killed any harder, it can leave the last packets out.</p>
<hr>
<h2 id="try-it">Try it</h2>
<p><a href="/labs/linux-cli-101/task-8-1/">Chapter 8 of the lab</a> works from ws1 towards srv1, through r1. You find the
gateway and the outgoing interface, count the hops and resolve srv1 with <code>dig</code>. Then a web service on srv1
answers when you <code>curl</code> it on srv1 itself but not from ws1, and it&rsquo;s yours to find out why and fix.
Last, you capture the conversation with <code>tcpdump -w</code>, copy the file back with <code>scp</code>, and read the handshake out
of it.</p>
<h2 id="where-to-go-next">Where to go next</h2>
<p>That&rsquo;s the end of Linux and the command line 101. You can now move around the filesystem, read and search files,
chain tools with pipes, fix permissions, manage processes, log in with keys, script a check, and troubleshoot a
service from the command line. Every lab on this site assumes those.</p>
<p>Two courses pick up from here:</p>
<ul>
<li><a href="/lessons/wireshark-101/">Wireshark 101</a> takes the pcap files <code>tcpdump</code> saves and reads them properly:
ARP, DHCP, DNS, the TCP handshake and TLS, and a broken path to troubleshoot.</li>
<li><a href="/lessons/python-101/">Python 101</a> takes over where Bash gets awkward: real data structures, parsing <code>show</code>
output, and logging in to every device from one script.</li>
</ul>
<p>If you want a refresher first, the course starts at <a href="/lessons/linux-cli-101/shell-and-filesystem/">the shell and the filesystem</a>.</p>
]]></content:encoded></item><item><title>A Little Bash Scripting: Variables, Loops and Exit Codes (Linux CLI 101, Lesson 7)</title><link>https://networkdoodles.com/lessons/linux-cli-101/bash-scripting-basics/</link><pubDate>Mon, 16 Nov 2026 09:00:00 -0500</pubDate><dc:creator>David Henderson</dc:creator><guid>https://networkdoodles.com/lessons/linux-cli-101/bash-scripting-basics/</guid><category>linux</category><category>automation</category><category>network-automation</category><category>howto</category><description>A first Bash script for network work: the shebang, quoting, arguments, loops over a host list, exit codes, if, &amp;&amp; and ||, set -euo pipefail, and Python's place.</description><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<p>A <strong>Bash script</strong> is a text file full of the commands you&rsquo;d otherwise type, run top to bottom by Bash. Three
ideas carry most of it: <strong>quote your variables</strong> (<code>&quot;$file&quot;</code>, not <code>$file</code>), <strong>every command returns an exit
code</strong> (0 means it worked, anything else means it didn&rsquo;t), and <code>if</code>, <code>&amp;&amp;</code> and <code>||</code> decide what to do next by
reading that code. Put <code>set -euo pipefail</code> near the top so a script stops at the first surprise instead of
carrying on with bad data.</p>
<p>That&rsquo;s enough to write the script every network engineer writes first: read a list of hosts, check each one,
and exit non-zero if any of them failed, so that whatever ran it (you, cron, a CI pipeline) knows. When the
script needs real data structures, parsing or error handling, that&rsquo;s where Bash stops and Python starts.</p>
<p>The examples here ran in the lab&rsquo;s codespace, Debian 12 with GNU Bash 5.2.15. The reference is the
<a href="https://www.gnu.org/software/bash/manual/bash.html">GNU Bash manual</a>.</p>
<hr>
<h2 id="what-is-a-script-and-how-does-linux-run-it">What is a script, and how does Linux run it?</h2>
<p>Here&rsquo;s a complete one, <code>hello.sh</code>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="cp">#!/usr/bin/env bash
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="c1"># hello.sh: say who and where we are</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;Hello from </span><span class="k">$(</span>hostname<span class="k">)</span><span class="s2">, I am </span><span class="k">$(</span>whoami<span class="k">)</span><span class="s2">&#34;</span>
</span></span></code></pre></div><p>The first line is the <strong>shebang</strong>: <code>#!</code> followed by the program that should run the file. <code>/usr/bin/env bash</code>
means <em>find <code>bash</code> on my PATH and use that</em>, the same search the shell does for any command
(<a href="/lessons/linux-cli-101/processes-environment-path/">lesson 5</a>). Lines starting <code>#</code> after that are comments.
<code>$(hostname)</code> is <strong>command substitution</strong>: Bash runs the command and pastes its output into the line.</p>
<p>A new file isn&rsquo;t allowed to run until you give it the execute bit, from
<a href="/lessons/linux-cli-101/users-permissions-sudo/">lesson 4</a>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ./hello.sh
</span></span><span class="line"><span class="cl">bash: line 8: ./hello.sh: Permission denied
</span></span><span class="line"><span class="cl">$ chmod +x hello.sh
</span></span><span class="line"><span class="cl">$ ls -l hello.sh
</span></span><span class="line"><span class="cl">-rwxr-xr-x 1 vscode vscode 99 Nov 11 05:55 hello.sh
</span></span><span class="line"><span class="cl">$ ./hello.sh
</span></span><span class="line"><span class="cl">Hello from codespace, I am vscode
</span></span></code></pre></div><p>The <code>./</code> matters. The current folder isn&rsquo;t on PATH, so <code>hello.sh</code> on its own would be &ldquo;command not found&rdquo;.
<code>./hello.sh</code> is a path: <em>this file, here</em>. You can also run <code>bash hello.sh</code>, which needs no execute bit and
ignores the shebang, because you&rsquo;ve named the interpreter yourself.</p>
<hr>
<h2 id="how-do-variables-and-quoting-work">How do variables and quoting work?</h2>
<p>A <strong>variable</strong> is a name that holds a piece of text. You set it with <code>name=value</code> and read it with <code>$name</code>.
There are no spaces around the <code>=</code>. With spaces, Bash thinks <code>site</code> is a command:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ site = nyc
</span></span><span class="line"><span class="cl">bash: line 1: site: command not found
</span></span></code></pre></div><p>Bash keeps every value as text and, by default, does something surprising when it reads an unquoted variable:
it <strong>splits</strong> the value on spaces (and expands any <code>*</code> in it as a glob). That bites the moment a file name
has a space in it, which network backups often do. Here&rsquo;s a two-line script, <code>count.sh</code>, that counts the lines
in a file:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nv">file</span><span class="o">=</span><span class="nv">$1</span>
</span></span><span class="line"><span class="cl">wc -l <span class="nv">$file</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ./count.sh &#34;cfgs/nyc leaf1.cfg&#34;
</span></span><span class="line"><span class="cl">wc: cfgs/nyc: No such file or directory
</span></span><span class="line"><span class="cl">wc: leaf1.cfg: No such file or directory
</span></span><span class="line"><span class="cl">0 total
</span></span></code></pre></div><p>One argument went in. Two came out, because <code>$file</code> without quotes became two words. Add the quotes and it&rsquo;s
one file again:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nv">file</span><span class="o">=</span><span class="s2">&#34;</span><span class="nv">$1</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl">wc -l <span class="s2">&#34;</span><span class="nv">$file</span><span class="s2">&#34;</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ./count.sh &#34;cfgs/nyc leaf1.cfg&#34;
</span></span><span class="line"><span class="cl">2 cfgs/nyc leaf1.cfg
</span></span></code></pre></div><p>The two kinds of quotes differ in one way:</p>
<table>
  <thead>
      <tr>
          <th>Quotes</th>
          <th><code>$</code> inside them</th>
          <th>Example</th>
          <th>Prints</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>double, <code>&quot;...&quot;</code></td>
          <td>expanded</td>
          <td><code>echo &quot;site is $site&quot;</code></td>
          <td><code>site is nyc</code></td>
      </tr>
      <tr>
          <td>single, <code>'...'</code></td>
          <td>left alone</td>
          <td><code>echo 'site is $site'</code></td>
          <td><code>site is $site</code></td>
      </tr>
  </tbody>
</table>
<p>Use double quotes around every variable unless you have a reason not to. Use <code>${name}</code> when the next character
could be part of the name: <code>&quot;${site}_leaf1&quot;</code>, because <code>$site_leaf1</code> asks for a variable called <code>site_leaf1</code>.
The manual&rsquo;s section on <a href="https://www.gnu.org/software/bash/manual/html_node/Quoting.html">quoting</a> has the
full rules.</p>
<hr>
<h2 id="how-does-a-script-read-its-arguments">How does a script read its arguments?</h2>
<p>Whatever you type after the script&rsquo;s name arrives as <strong>positional parameters</strong>:</p>
<table>
  <thead>
      <tr>
          <th>Name</th>
          <th>Holds</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>$0</code></td>
          <td>the script&rsquo;s own name, as you called it</td>
      </tr>
      <tr>
          <td><code>$1</code>, <code>$2</code> …</td>
          <td>the first, second … argument</td>
      </tr>
      <tr>
          <td><code>$#</code></td>
          <td>how many arguments there are</td>
      </tr>
      <tr>
          <td><code>&quot;$@&quot;</code></td>
          <td>all of them, each one kept whole</td>
      </tr>
  </tbody>
</table>
<p>A short <code>args.sh</code> that prints them, called with an argument that has a space in it:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ./args.sh r1 &#34;nyc leaf1&#34; srv1
</span></span><span class="line"><span class="cl">script: ./args.sh
</span></span><span class="line"><span class="cl">first:  r1
</span></span><span class="line"><span class="cl">count:  3
</span></span><span class="line"><span class="cl">arg:    r1
</span></span><span class="line"><span class="cl">arg:    nyc leaf1
</span></span><span class="line"><span class="cl">arg:    srv1
</span></span></code></pre></div><p>The loop in it was <code>for a in &quot;$@&quot;</code>. With the quotes, <code>nyc leaf1</code> stays one argument. Without them it would be
two, for the same reason as <code>$file</code> above.</p>
<p>Often an argument is optional. <code>${1:-hosts.txt}</code> means <em>the first argument, or <code>hosts.txt</code> if there isn&rsquo;t
one</em>. The lab&rsquo;s <code>reach.sh</code> starts with exactly that line.</p>
<hr>
<h2 id="how-do-you-loop-over-a-file-of-hosts">How do you loop over a file of hosts?</h2>
<p>The tempting way is <code>for h in $(cat hosts.txt)</code>. It reads the file and splits it into words, so it breaks on
anything that isn&rsquo;t one word per line. Here it is on a small file with a comment and a blank line:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl"># lab machines
</span></span><span class="line"><span class="cl">ws1
</span></span><span class="line"><span class="cl">srv1
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">r1
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ for h in $(cat hosts-demo.txt); do echo &#34;[$h]&#34;; done
</span></span><span class="line"><span class="cl">[#]
</span></span><span class="line"><span class="cl">[lab]
</span></span><span class="line"><span class="cl">[machines]
</span></span><span class="line"><span class="cl">[ws1]
</span></span><span class="line"><span class="cl">[srv1]
</span></span><span class="line"><span class="cl">[r1]
</span></span></code></pre></div><p>The comment became three &ldquo;hosts&rdquo;. The way that reads <strong>lines</strong> is <code>while read -r</code>, with the file fed in at the
end of the loop:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="k">while</span> <span class="nb">read</span> -r h<span class="p">;</span> <span class="k">do</span>
</span></span><span class="line"><span class="cl">  <span class="nb">echo</span> <span class="s2">&#34;[</span><span class="nv">$h</span><span class="s2">]&#34;</span>
</span></span><span class="line"><span class="cl"><span class="k">done</span> &lt; hosts-demo.txt
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">[# lab machines]
</span></span><span class="line"><span class="cl">[ws1]
</span></span><span class="line"><span class="cl">[srv1]
</span></span><span class="line"><span class="cl">[]
</span></span><span class="line"><span class="cl">[r1]
</span></span></code></pre></div><p>One line per pass, kept whole, including the blank one. Now the comment and the empty line are easy to
recognize and skip (the lab&rsquo;s starter script does that for you with a <code>case</code> line). <code>-r</code> tells <code>read</code> to
leave backslashes alone. A <code>for</code> loop is still the right tool over a <strong>glob</strong>, such as <code>for f in &quot;$dir&quot;/*.cfg</code>,
because the shell hands it one file name per pass, spaces and all.</p>
<hr>
<h2 id="what-is-an-exit-code">What is an exit code?</h2>
<p>Every command finishes with a number, its <strong>exit code</strong> (or <em>exit status</em>): <code>0</code> for success, anything from
<code>1</code> to <code>255</code> for some kind of failure. The shell doesn&rsquo;t print it. It keeps it in <code>$?</code> until the next command
replaces it:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ls /etc/hostname; echo $?
</span></span><span class="line"><span class="cl">/etc/hostname
</span></span><span class="line"><span class="cl">0
</span></span><span class="line"><span class="cl">$ ls /nope; echo $?
</span></span><span class="line"><span class="cl">ls: cannot access &#39;/nope&#39;: No such file or directory
</span></span><span class="line"><span class="cl">2
</span></span><span class="line"><span class="cl">$ grep -q root /etc/passwd; echo $?
</span></span><span class="line"><span class="cl">0
</span></span><span class="line"><span class="cl">$ grep -q nobodyhere /etc/passwd; echo $?
</span></span><span class="line"><span class="cl">1
</span></span></code></pre></div><p>Each program documents its own codes in its man page. A few you&rsquo;ll meet constantly:</p>
<table>
  <thead>
      <tr>
          <th>Code</th>
          <th>From</th>
          <th>Means</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>0</code></td>
          <td>anything</td>
          <td>it worked</td>
      </tr>
      <tr>
          <td><code>1</code></td>
          <td><code>grep</code></td>
          <td>no line matched (not an error, just <em>no</em>)</td>
      </tr>
      <tr>
          <td><code>2</code></td>
          <td><code>grep</code>, <code>ls</code></td>
          <td>real trouble, such as a file that isn&rsquo;t there</td>
      </tr>
      <tr>
          <td><code>1</code></td>
          <td><code>ping</code></td>
          <td>no reply came back</td>
      </tr>
      <tr>
          <td><code>2</code></td>
          <td><code>ping</code></td>
          <td>something else went wrong, such as a name that doesn&rsquo;t resolve</td>
      </tr>
      <tr>
          <td><code>126</code></td>
          <td>Bash</td>
          <td>found the file but couldn&rsquo;t run it (no execute bit)</td>
      </tr>
      <tr>
          <td><code>127</code></td>
          <td>Bash</td>
          <td>command not found</td>
      </tr>
  </tbody>
</table>
<p>The <code>ping</code> codes, from the codespace:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ping -c 1 -W 1 ws1 &gt;/dev/null; echo &#34;ws1: $?&#34;
</span></span><span class="line"><span class="cl">ws1: 0
</span></span><span class="line"><span class="cl">$ ping -c 1 -W 1 172.80.102.99 &gt;/dev/null; echo &#34;dead: $?&#34;
</span></span><span class="line"><span class="cl">dead: 1
</span></span><span class="line"><span class="cl">$ ping -c 1 -W 1 nosuchhost &gt;/dev/null; echo &#34;noname: $?&#34;
</span></span><span class="line"><span class="cl">ping: nosuchhost: Name or service not known
</span></span><span class="line"><span class="cl">noname: 2
</span></span></code></pre></div><p><code>&gt;/dev/null</code> throws the normal output away. The exit code still arrives, and that&rsquo;s all a script needs.</p>
<p>Your own script sets its exit code with <code>exit N</code>. Without one, it exits with the code of the last command it
ran. That&rsquo;s how a script reports back: a monitoring job, a CI pipeline, an Ansible task or another script
reads the number, not the text. The manual&rsquo;s <a href="https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html">exit status</a>
page lists Bash&rsquo;s own codes.</p>
<hr>
<h2 id="how-do-if--and--use-exit-codes">How do if, &amp;&amp; and || use exit codes?</h2>
<p><code>if</code> doesn&rsquo;t test a true-or-false value. It <strong>runs a command</strong> and looks at its exit code: <code>0</code> takes the
<code>then</code> branch, anything else takes <code>else</code>. <code>[ ... ]</code> is itself a command (also called <code>test</code>) whose whole job is
to return 0 or 1: <code>[ -f &quot;$file&quot; ]</code> asks <em>is this a regular file?</em>, <code>[ -z &quot;$x&quot; ]</code> asks <em>is this empty?</em>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="cp">#!/usr/bin/env bash
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="nb">set</span> -euo pipefail
</span></span><span class="line"><span class="cl"><span class="nv">file</span><span class="o">=</span><span class="s2">&#34;</span><span class="si">${</span><span class="nv">1</span><span class="k">:-</span><span class="nv">hosts</span><span class="p">-demo.txt</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="k">if</span> <span class="o">[</span> ! -f <span class="s2">&#34;</span><span class="nv">$file</span><span class="s2">&#34;</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
</span></span><span class="line"><span class="cl">  <span class="nb">echo</span> <span class="s2">&#34;no such file: </span><span class="nv">$file</span><span class="s2">&#34;</span> &gt;<span class="p">&amp;</span><span class="m">2</span>
</span></span><span class="line"><span class="cl">  <span class="nb">exit</span> <span class="m">2</span>
</span></span><span class="line"><span class="cl"><span class="k">fi</span>
</span></span><span class="line"><span class="cl"><span class="k">if</span> grep -q <span class="s2">&#34;^r1</span>$<span class="s2">&#34;</span> <span class="s2">&#34;</span><span class="nv">$file</span><span class="s2">&#34;</span><span class="p">;</span> <span class="k">then</span>
</span></span><span class="line"><span class="cl">  <span class="nb">echo</span> <span class="s2">&#34;</span><span class="nv">$file</span><span class="s2"> lists r1&#34;</span>
</span></span><span class="line"><span class="cl"><span class="k">else</span>
</span></span><span class="line"><span class="cl">  <span class="nb">echo</span> <span class="s2">&#34;</span><span class="nv">$file</span><span class="s2"> has no r1&#34;</span>
</span></span><span class="line"><span class="cl"><span class="k">fi</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;done&#34;</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ./ifdemo.sh; echo &#34;exit $?&#34;
</span></span><span class="line"><span class="cl">hosts-demo.txt lists r1
</span></span><span class="line"><span class="cl">done
</span></span><span class="line"><span class="cl">exit 0
</span></span><span class="line"><span class="cl">$ ./ifdemo.sh args.sh; echo &#34;exit $?&#34;
</span></span><span class="line"><span class="cl">args.sh has no r1
</span></span><span class="line"><span class="cl">done
</span></span><span class="line"><span class="cl">exit 0
</span></span><span class="line"><span class="cl">$ ./ifdemo.sh nope.txt; echo &#34;exit $?&#34;
</span></span><span class="line"><span class="cl">no such file: nope.txt
</span></span><span class="line"><span class="cl">exit 2
</span></span></code></pre></div><p>The error message goes to stderr with <code>&gt;&amp;2</code>, as in <a href="/lessons/linux-cli-101/pipes-redirection-grep/">lesson 3</a>,
so it doesn&rsquo;t mix with the script&rsquo;s real output. <code>grep -q</code> prints nothing and only sets the code, which makes it
a natural <code>if</code> condition.</p>
<p>For one-liners there&rsquo;s a shorter form. <code>a &amp;&amp; b</code> runs <code>b</code> only if <code>a</code> succeeded. <code>a || b</code> runs <code>b</code> only if <code>a</code>
failed:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ping -c 1 -W 1 srv1 &gt;/dev/null &amp;&amp; echo &#34;srv1 answers&#34; || echo &#34;srv1 is quiet&#34;
</span></span><span class="line"><span class="cl">srv1 answers
</span></span><span class="line"><span class="cl">$ ping -c 1 -W 1 172.80.102.99 &gt;/dev/null &amp;&amp; echo &#34;old box answers&#34; || echo &#34;old box is quiet&#34;
</span></span><span class="line"><span class="cl">old box is quiet
</span></span></code></pre></div><p>Use that on the command line. In a script, prefer <code>if</code>: in <code>a &amp;&amp; b || c</code>, <code>c</code> also runs when <code>b</code> fails, which
is rarely what you meant.</p>
<hr>
<h2 id="what-does-set--euo-pipefail-do">What does set -euo pipefail do?</h2>
<p>By default Bash is forgiving: a command fails, Bash prints its error and moves on to the next line. In a script
that changes things, that&rsquo;s dangerous. Three options, set together, make it strict
(<a href="https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html">the set builtin</a>):</p>
<table>
  <thead>
      <tr>
          <th>Option</th>
          <th>Effect</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>-e</code></td>
          <td>exit as soon as a command fails</td>
      </tr>
      <tr>
          <td><code>-u</code></td>
          <td>treat reading an unset variable as an error</td>
      </tr>
      <tr>
          <td><code>-o pipefail</code></td>
          <td>a pipeline fails if any command in it fails, not only the last</td>
      </tr>
  </tbody>
</table>
<p><strong><code>-u</code> catches typos.</strong> This script was meant to print a folder to clean, but the second line says <code>$sitedir</code>
instead of <code>$site_dir</code>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="cp">#!/usr/bin/env bash
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="c1"># clean out the old backups for one site</span>
</span></span><span class="line"><span class="cl"><span class="nv">site_dir</span><span class="o">=</span><span class="s2">&#34;/tmp/lesson7-demo/backups/</span><span class="nv">$1</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;would remove: </span><span class="nv">$sitedir</span><span class="s2">/*&#34;</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ./backup-dir.sh nyc
</span></span><span class="line"><span class="cl">would remove: /*
</span></span></code></pre></div><p>An unset variable is silently empty, so the path collapsed to <code>/*</code>. The same script with <code>set -euo pipefail</code>
as its second line (so the two lines above become lines 4 and 5):</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ./backup-dir-u.sh nyc
</span></span><span class="line"><span class="cl">./backup-dir-u.sh: line 5: sitedir: unbound variable
</span></span><span class="line"><span class="cl">$ ./backup-dir-u.sh
</span></span><span class="line"><span class="cl">./backup-dir-u.sh: line 4: $1: unbound variable
</span></span></code></pre></div><p>Both exit with code 1 before doing anything. The second shows <code>-u</code> also catching a missing argument. When an
argument really is optional, say so with <code>${1:-}</code> (empty if missing) or a default.</p>
<p><strong><code>-e</code> stops after a failed step.</strong> A <code>cd</code> into a folder that isn&rsquo;t there, then the rest of the script:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ./e.sh; echo &#34;exit $?&#34;
</span></span><span class="line"><span class="cl">./e.sh: line 2: cd: /tmp/lesson7-demo/no-such-folder: No such file or directory
</span></span><span class="line"><span class="cl">still running, in /tmp/lesson7-demo
</span></span><span class="line"><span class="cl">exit 0
</span></span><span class="line"><span class="cl">$ ./e2.sh; echo &#34;exit $?&#34;
</span></span><span class="line"><span class="cl">./e2.sh: line 3: cd: /tmp/lesson7-demo/no-such-folder: No such file or directory
</span></span><span class="line"><span class="cl">exit 1
</span></span></code></pre></div><p>Without <code>-e</code>, the script carried on in the wrong folder and still reported success. With it, it stopped.
<code>-e</code> doesn&rsquo;t fire for a command that&rsquo;s being tested, by <code>if</code>, <code>while</code>, <code>!</code> or on the left of <code>&amp;&amp;</code> and <code>||</code>. That&rsquo;s
why <code>ifdemo.sh</code> carried on when <code>grep</code> found no <code>r1</code>: a &ldquo;no&rdquo; you asked for isn&rsquo;t a crash.</p>
<p><strong><code>pipefail</code> keeps errors from hiding in pipelines.</strong> A pipeline&rsquo;s exit code is normally the last command&rsquo;s:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ grep ntp /nope/r1.cfg | wc -l; echo &#34;exit $?&#34;
</span></span><span class="line"><span class="cl">grep: /nope/r1.cfg: No such file or directory
</span></span><span class="line"><span class="cl">0
</span></span><span class="line"><span class="cl">exit 0
</span></span><span class="line"><span class="cl">$ set -o pipefail; grep ntp /nope/r1.cfg | wc -l; echo &#34;exit $?&#34;
</span></span><span class="line"><span class="cl">grep: /nope/r1.cfg: No such file or directory
</span></span><span class="line"><span class="cl">0
</span></span><span class="line"><span class="cl">exit 2
</span></span></code></pre></div><p><code>wc</code> happily counted zero lines of nothing. With <code>pipefail</code>, <code>grep</code>&rsquo;s failure comes through.</p>
<p>None of the three is perfect, and <code>-e</code> in particular has corner cases the manual spends a page on. Treat them as
a safety net, and still check the things that matter with <code>if</code>.</p>
<hr>
<h2 id="where-does-bash-stop-and-python-start">Where does Bash stop and Python start?</h2>
<p>Bash is glue. It&rsquo;s at its best starting programs, connecting them with pipes, and checking their exit codes.
It gets painful as soon as the data has shape.</p>
<table>
  <thead>
      <tr>
          <th>Job</th>
          <th>Bash</th>
          <th>Python</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>run a few commands in order, stop on failure</td>
          <td>natural</td>
          <td>more typing for the same thing</td>
      </tr>
      <tr>
          <td>loop over hosts or files, call a tool on each</td>
          <td>natural</td>
          <td>fine</td>
      </tr>
      <tr>
          <td>glue <code>grep</code>, <code>sort</code>, <code>ssh</code>, <code>scp</code> together</td>
          <td>natural</td>
          <td>awkward</td>
      </tr>
      <tr>
          <td>numbers beyond whole-number arithmetic</td>
          <td>no floats at all</td>
          <td>built in</td>
      </tr>
      <tr>
          <td>lists of dicts, JSON, YAML, CSV</td>
          <td>fragile, needs extra tools</td>
          <td>built in or one import</td>
      </tr>
      <tr>
          <td>parsing <code>show</code> output into fields</td>
          <td><code>grep</code>/<code>cut</code> until it breaks</td>
          <td>regex, TextFSM</td>
      </tr>
      <tr>
          <td>talking to devices or APIs</td>
          <td><code>ssh</code> and <code>curl</code>, one-shot</td>
          <td>Netmiko, <code>requests</code></td>
      </tr>
      <tr>
          <td>handling some errors and carrying on</td>
          <td>possible, gets ugly</td>
          <td><code>try</code>/<code>except</code></td>
      </tr>
      <tr>
          <td>testing it</td>
          <td>rare</td>
          <td>pytest</td>
      </tr>
  </tbody>
</table>
<p>A rough rule: if a script passes 50 lines, needs a data structure more complicated than a list of names, or has
you reaching for <code>awk</code> inside <code>sed</code> inside a loop, write it in Python. <a href="/lessons/python-101/">Python 101</a>
starts from <a href="/lessons/python-101/running-python/">running Python</a> in the same terminal, and its
<a href="/lessons/python-101/inventory-to-csv/">capstone</a> is the grown-up version of this lesson&rsquo;s host check: an
inventory of every device, written to CSV, with a list of the ones it couldn&rsquo;t reach.</p>
<hr>
<h2 id="what-i-see-go-wrong">What I see go wrong</h2>
<p><strong>Unquoted variables.</strong> Works for months, then a backup called <code>sw1 2026-11-02.cfg</code> turns up and the script
reads two files that don&rsquo;t exist. Quote every <code>&quot;$var&quot;</code> and <code>&quot;$@&quot;</code>.</p>
<p><strong>Spaces around <code>=</code>.</strong> <code>site = nyc</code> runs a command called <code>site</code>. Assignments have no spaces.</p>
<p><strong>Reading <code>$?</code> too late.</strong> <code>$?</code> belongs to the most recent command, and <code>echo</code> is a command. Save it
(<code>rc=$?</code>) straight away if you need it twice.</p>
<p><strong>A script that always exits 0.</strong> It printed &ldquo;down&rdquo; for a host but still reported success, so the pipeline
that ran it went green. If the script&rsquo;s job is to check something, its exit code is the answer.</p>
<p><strong><code>for</code> over <code>$(cat file)</code>.</strong> Splits comments and anything with a space into pieces. Use <code>while read -r</code> for
lines.</p>
<p><strong>Windows line endings.</strong> A script edited on Windows can carry invisible <code>\r</code> characters at the end of each line,
and Bash then reports errors about commands or paths you can&rsquo;t see anything wrong with. <code>cat -A script.sh</code>
shows them as <code>^M</code>.</p>
<hr>
<h2 id="try-it">Try it</h2>
<p><a href="/labs/linux-cli-101/task-7-1/">Chapter 7 of the lab</a> has you finish <code>scripts/reach.sh</code>, which pings every
host in <code>hosts.txt</code> (including one that was switched off last year) and exits 1 if any are down. The challenge
writes <code>scripts/audit.sh</code>, which lists the configs in a folder with no <code>ntp server</code> line. The checker runs both
against host lists and folders they haven&rsquo;t seen, including file names with spaces and an empty folder.</p>
<h2 id="whats-next">What&rsquo;s next</h2>
<p><code>reach.sh</code> only asks whether a host answers ping. <a href="/lessons/linux-cli-101/network-commands/">Lesson 8</a> asks
the harder questions from the command line: which way a packet goes, what&rsquo;s listening and on which address,
what <code>curl</code> sees from DNS to HTTP, and what&rsquo;s actually on the wire.</p>
]]></content:encoded></item><item><title>SSH and Keys: Logging In Without a Password (Linux CLI 101, Lesson 6)</title><link>https://networkdoodles.com/lessons/linux-cli-101/ssh-and-keys/</link><pubDate>Mon, 16 Nov 2026 09:00:00 -0500</pubDate><dc:creator>David Henderson</dc:creator><guid>https://networkdoodles.com/lessons/linux-cli-101/ssh-and-keys/</guid><category>linux</category><category>networking-basics</category><category>network-automation</category><category>security</category><description>How SSH checks a server's host key and proves who you are: known_hosts, ed25519 key pairs, authorized_keys, ~/.ssh/config, scp, and a host key that changed.</description><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<p><strong>SSH</strong> (Secure Shell) gives you a shell on another machine over an encrypted connection. Two kinds of key
make it safe. The server&rsquo;s <strong>host key</strong> proves to you that you&rsquo;ve reached the right machine, and your
computer remembers it in <code>~/.ssh/known_hosts</code>. Your own <strong>key pair</strong> proves to the server who you are: the
<strong>private key</strong> never leaves your machine, and the <strong>public key</strong> goes into the server&rsquo;s
<code>~/.ssh/authorized_keys</code>. With both in place you log in with no password, and a line in <code>~/.ssh/config</code>
lets you do it by a short name.</p>
<p>You&rsquo;ve probably used SSH to network devices for years, with a password. Automation needs the key-based
version: Ansible logs in to every host over SSH, and Git pushes over it.</p>
<hr>
<h2 id="what-are-the-client-and-the-server">What are the client and the server?</h2>
<p>The <strong>SSH client</strong> is the program you run, <code>ssh</code>. The <strong>SSH server</strong> is a program on the other machine,
<code>sshd</code> (the <em>d</em> is for <em>daemon</em>, a program that runs in the background waiting for work), listening on TCP
port 22. Both come from the same project, <a href="https://www.openssh.org/">OpenSSH</a>, and the versions in this lab
differ, which matters below:</p>
<table>
  <thead>
      <tr>
          <th>Machine</th>
          <th>Role in the lab</th>
          <th><code>ssh -V</code> says</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>the codespace (Debian 12)</td>
          <td>client</td>
          <td><code>OpenSSH_9.2p1 Debian-2+deb12u10</code></td>
      </tr>
      <tr>
          <td>ws1 and srv1 (Debian 13)</td>
          <td>servers (they have the client too)</td>
          <td><code>OpenSSH_10.0p2 Debian-7+deb13u4</code></td>
      </tr>
      <tr>
          <td>r1 (CapeOS)</td>
          <td>a router with an SSH server and a password login</td>
          <td></td>
      </tr>
  </tbody>
</table>
<p>When you connect, the client and server agree on encryption, the server proves its identity with its host
key, and then you prove yours, with a key or a password.</p>
<hr>
<h2 id="what-is-a-host-key-and-whats-the-first-connection-question">What is a host key, and what&rsquo;s the first-connection question?</h2>
<p>A <strong>host key</strong> is a key pair that belongs to the server itself, made when SSH was installed. Its job is to
stop you typing your password into an impostor. The first time you connect, your client has never seen this
server&rsquo;s key, so it asks you. Here is that question from the codespace, connecting to srv1:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">The authenticity of host &#39;srv1 (172.80.102.12)&#39; can&#39;t be established.
</span></span><span class="line"><span class="cl">ED25519 key fingerprint is SHA256:D5T0vLnK5aqJqyXkl0nNrxpCcACGUqPLTgSGR0KO4Eo.
</span></span><span class="line"><span class="cl">This key is not known by any other names.
</span></span><span class="line"><span class="cl">Are you sure you want to continue connecting (yes/no/[fingerprint])?
</span></span></code></pre></div><p>A <strong>fingerprint</strong> is a short hash of the key, easy to compare by eye. The honest way to answer is to compare it
with the fingerprint taken on the server itself, through some other channel you already trust (a console, a
colleague, your build records). On srv1, <code>ssh-keygen -lf</code> reads its own public host key:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">256 SHA256:D5T0vLnK5aqJqyXkl0nNrxpCcACGUqPLTgSGR0KO4Eo root@srv1 (ED25519)
</span></span></code></pre></div><p>They match, so it&rsquo;s srv1. Answer <code>yes</code> and the client saves the key in <code>~/.ssh/known_hosts</code>. Every later
connection checks the server against that saved key and doesn&rsquo;t ask again. This is called <strong>trust on first
use</strong>: the first answer is the one that counts. You can also paste the fingerprint itself as the answer, and
the client checks it for you.</p>
<p><code>ssh-keyscan srv1</code> fetches a server&rsquo;s public host keys without logging in, which is how automation fills
<code>known_hosts</code> for many machines. It saves you typing; it doesn&rsquo;t prove anything, because it trusts whatever
answers.</p>
<h3 id="why-does-known_hosts-look-like-gibberish">Why does known_hosts look like gibberish?</h3>
<p>Because Debian&rsquo;s client settings, in <code>/etc/ssh/ssh_config</code>, include <code>HashKnownHosts yes</code>. The client stores a
hash of each host name instead of the name, so someone who reads your file can&rsquo;t get a list of every server
you use. A line looks like this:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">|1|V5D+TWLxnaBV6Ck3cnAQOfkYWZA=|qeCTHIXQjdcQrnEdhx7ml2NDR5g= ssh-ed25519 AAAAC3NzaC1lZDI1N...
</span></span></code></pre></div><p>So don&rsquo;t search the file with <code>grep srv1</code>: it won&rsquo;t find anything. <code>ssh-keygen -F srv1</code> hashes the name and
finds the line for you. The <a href="https://man.openbsd.org/ssh-keygen">ssh-keygen(1) manual page</a> covers it.</p>
<hr>
<h2 id="what-does-remote-host-identification-has-changed-mean">What does &ldquo;REMOTE HOST IDENTIFICATION HAS CHANGED&rdquo; mean?</h2>
<p>It means the server offered a different host key from the one in your <code>known_hosts</code>. To show it here, this
lesson gave the client a throwaway <code>known_hosts</code> file holding the wrong key for srv1:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
</span></span><span class="line"><span class="cl">@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
</span></span><span class="line"><span class="cl">@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
</span></span><span class="line"><span class="cl">IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
</span></span><span class="line"><span class="cl">Someone could be eavesdropping on you right now (man-in-the-middle attack)!
</span></span><span class="line"><span class="cl">It is also possible that a host key has just been changed.
</span></span><span class="line"><span class="cl">The fingerprint for the ED25519 key sent by the remote host is
</span></span><span class="line"><span class="cl">SHA256:D5T0vLnK5aqJqyXkl0nNrxpCcACGUqPLTgSGR0KO4Eo.
</span></span><span class="line"><span class="cl">...
</span></span><span class="line"><span class="cl">Host key verification failed.
</span></span></code></pre></div><p>The client refuses to connect at all. There are two explanations, and the message names both:</p>
<table>
  <thead>
      <tr>
          <th>Cause</th>
          <th>How often</th>
          <th>What to do</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>the server was rebuilt or reinstalled, so it made new host keys</td>
          <td>almost always</td>
          <td>confirm the new fingerprint with whoever rebuilt it, then replace the old line</td>
      </tr>
      <tr>
          <td>something between you and the server is pretending to be it (a <strong>man-in-the-middle</strong>)</td>
          <td>rarely, and that&rsquo;s the case it exists for</td>
          <td>don&rsquo;t connect; find out what&rsquo;s answering</td>
      </tr>
  </tbody>
</table>
<p>The wrong fix is to
delete the whole <code>known_hosts</code> file, or to switch checking off: both throw away the protection for every
server, not just this one. The right fix removes only the one stale entry, and the full message (trimmed
above) tells you which line it is and how. Then connect again and check the new fingerprint, as on first use.</p>
<hr>
<h2 id="how-does-a-key-pair-log-you-in">How does a key pair log you in?</h2>
<p>A <strong>key pair</strong> is two files made together. The <strong>private key</strong> can sign things, and must stay secret. The
<strong>public key</strong> can only check those signatures, so anyone may have it. When you log in, the server sends a
challenge, your client signs it with the private key, and the server checks the signature with the public key
it already has. Your private key never crosses the network, and there&rsquo;s no password to steal or guess.</p>
<p><code>ssh-keygen</code> makes the pair. Here it is in the codespace, writing to a throwaway path (<code>-N ''</code> means no
passphrase, used only for this demo):</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ssh-keygen -t ed25519 -N &#39;&#39; -f /tmp/lesson6-demo/id_ed25519 -C demo@codespace
</span></span><span class="line"><span class="cl">Generating public/private ed25519 key pair.
</span></span><span class="line"><span class="cl">Your identification has been saved in /tmp/lesson6-demo/id_ed25519
</span></span><span class="line"><span class="cl">Your public key has been saved in /tmp/lesson6-demo/id_ed25519.pub
</span></span><span class="line"><span class="cl">The key fingerprint is:
</span></span><span class="line"><span class="cl">SHA256:Jc1YWRs4Z7TUlItc+zW2wdGVlMZ9wO3p3+qm02hmOHE demo@codespace
</span></span></code></pre></div><p>The two files, and the public key&rsquo;s one line:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">-rw------- 1 vscode vscode  411 Nov 11 05:55 id_ed25519
</span></span><span class="line"><span class="cl">-rw-r--r-- 1 vscode vscode   96 Nov 11 05:55 id_ed25519.pub
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIF9gi15jt5MNl7110z05ei2oPcxC5i0B7QzCMhXRQPQv demo@codespace
</span></span></code></pre></div><p>The private key is <code>600</code>, readable only by you, as <a href="/lessons/linux-cli-101/users-permissions-sudo/">lesson 4</a>
explained: SSH refuses a private key that others can read. The <code>.pub</code> file is one line: the key type, the key,
and a comment that&rsquo;s only a label.</p>
<p>To log in with it, the public key&rsquo;s line goes into <code>~/.ssh/authorized_keys</code> in your account on the server, one
line per key you trust. <code>ssh-copy-id</code> does that for you over a password login. On srv1 that file and its
folder are locked down as <code>sshd</code> expects:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">700 netops /home/netops/.ssh
</span></span><span class="line"><span class="cl">600 netops /home/netops/.ssh/authorized_keys
</span></span></code></pre></div><table>
  <thead>
      <tr>
          <th>File</th>
          <th>Where</th>
          <th>Holds</th>
          <th>Secret?</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>~/.ssh/id_ed25519</code></td>
          <td>your machine</td>
          <td>your private key</td>
          <td><strong>yes</strong>: never copy it, share it or commit it</td>
      </tr>
      <tr>
          <td><code>~/.ssh/id_ed25519.pub</code></td>
          <td>your machine</td>
          <td>your public key</td>
          <td>no</td>
      </tr>
      <tr>
          <td><code>~/.ssh/authorized_keys</code></td>
          <td>the server</td>
          <td>public keys allowed to log in as this user</td>
          <td>no, but only you may write it</td>
      </tr>
      <tr>
          <td><code>~/.ssh/known_hosts</code></td>
          <td>your machine</td>
          <td>servers&rsquo; public host keys</td>
          <td>no</td>
      </tr>
      <tr>
          <td><code>/etc/ssh/ssh_host_*_key</code></td>
          <td>the server</td>
          <td>the server&rsquo;s own private host keys</td>
          <td>yes</td>
      </tr>
  </tbody>
</table>
<p>A <strong>passphrase</strong> encrypts the private key file, so a stolen copy is useless without it. Use one on your own
laptop. The agent, below, means you type it once, not every time.</p>
<h3 id="why-does-the-lab-type--t-ed25519">Why does the lab type -t ed25519?</h3>
<p><strong>Ed25519</strong> is a modern key type: small, fast and strong. It&rsquo;s been ssh-keygen&rsquo;s default since OpenSSH 9.5,
whose <a href="https://www.openssh.org/txt/release-9.5">release notes</a> (2023-10-04) say &ldquo;generate Ed25519 keys by
default&rdquo;. The codespace runs OpenSSH 9.2, older than that, so there <code>ssh-keygen</code> with no <code>-t</code> still makes RSA:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ssh-keygen -N &#39;&#39; -f /tmp/lesson6-demo/default_key -C demo@codespace
</span></span><span class="line"><span class="cl">Generating public/private rsa key pair.
</span></span><span class="line"><span class="cl">...
</span></span><span class="line"><span class="cl">3072 SHA256:PSXemktsnew7YRkQIkYNXcNul6piSYKqv4qJ9/jqx08 demo@codespace (RSA)
</span></span></code></pre></div><p>A 3072-bit RSA key still works. But <code>-t ed25519</code> gives you the same key type on every machine, whatever its
OpenSSH version, and it&rsquo;s the type the lab&rsquo;s checker looks for.</p>
<hr>
<h2 id="what-does-permission-denied-publickeypassword-mean">What does &ldquo;Permission denied (publickey,password)&rdquo; mean?</h2>
<p>The server turned you away, and the brackets list the login methods it would have accepted. Here the
codespace tries ws1 with the demo key, which ws1 has never been given, in <strong>batch mode</strong>
(<code>-o BatchMode=yes</code>: never stop to ask a question, fail instead, which is how scripts run ssh):</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">netops@ws1: Permission denied (publickey,password).
</span></span></code></pre></div><p>The key was offered and refused, and batch mode wasn&rsquo;t allowed to fall back to a password prompt. The same
words appear when the key isn&rsquo;t in <code>authorized_keys</code>, when you&rsquo;ve given the wrong user name, or when the
server ignores <code>authorized_keys</code> because its permissions are too open. <code>ssh -v</code> shows each key the client
offers and the server&rsquo;s answer.</p>
<hr>
<h2 id="how-do-i-log-in-by-a-short-name-sshconfig">How do I log in by a short name? ~/.ssh/config</h2>
<p><code>~/.ssh/config</code> is your client&rsquo;s settings file. Each <code>Host</code> block gives a name you can type, and the settings
to use for it. The options are in <a href="https://man.openbsd.org/ssh_config">ssh_config(5)</a>. Here&rsquo;s a block in a
throwaway file that makes <code>web</code> mean &ldquo;srv1, as netops, with this key&rdquo;:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">Host web
</span></span><span class="line"><span class="cl">    HostName srv1
</span></span><span class="line"><span class="cl">    User netops
</span></span><span class="line"><span class="cl">    IdentityFile /tmp/lesson6-demo/id_ed25519
</span></span></code></pre></div><p><code>ssh -G web</code> prints the settings the client would actually use, without connecting. It&rsquo;s the quickest way to
check a block does what you meant:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">user netops
</span></span><span class="line"><span class="cl">hostname srv1
</span></span><span class="line"><span class="cl">port 22
</span></span><span class="line"><span class="cl">identityfile /tmp/lesson6-demo/id_ed25519
</span></span></code></pre></div><table>
  <thead>
      <tr>
          <th>Keyword</th>
          <th>Means</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>Host</code></td>
          <td>the name (or names, or a pattern like <code>*.lab</code>) this block applies to</td>
      </tr>
      <tr>
          <td><code>HostName</code></td>
          <td>the real name or address to connect to, if different</td>
      </tr>
      <tr>
          <td><code>User</code></td>
          <td>the user to log in as, so you can drop <code>user@</code></td>
      </tr>
      <tr>
          <td><code>Port</code></td>
          <td>if it&rsquo;s not 22</td>
      </tr>
      <tr>
          <td><code>IdentityFile</code></td>
          <td>which private key to offer</td>
      </tr>
  </tbody>
</table>
<p>The client reads the file from the top and, for each setting, keeps the <strong>first</strong> value it finds. Put
specific hosts first and a catch-all <code>Host *</code> last. Every tool that uses OpenSSH reads this file, so an alias
works for <code>scp</code>, Git and Ansible too.</p>
<p>A fresh install has no <code>~/.ssh/config</code> at all. If yours already has blocks, they&rsquo;re left from earlier work.</p>
<hr>
<h2 id="how-do-i-copy-a-file-over-ssh-scp">How do I copy a file over SSH? scp</h2>
<p><code>scp</code> copies files over the same connection, with the same keys, <code>known_hosts</code> and aliases. It reads like
<code>cp</code>, with a host name and a colon in front of a remote path. A path after the colon is relative to your home
folder on the other side, and nothing after the colon means the home folder itself. Forget the colon and <code>scp</code>
makes a local copy with a file name that looks like a host.</p>
<hr>
<h2 id="what-does-the-ssh-agent-do">What does the SSH agent do?</h2>
<p>The <strong>agent</strong>, <code>ssh-agent</code>, is a small background process (a process like the ones in
<a href="/lessons/linux-cli-101/processes-environment-path/">lesson 5</a>) that holds your unlocked private keys in memory.
You add a key once with <code>ssh-add</code>, typing its passphrase once, and every <code>ssh</code> after that asks the agent to
sign for it. The client finds the agent through an environment variable, <code>SSH_AUTH_SOCK</code>, so only shells that have
that variable can use it. Here&rsquo;s the demo key added to an agent and listed:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">Identity added: /tmp/lesson6-demo/id_ed25519 (demo@codespace)
</span></span><span class="line"><span class="cl">256 SHA256:Jc1YWRs4Z7TUlItc+zW2wdGVlMZ9wO3p3+qm02hmOHE demo@codespace (ED25519)
</span></span></code></pre></div><p><code>ssh-add -l</code> shows fingerprints, never the keys. The agent is what makes a passphrase bearable, and what lets
Ansible open fifty connections without fifty prompts.</p>
<hr>
<h2 id="why-do-keys-beat-passwords-for-automation">Why do keys beat passwords for automation?</h2>
<table>
  <thead>
      <tr>
          <th></th>
          <th>Password</th>
          <th>Key</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>a script can use it without a human</td>
          <td>only by storing the password somewhere</td>
          <td>yes, through the agent or an unencrypted key kept for that job</td>
      </tr>
      <tr>
          <td>what crosses the network</td>
          <td>the password itself, encrypted</td>
          <td>a signature; the private key never leaves</td>
      </tr>
      <tr>
          <td>guessable</td>
          <td>yes, and servers get guessed at all day</td>
          <td>no</td>
      </tr>
      <tr>
          <td>revoke one machine&rsquo;s access</td>
          <td>change the password everywhere it&rsquo;s used</td>
          <td>delete one line from <code>authorized_keys</code></td>
      </tr>
      <tr>
          <td>one per person or job</td>
          <td>rarely, in practice</td>
          <td>easily: one key each, each with its own comment</td>
      </tr>
  </tbody>
</table>
<p>Servers defend themselves against guessing too. Since <a href="https://www.openssh.org/txt/release-9.8">OpenSSH 9.8</a>
(2024-07-01), <code>sshd</code> penalises client addresses that don&rsquo;t complete a login, through a <code>PerSourcePenalties</code>
setting that&rsquo;s &ldquo;on by default&rdquo;. srv1&rsquo;s effective settings show it:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">persourcepenalties crash:90 authfail:5 noauth:1 grace-exceeded:10 refuseconnection:10 max:600 min:15 ...
</span></span></code></pre></div><p>A script that keeps retrying a wrong password can get its own address refused for a while. With keys, that
doesn&rsquo;t happen.</p>
<p>That&rsquo;s why <a href="/lessons/ansible-101/">Ansible 101</a> logs in to Linux hosts with keys, why
<span class="link-soon">GitOps</span> pushes with one, and why Python 101&rsquo;s
<a href="/lessons/python-101/netmiko/">Netmiko lesson</a> keeps device passwords in environment variables rather than in
code. Many network devices, like r1 here, still use passwords, so those tools also keep passwords out of your files.</p>
<hr>
<h2 id="what-i-see-go-wrong">What I see go wrong</h2>
<p><strong>A private key others can read.</strong> The client won&rsquo;t use it, and says so in capitals. <code>chmod 600</code>, from
lesson 4.</p>
<p><strong>A key that&rsquo;s in authorized_keys and still refused.</strong> The server ignores the file if <code>~/.ssh</code> or
<code>authorized_keys</code> can be written by anyone but you. Or the key went into the wrong user&rsquo;s home folder.</p>
<p><strong>Wrong user.</strong> Without <code>user@</code> or a <code>User</code> line, ssh uses your local user name (<code>vscode</code> in the codespace),
which doesn&rsquo;t exist on the server.</p>
<p><strong>Deleting all of known_hosts after one rebuild.</strong> It works, and it throws away every other server&rsquo;s key with
it. Remove only the stale entry.</p>
<p><strong><code>StrictHostKeyChecking no</code> in a config &ldquo;to make it work&rdquo;.</strong> It turns off the one check that tells you you&rsquo;ve
reached the right machine. Scan and verify the keys instead.</p>
<p><strong>The private key in Git.</strong> Once pushed, assume it&rsquo;s public: remove its line from every <code>authorized_keys</code> and
make a new pair. <a href="/posts/donthardcodepasswords/">Don&rsquo;t hardcode passwords</a> applies to keys too.</p>
<p><strong>Copying the wrong file.</strong> <code>authorized_keys</code> takes the <code>.pub</code> file&rsquo;s line. If you&rsquo;re about to paste something
that starts with <code>-----BEGIN OPENSSH PRIVATE KEY-----</code>, stop.</p>
<hr>
<h2 id="try-it">Try it</h2>
<p><a href="/labs/linux-cli-101/task-6-1/">Chapter 6 of the lab</a> has you log in to ws1 with a password, check and accept
its host key, make an ed25519 key pair and log in again without a password. Then you give ws1, srv1 and r1
short names, copy a file to srv1, and meet &ldquo;host key changed&rdquo; for real when srv1 is rebuilt.</p>
<h2 id="whats-next">What&rsquo;s next</h2>
<p>Typing the same SSH command for every host gets old fast. <a href="/lessons/linux-cli-101/bash-scripting-basics/">Lesson 7</a>
turns commands you&rsquo;d type into a script: a loop over a list of hosts, and an exit code that says whether they
all answered.</p>
]]></content:encoded></item><item><title>Processes, Environment Variables and PATH: Finding What Runs (Linux CLI 101, Lesson 5)</title><link>https://networkdoodles.com/lessons/linux-cli-101/processes-environment-path/</link><pubDate>Mon, 16 Nov 2026 09:00:00 -0500</pubDate><dc:creator>David Henderson</dc:creator><guid>https://networkdoodles.com/lessons/linux-cli-101/processes-environment-path/</guid><category>linux</category><category>networking-basics</category><category>network-automation</category><category>troubleshooting</category><description>Processes and PIDs, background jobs, ps and top, what TERM and KILL do, how exported environment variables reach child processes, and how PATH finds a command.</description><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<p>A <strong>process</strong> is a program that&rsquo;s running, and Linux gives every one a number, its <strong>PID</strong> (process ID). You
find processes with <code>ps</code> and <code>top</code>, and you stop one by sending it a <strong>signal</strong>: <code>kill</code> sends TERM, a polite
request to stop, and <code>kill -9</code> sends KILL, which no process can refuse. Each process also carries a set of
<strong>environment variables</strong>, named values it hands down to the processes it starts. One of them, <code>PATH</code>, is the
list of folders the shell searches when you type a command name.</p>
<p>Those three ideas explain why a script can&rsquo;t see a variable you just set, why a tool you installed says
&ldquo;command not found&rdquo;, and what to do when something is eating a CPU. They come back in every later course:
Ansible reads settings from environment variables, and Python&rsquo;s venv works by changing <code>PATH</code>.</p>
<hr>
<h2 id="what-is-a-process-and-whats-a-pid">What is a process, and what&rsquo;s a PID?</h2>
<p>When you type <code>sleep 300</code> and press <kbd>Enter</kbd>, the shell asks the Linux kernel to start the <code>sleep</code>
program. The kernel loads it, gives it a PID and some memory, and lets it run. That running copy is a
process. Run <code>sleep</code> twice and you have two processes of one program, each with its own PID.</p>
<p>Every process except the very first has a <strong>parent</strong>: the process that started it. Your shell is the parent
of every command you type. Here&rsquo;s the codespace&rsquo;s shell and a <code>sleep</code> it started in the background, as <code>ps</code>
shows them:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">  PID  PPID STAT     ELAPSED COMMAND
</span></span><span class="line"><span class="cl">35250     0 Ss         00:00 bash
</span></span><span class="line"><span class="cl">35256 35250 S          00:00 sleep
</span></span></code></pre></div><table>
  <thead>
      <tr>
          <th>Column</th>
          <th>Means</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>PID</code></td>
          <td>this process&rsquo;s number</td>
      </tr>
      <tr>
          <td><code>PPID</code></td>
          <td>its parent&rsquo;s PID: <code>sleep</code>&rsquo;s parent, 35250, is the <code>bash</code> above it (that <code>bash</code> shows 0 because it was started from outside the container)</td>
      </tr>
      <tr>
          <td><code>STAT</code></td>
          <td>its state: <code>R</code> running, <code>S</code> sleeping (waiting for something), <code>T</code> stopped, <code>Z</code> a zombie (finished, but its parent hasn&rsquo;t collected it yet)</td>
      </tr>
      <tr>
          <td><code>ELAPSED</code></td>
          <td>how long it has been running</td>
      </tr>
      <tr>
          <td><code>COMMAND</code></td>
          <td>the program</td>
      </tr>
  </tbody>
</table>
<p>Your PIDs will be different, and a PID is reused after its process ends, so look one up just before you act on it.</p>
<hr>
<h2 id="whats-the-difference-between-foreground-and-background">What&rsquo;s the difference between foreground and background?</h2>
<p>A command runs in the <strong>foreground</strong> by default: the shell waits for it to finish before it gives you the
prompt back. That&rsquo;s what you want for <code>ls</code>. It&rsquo;s not what you want for a capture or a long copy that takes ten
minutes.</p>
<p>Put <code>&amp;</code> at the end of a command and it runs in the <strong>background</strong>: the shell starts it, prints its job number
and PID, and gives you the prompt straight away. The shell&rsquo;s own list of what it started is called its
<strong>jobs</strong>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ sleep 300 &amp;
</span></span><span class="line"><span class="cl">[1] 36465
</span></span><span class="line"><span class="cl">$ jobs -l
</span></span><span class="line"><span class="cl">[1]+ 36465 Running                 sleep 300 &amp;
</span></span></code></pre></div><p><code>[1]</code> is the job number, which only your shell knows. <code>36465</code> is the PID, which the whole system knows.
<code>kill %1</code> stops a job by its number; <code>kill 36465</code> does the same by PID.</p>
<table>
  <thead>
      <tr>
          <th>You want to</th>
          <th>Do this</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>start something in the background</td>
          <td>end the command with <code>&amp;</code></td>
      </tr>
      <tr>
          <td>pause the foreground command</td>
          <td><kbd>Ctrl</kbd>+<kbd>Z</kbd></td>
      </tr>
      <tr>
          <td>list this shell&rsquo;s jobs</td>
          <td><code>jobs</code> (<code>jobs -l</code> adds PIDs)</td>
      </tr>
      <tr>
          <td>bring a job back to the foreground</td>
          <td><code>fg %1</code></td>
      </tr>
      <tr>
          <td>let a paused job carry on in the background</td>
          <td><code>bg %1</code></td>
      </tr>
      <tr>
          <td>stop the foreground command</td>
          <td><kbd>Ctrl</kbd>+<kbd>C</kbd></td>
      </tr>
  </tbody>
</table>
<p><kbd>Ctrl</kbd>+<kbd>Z</kbd> and <kbd>Ctrl</kbd>+<kbd>C</kbd> look alike and do very different things.
<kbd>Ctrl</kbd>+<kbd>Z</kbd> <strong>pauses</strong>: the process is still there, holding its memory, doing nothing, until
you <code>fg</code> or <code>bg</code> it. <kbd>Ctrl</kbd>+<kbd>C</kbd> asks it to <strong>stop</strong>. Background jobs belong to the shell that started them. Close that terminal and they normally go with it.</p>
<hr>
<h2 id="how-do-i-find-the-process-thats-eating-a-cpu">How do I find the process that&rsquo;s eating a CPU?</h2>
<p>Ask for every process, sorted by CPU use. <code>ps -e</code> lists every process on the system, not just yours, and
<code>--sort=-pcpu</code> puts the busiest first:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ps -eo pid,user,pcpu,pmem,comm --sort=-pcpu | head -6
</span></span><span class="line"><span class="cl">  PID USER     %CPU %MEM COMMAND
</span></span><span class="line"><span class="cl">33433 vscode    200  0.0 ps
</span></span><span class="line"><span class="cl">   26 root     28.5  1.4 dockerd
</span></span><span class="line"><span class="cl">33423 vscode    2.6  0.0 bash
</span></span><span class="line"><span class="cl">26452 root      2.5  0.3 capeosd
</span></span><span class="line"><span class="cl">   37 root      2.0  0.7 containerd
</span></span></code></pre></div><p><code>%CPU</code> is per core, so it can pass 100 on a machine with several cores. And <code>ps</code> averages it over the
process&rsquo;s lifetime, so a command that has only just started (<code>ps</code> itself, here) can look wildly busy. For
&ldquo;what&rsquo;s busy right now&rdquo;, use <code>top</code>.</p>
<p><code>top</code> is a live view that refreshes every few seconds and sorts by current CPU use. Here&rsquo;s one screen of it from
the codespace, taken with <code>top -b -n1</code> (batch mode, one round, so it can be shown here):</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">top - 05:56:30 up 3 days,  7:01,  0 user,  load average: 12.30, 7.90, 7.51
</span></span><span class="line"><span class="cl">Tasks:  46 total,   1 running,  24 sleeping,   0 stopped,  21 zombie
</span></span><span class="line"><span class="cl">%Cpu(s):  5.1 us, 43.6 sy,  0.0 ni, 48.7 id,  2.6 wa,  0.0 hi,  0.0 si,  0.0 st
</span></span><span class="line"><span class="cl">MiB Mem :   7837.1 total,    242.9 free,   1890.9 used,   5971.7 buff/cache
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
</span></span><span class="line"><span class="cl">26452 root      20   0   35820  28584   9012 S   6.2   0.4   0:07.17 capeosd
</span></span><span class="line"><span class="cl">33471 vscode    20   0   12064   4784   2736 R   6.2   0.1   0:00.03 top
</span></span></code></pre></div><p>The <strong>load average</strong> is how many processes, on average, were running or waiting to run over the last 1, 5
and 15 minutes. Compare it with the number of cores (<code>nproc</code>): a load of 2 on a 2-core codespace means it&rsquo;s
fully busy. In the list, <code>PID</code>, <code>%CPU</code>, <code>TIME+</code> (total CPU time used) and <code>COMMAND</code> are the columns that find a
runaway.</p>
<hr>
<h2 id="what-does-kill-actually-do-term-vs-kill">What does kill actually do? TERM vs KILL</h2>
<p><code>kill</code> doesn&rsquo;t kill. It sends a <strong>signal</strong>: a small numbered message from the kernel to a process. The
process can have a <strong>handler</strong> for most signals, a piece of its own code that runs when the signal arrives,
or it can ignore them. The <a href="https://man7.org/linux/man-pages/man7/signal.7.html">signal(7) manual page</a> lists
them all. These are the ones you&rsquo;ll use:</p>
<table>
  <thead>
      <tr>
          <th>Signal</th>
          <th>Number</th>
          <th>Sent by</th>
          <th>What it asks</th>
          <th>Can it be ignored?</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>TERM</code></td>
          <td>15</td>
          <td><code>kill PID</code> (the default)</td>
          <td>please finish up and exit</td>
          <td>yes</td>
      </tr>
      <tr>
          <td><code>INT</code></td>
          <td>2</td>
          <td><kbd>Ctrl</kbd>+<kbd>C</kbd></td>
          <td>interrupt what you&rsquo;re doing</td>
          <td>yes</td>
      </tr>
      <tr>
          <td><code>HUP</code></td>
          <td>1</td>
          <td>the terminal closing</td>
          <td>your terminal has gone</td>
          <td>yes</td>
      </tr>
      <tr>
          <td><code>KILL</code></td>
          <td>9</td>
          <td><code>kill -9 PID</code> or <code>kill -KILL PID</code></td>
          <td>nothing: the kernel ends it</td>
          <td><strong>no</strong></td>
      </tr>
      <tr>
          <td><code>STOP</code></td>
          <td>19</td>
          <td><code>kill -STOP PID</code></td>
          <td>pause, like <kbd>Ctrl</kbd>+<kbd>Z</kbd></td>
          <td><strong>no</strong></td>
      </tr>
  </tbody>
</table>
<p>Send TERM first, because it lets a program clean up: close its files, remove its lock file, write its last
log line. KILL gives it no chance. The kernel just removes it, mid-write if that&rsquo;s where it was. signal(7) puts it plainly: SIGKILL and SIGSTOP &ldquo;cannot be
caught, blocked, or ignored&rdquo;.</p>
<p>Here&rsquo;s a small script, written for this lesson, that spins one CPU and ignores TERM. It&rsquo;s started in the
background, sent TERM, then sent KILL:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ps -o pid,stat,pcpu,etime,cmd -p 33680
</span></span><span class="line"><span class="cl">  PID STAT %CPU     ELAPSED CMD
</span></span><span class="line"><span class="cl">33680 R     100       00:02 bash /tmp/lesson5-stubborn
</span></span><span class="line"><span class="cl">$ kill 33680
</span></span><span class="line"><span class="cl">$ ps -o pid,stat,pcpu,cmd -p 33680
</span></span><span class="line"><span class="cl">  PID STAT %CPU CMD
</span></span><span class="line"><span class="cl">33680 R    99.0 bash /tmp/lesson5-stubborn
</span></span><span class="line"><span class="cl">$ kill -9 33680
</span></span><span class="line"><span class="cl">bash: line 6: 33680 Killed                  /tmp/lesson5-stubborn
</span></span><span class="line"><span class="cl">$ ps -p 33680 -o pid,cmd
</span></span><span class="line"><span class="cl">  PID CMD
</span></span></code></pre></div><p>After TERM it&rsquo;s still there, still at 99% CPU. <code>kill</code> printed nothing, because sending the signal worked; the
process simply chose to ignore it. After KILL it&rsquo;s gone, and <code>ps</code> finds nothing. The shell reported its exit
status as 137, which is 128 plus 9: &ldquo;ended by signal 9&rdquo;. You&rsquo;ll meet exit codes properly in
<a href="/lessons/linux-cli-101/bash-scripting-basics/">lesson 7</a>.</p>
<p>The habit is: <strong>TERM, wait a few seconds, check, and only then KILL.</strong> Reaching for <code>kill -9</code> first is how you
end up with half-written files and lock files that stop the program starting again.</p>
<hr>
<h2 id="what-is-an-environment-variable">What is an environment variable?</h2>
<p>An <strong>environment variable</strong> is a named value, like <code>HOME=/home/vscode</code>, that a process carries with it and
that programs can read. <code>env</code> lists the ones your shell has; <code>echo &quot;$HOME&quot;</code> prints one. They tell a program something without changing its code or files.</p>
<p>The shell also has plain <strong>shell variables</strong>, which stay inside the shell. The difference is the whole point
of <code>export</code>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ GREETING=hi
</span></span><span class="line"><span class="cl">$ bash -c &#39;echo child sees: [$GREETING]&#39;
</span></span><span class="line"><span class="cl">child sees: []
</span></span><span class="line"><span class="cl">$ export GREETING
</span></span><span class="line"><span class="cl">$ bash -c &#39;echo child sees: [$GREETING]&#39;
</span></span><span class="line"><span class="cl">child sees: [hi]
</span></span></code></pre></div><p><code>GREETING=hi</code> set a shell variable. The second <code>bash</code>, a <strong>child process</strong> started by the first, couldn&rsquo;t see
it. After <code>export</code>, the shell put it in its environment, and every child it starts from then on gets a copy.</p>
<p>Three rules follow, and they explain most &ldquo;but I set it!&rdquo; problems:</p>
<ul>
<li><strong>Children get a copy, not a link.</strong> A child that changes the variable changes only its own copy. Nothing a
script does to its environment comes back to the shell that ran it.</li>
<li><strong>It goes down, never sideways.</strong> A variable exported in one terminal tab doesn&rsquo;t exist in another. Each
terminal runs its own shell.</li>
<li><strong>It lasts as long as the shell.</strong> Close the terminal and it&rsquo;s gone. To have it in every new terminal, put
the <code>export</code> line in a startup file (below).</li>
</ul>
<h3 id="why-do-secrets-go-in-environment-variables">Why do secrets go in environment variables?</h3>
<p>Because a file you commit to Git is shared with everyone who can read the repository, forever, history
included. A password in an environment variable lives only in the memory of the processes that need it. That&rsquo;s
why Python 101 has Netmiko read credentials from the environment, and why
<a href="/posts/donthardcodepasswords/">Don&rsquo;t hardcode passwords</a> and Ansible 101&rsquo;s
<a href="/lessons/ansible-101/secrets/">lesson on secrets</a> take the same line. They aren&rsquo;t a vault (any process running as you can read them), but they&rsquo;re far better than a password in code.</p>
<hr>
<h2 id="how-does-the-shell-find-a-command-path">How does the shell find a command? PATH</h2>
<p>When you type <code>ssh</code>, the shell needs a file to run. It doesn&rsquo;t search the whole disk. It reads the
environment variable <strong><code>PATH</code></strong>, a list of folders separated by colons, and looks in each in turn. The
first folder holding an executable file called <code>ssh</code> wins. Here&rsquo;s the codespace&rsquo;s <code>PATH</code>, one folder per line:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ echo &#34;$PATH&#34; | tr &#34;:&#34; &#34;\n&#34;
</span></span><span class="line"><span class="cl">/home/vscode/.venvs/lab/bin
</span></span><span class="line"><span class="cl">/home/vscode/.local/bin
</span></span><span class="line"><span class="cl">/home/vscode/.atuin/bin
</span></span><span class="line"><span class="cl">/usr/local/python/current/bin
</span></span><span class="line"><span class="cl">/usr/local/py-utils/bin
</span></span><span class="line"><span class="cl">/usr/local/jupyter
</span></span><span class="line"><span class="cl">/usr/local/share/nvm/current/bin
</span></span><span class="line"><span class="cl">/usr/local/bin
</span></span><span class="line"><span class="cl">/usr/local/sbin
</span></span><span class="line"><span class="cl">/usr/local/bin
</span></span><span class="line"><span class="cl">/usr/sbin
</span></span><span class="line"><span class="cl">/usr/bin
</span></span><span class="line"><span class="cl">/sbin
</span></span><span class="line"><span class="cl">/bin
</span></span></code></pre></div><p>Order matters. The lab&rsquo;s Python venv is first, which is how activating a venv works: it puts its own <code>bin</code>
folder in front, so its <code>python</code> is found before the system&rsquo;s. (Python 101 picks this up.)</p>
<p>To ask which file a name resolves to, use <code>type</code>, which knows everything the shell knows, or <code>which</code>, which
only searches <code>PATH</code>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ type cd ls ssh
</span></span><span class="line"><span class="cl">cd is a shell builtin
</span></span><span class="line"><span class="cl">ls is /usr/bin/ls
</span></span><span class="line"><span class="cl">ssh is /usr/bin/ssh
</span></span></code></pre></div><p><code>cd</code> isn&rsquo;t a file at all. It&rsquo;s a <strong>builtin</strong>, part of Bash itself, because changing the shell&rsquo;s folder is
something only the shell can do to itself.</p>
<p>A command that isn&rsquo;t in any <code>PATH</code> folder isn&rsquo;t found, even if the file is sitting right there. Here&rsquo;s a
two-line script, <code>hello5</code>, in a folder that&rsquo;s not on <code>PATH</code>, then the same name after that folder is added
to the front:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ hello5
</span></span><span class="line"><span class="cl">bash: line 1: hello5: command not found
</span></span><span class="line"><span class="cl">$ PATH=&#34;/tmp/lesson5-tools:$PATH&#34;
</span></span><span class="line"><span class="cl">$ hello5
</span></span><span class="line"><span class="cl">hello from my own command
</span></span><span class="line"><span class="cl">$ type hello5
</span></span><span class="line"><span class="cl">hello5 is hashed (/tmp/lesson5-tools/hello5)
</span></span></code></pre></div><p>&ldquo;Hashed&rdquo; means Bash remembered where it found it. A name with a <code>/</code> in it, such as <code>./hello5</code>, skips the
<code>PATH</code> search entirely: that&rsquo;s why you type <code>./</code> to run a script in the current folder.</p>
<p>Exit code 127 is the shell&rsquo;s code for &ldquo;command not found&rdquo;. When a script fails with 127, check <code>PATH</code> first.</p>
<hr>
<h2 id="where-do-settings-for-every-new-terminal-go-bashrc">Where do settings for every new terminal go? ~/.bashrc</h2>
<p>A variable you set or a <code>PATH</code> you change lasts until the terminal closes. To have it in every new terminal,
add the line to a startup file that Bash reads when it starts:</p>
<table>
  <thead>
      <tr>
          <th>File</th>
          <th>Read by</th>
          <th>Use it for</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>~/.bashrc</code></td>
          <td>every new interactive shell, such as each terminal tab in the codespace</td>
          <td><code>export</code> lines, <code>PATH</code> changes, aliases</td>
      </tr>
      <tr>
          <td><code>~/.profile</code></td>
          <td>login shells, such as when you log in over SSH</td>
          <td>settings for a login; on Debian it also reads <code>~/.bashrc</code> when Bash is the shell</td>
      </tr>
  </tbody>
</table>
<p>The <a href="https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html">GNU Bash manual&rsquo;s section on startup files</a>
has the exact rules. For this course, <code>~/.bashrc</code> is the one you edit.</p>
<p>Debian&rsquo;s default <code>~/.profile</code> already adds <code>~/bin</code> to <code>PATH</code> for login shells, if that folder exists. The lab
uses a different folder, <code>~/tools</code>, so you add it yourself and see how it works.</p>
<p>Editing <code>~/.bashrc</code> changes nothing in the terminal you already have open: it&rsquo;s only read when a shell starts.
Open a new terminal, or run <code>source ~/.bashrc</code> to read it into the current one. The checker always tests a new terminal.</p>
<hr>
<h2 id="what-i-see-go-wrong">What I see go wrong</h2>
<p><strong>&ldquo;I set it, but the script can&rsquo;t see it.&rdquo;</strong> It&rsquo;s a shell variable, not exported. Use <code>export NAME=value</code>.</p>
<p><strong>&ldquo;I exported it, but the other tab can&rsquo;t see it.&rdquo;</strong> Each terminal is its own shell. Put it in <code>~/.bashrc</code>
and open a new terminal.</p>
<p><strong>&ldquo;I added it to ~/.bashrc and nothing changed.&rdquo;</strong> The file is read when a shell starts. Open a new terminal.</p>
<p><strong>&ldquo;command not found&rdquo;, but the file is right there.</strong> Its folder isn&rsquo;t on <code>PATH</code>, or the file isn&rsquo;t
executable (<code>chmod +x</code>, from <a href="/lessons/linux-cli-101/users-permissions-sudo/">lesson 4</a>). <code>type name</code> tells
you what the shell would run.</p>
<p><strong>The wrong version runs.</strong> Two folders on <code>PATH</code> have a file of that name, and the earlier one wins.
<code>type -a name</code> lists every match, in order.</p>
<p><strong>A broken PATH.</strong> <code>PATH=/home/me/tools</code> without <code>:$PATH</code> replaces the whole list, and suddenly <code>ls</code> is &ldquo;not
found&rdquo;. Always add to it: <code>PATH=&quot;$HOME/tools:$PATH&quot;</code>. If you&rsquo;ve done it in a terminal, close that terminal.
If you&rsquo;ve done it in <code>~/.bashrc</code>, fix the file with its full path, such as <code>/usr/bin/nano ~/.bashrc</code>.</p>
<p><strong><code>kill -9</code> first.</strong> It works, and it skips the program&rsquo;s clean-up. Send TERM, wait, check with <code>ps</code>, then
KILL.</p>
<hr>
<h2 id="try-it">Try it</h2>
<p><a href="/labs/linux-cli-101/task-5-1/">Chapter 5 of the lab</a> has you start and pause jobs, read <code>ps</code> and <code>top</code>, set
a variable that every new terminal and the programs it starts can see, then find a runaway process that
doesn&rsquo;t stop when it&rsquo;s asked, and make a command of your own runnable from any folder.</p>
<h2 id="whats-next">What&rsquo;s next</h2>
<p>So far everything has run on one machine. <a href="/lessons/linux-cli-101/ssh-and-keys/">Lesson 6</a> is about getting a
shell on another one with SSH: host keys, key pairs, and logging in by a short name with no password.</p>
]]></content:encoded></item><item><title>Users, Permissions and sudo: Reading ls -l (Linux CLI 101, Lesson 4)</title><link>https://networkdoodles.com/lessons/linux-cli-101/users-permissions-sudo/</link><pubDate>Mon, 16 Nov 2026 09:00:00 -0500</pubDate><dc:creator>David Henderson</dc:creator><guid>https://networkdoodles.com/lessons/linux-cli-101/users-permissions-sudo/</guid><category>linux</category><category>networking-basics</category><category>security</category><category>devops</category><description>Owners, groups and the nine permission bits in ls -l, chmod with 600, 644 and 755, why SSH refuses a key others can read, and when sudo and apt need root.</description><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<p>Every file on a Linux system belongs to one <strong>user</strong> and one <strong>group</strong>, and carries nine permission bits: read,
write and execute (<code>rwx</code>), once for the owner, once for the group and once for everyone else. <code>ls -l</code> shows them
as letters, like <code>-rw-r--r--</code>; <code>chmod</code> sets them, usually as three digits: <code>644</code> for a normal file, <code>755</code> for a
program or folder, <code>600</code> for anything private. <strong>root</strong> is the one user the rules don&rsquo;t apply to, and <code>sudo</code> is
how you borrow root&rsquo;s power for one command, which is what installing software with <code>apt</code> needs.</p>
<p>You&rsquo;ll meet this the first time SSH refuses your key, a script won&rsquo;t run, or a tool says &ldquo;Permission denied&rdquo;.
Each of those is a permission bit, and each takes a few seconds to fix once you can read <code>ls -l</code>.</p>
<hr>
<h2 id="what-are-users-and-groups">What are users and groups?</h2>
<p>A <strong>user</strong> is an identity the system checks before it lets anything happen. Every process runs as some user,
and every file is owned by one. To the kernel, a user is a number, the <strong>UID</strong> (user ID). The name is just a
label for it, kept in <code>/etc/passwd</code>.</p>
<p>A <strong>group</strong> is a named set of users, with its own number, the <strong>GID</strong>. Groups let you give several people the
same access without listing each of them on every file.</p>
<p><code>id</code> shows who you are. In the lab&rsquo;s codespace:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ id
</span></span><span class="line"><span class="cl">uid=1000(vscode) gid=1000(vscode) groups=1000(vscode),104(docker),997(clab_admins),998(pipx),999(nvm),1001(ssh)
</span></span></code></pre></div><p>You&rsquo;re user <code>vscode</code>, number 1000. Your <strong>primary group</strong> (the one new files get) is also called <code>vscode</code>, and
you&rsquo;re in a few more. Being in the <code>docker</code> group is what lets you run <code>docker</code> without asking for root.</p>
<p>On ws1, the lab&rsquo;s workstation, the same command says something different:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ id
</span></span><span class="line"><span class="cl">uid=1000(netops) gid=1000(netops) groups=1000(netops)
</span></span></code></pre></div><p>Same number, different name. Users are <strong>local to each machine</strong>: UID 1000 on the codespace and UID 1000 on
ws1 have nothing to do with each other. That matters later, when you copy files between machines with SSH
(<a href="/lessons/linux-cli-101/ssh-and-keys/">lesson 6</a>).</p>
<p>Each line of <code>/etc/passwd</code> is one user, with fields split by colons:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">root:x:0:0:root:/root:/bin/bash
</span></span><span class="line"><span class="cl">vscode:x:1000:1000::/home/vscode:/bin/bash
</span></span></code></pre></div><p>Name, a placeholder <code>x</code> (the password hash lives in <code>/etc/shadow</code>, which only root can read), UID, GID, a
comment, the home folder and the login shell.</p>
<hr>
<h2 id="how-do-i-read-ls--l">How do I read ls -l?</h2>
<p><code>ls -l</code> prints one line per file. The first ten characters are the type and the permissions:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ls -l
</span></span><span class="line"><span class="cl">-rw-r--r-- 1 vscode vscode  3 Nov 11 05:56 notes.txt
</span></span><span class="line"><span class="cl">drwxr-xr-x 2 vscode vscode 40 Nov 11 05:56 reports
</span></span></code></pre></div><p>Take <code>-rw-r--r--</code> apart:</p>
<table>
  <thead>
      <tr>
          <th>Characters</th>
          <th><code>-</code></th>
          <th><code>rw-</code></th>
          <th><code>r--</code></th>
          <th><code>r--</code></th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Meaning</td>
          <td>type</td>
          <td><strong>user</strong> (the owner)</td>
          <td><strong>group</strong></td>
          <td><strong>other</strong> (everyone else)</td>
      </tr>
      <tr>
          <td>Here</td>
          <td>a regular file</td>
          <td>read, write</td>
          <td>read</td>
          <td>read</td>
      </tr>
  </tbody>
</table>
<p>The type is <code>-</code> for a file and <code>d</code> for a directory. Then three sets of three, always in the order <code>r</code>, <code>w</code>, <code>x</code>.
A letter means the permission is there; a dash means it isn&rsquo;t.</p>
<p>After the permissions come a link count (ignore it for now), the <strong>owner</strong> (<code>vscode</code>), the <strong>group</strong>
(<code>vscode</code>), the size in bytes, the time it was last changed, and the name.</p>
<p>When you try to open a file, Linux checks <strong>one</strong> set: the user set if you own it, otherwise the group set if
you&rsquo;re in its group, otherwise the other set. It doesn&rsquo;t add them up. If you own a file with <code>---r--r--</code>, you
can&rsquo;t read it, even though everyone else can.</p>
<hr>
<h2 id="what-do-r-w-and-x-mean-on-files-and-on-folders">What do r, w and x mean on files and on folders?</h2>
<p>The same three letters mean slightly different things on a directory:</p>
<table>
  <thead>
      <tr>
          <th>Bit</th>
          <th>On a file</th>
          <th>On a directory</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>r</code> read</td>
          <td>read its contents</td>
          <td>list the names in it</td>
      </tr>
      <tr>
          <td><code>w</code> write</td>
          <td>change its contents</td>
          <td>create, delete and rename files in it</td>
      </tr>
      <tr>
          <td><code>x</code> execute</td>
          <td>run it as a program</td>
          <td>go into it, and reach the files inside</td>
      </tr>
  </tbody>
</table>
<p>Two consequences catch people out. Deleting a file needs <code>w</code> on the <strong>folder</strong>, not on the file. And a folder
without <code>x</code> is a locked door, even if you can read the names through the keyhole. Here&rsquo;s a folder set to <code>600</code>
(read and write, no execute) by its own owner:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ls -l locked
</span></span><span class="line"><span class="cl">ls: cannot access &#39;locked/f&#39;: Permission denied
</span></span><span class="line"><span class="cl">total 0
</span></span><span class="line"><span class="cl">-????????? ? ? ? ?            ? f
</span></span><span class="line"><span class="cl">$ cat locked/f
</span></span><span class="line"><span class="cl">cat: locked/f: Permission denied
</span></span></code></pre></div><p><code>ls</code> could read the name <code>f</code>, because of <code>r</code>, but not look at the file, because there&rsquo;s no <code>x</code>. That&rsquo;s why
folders are almost always <code>755</code> or <code>700</code>, never <code>644</code> or <code>600</code>.</p>
<hr>
<h2 id="what-do-644-755-and-600-mean">What do 644, 755 and 600 mean?</h2>
<p>Each permission is worth a number: <strong>r = 4, w = 2, x = 1</strong>. Add them up for each set and you get one digit per
set, user then group then other.</p>
<table>
  <thead>
      <tr>
          <th>Mode</th>
          <th>Letters</th>
          <th>Who can do what</th>
          <th>Use it for</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>644</code></td>
          <td><code>rw-r--r--</code></td>
          <td>you read and write; everyone reads</td>
          <td>ordinary files, configs, notes</td>
      </tr>
      <tr>
          <td><code>755</code></td>
          <td><code>rwxr-xr-x</code></td>
          <td>you do anything; everyone reads and runs</td>
          <td>scripts, programs, folders</td>
      </tr>
      <tr>
          <td><code>600</code></td>
          <td><code>rw-------</code></td>
          <td>only you, read and write</td>
          <td>private keys, files with passwords</td>
      </tr>
      <tr>
          <td><code>700</code></td>
          <td><code>rwx------</code></td>
          <td>only you, everything</td>
          <td>private folders, like <code>~/.ssh</code></td>
      </tr>
      <tr>
          <td><code>666</code></td>
          <td><code>rw-rw-rw-</code></td>
          <td>everyone reads and writes</td>
          <td>almost never what you want</td>
      </tr>
      <tr>
          <td><code>777</code></td>
          <td><code>rwxrwxrwx</code></td>
          <td>everyone does anything</td>
          <td>never: it&rsquo;s the &ldquo;I gave up&rdquo; mode</td>
      </tr>
  </tbody>
</table>
<p>So <code>6</code> is <code>4 + 2</code>, read and write; <code>5</code> is <code>4 + 1</code>, read and execute; <code>7</code> is all three.</p>
<p><code>chmod</code> (change mode) sets them. You can give the number, which sets all nine bits at once, or letters, which
change only what you name: <code>u</code>, <code>g</code>, <code>o</code> for user, group and other (<code>a</code> for all), then <code>+</code> or <code>-</code>, then the bits.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ chmod 600 notes.txt; ls -l notes.txt
</span></span><span class="line"><span class="cl">-rw------- 1 vscode vscode 3 Nov 11 05:56 notes.txt
</span></span><span class="line"><span class="cl">$ chmod g+r,o+r notes.txt; ls -l notes.txt
</span></span><span class="line"><span class="cl">-rw-r--r-- 1 vscode vscode 3 Nov 11 05:56 notes.txt
</span></span><span class="line"><span class="cl">$ chmod o+w notes.txt; ls -l notes.txt
</span></span><span class="line"><span class="cl">-rw-r--rw- 1 vscode vscode 3 Nov 11 05:56 notes.txt
</span></span></code></pre></div><p>The last one is the kind of thing you want to spot: anyone can change that file. <code>stat -c &quot;%a %A %n&quot;</code> shows
the number and the letters side by side, if you&rsquo;d rather not do the arithmetic:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ stat -c &#34;%a %A %U %G %n&#34; notes.txt hello.sh
</span></span><span class="line"><span class="cl">646 -rw-r--rw- vscode vscode notes.txt
</span></span><span class="line"><span class="cl">755 -rwxr-xr-x vscode vscode hello.sh
</span></span></code></pre></div><p>Use numbers for the whole result, letters for &ldquo;add execute, leave the rest alone&rdquo; (<code>chmod +x</code>). The <a href="https://www.gnu.org/software/coreutils/manual/html_node/File-permissions.html">coreutils manual on file
permissions</a> has every form.</p>
<h3 id="why-do-new-files-start-as-644">Why do new files start as 644?</h3>
<p>Because of the <strong>umask</strong>, a mask of bits your shell takes away from every new file and folder. In the codespace
it&rsquo;s <code>0022</code>, which removes write for group and other. A new file starts from <code>666</code> and ends up <code>644</code>; a new
folder starts from <code>777</code> and ends up <code>755</code>. New files never get <code>x</code>, which is why you <code>chmod +x</code> a script you&rsquo;ve
just written.</p>
<hr>
<h2 id="why-wont-my-script-run">Why won&rsquo;t my script run?</h2>
<p>Because running a file is a permission, and a new file doesn&rsquo;t have it:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ls -l hello.sh
</span></span><span class="line"><span class="cl">-rw-r--r-- 1 vscode vscode 33 Nov 11 05:56 hello.sh
</span></span><span class="line"><span class="cl">$ ./hello.sh
</span></span><span class="line"><span class="cl">bash: line 1: ./hello.sh: Permission denied
</span></span><span class="line"><span class="cl">$ bash hello.sh
</span></span><span class="line"><span class="cl">it runs
</span></span><span class="line"><span class="cl">$ chmod 755 hello.sh; ./hello.sh
</span></span><span class="line"><span class="cl">it runs
</span></span></code></pre></div><p><code>bash hello.sh</code> works without <code>x</code> because there you&rsquo;re running <code>bash</code>, which has <code>x</code>, and it only <em>reads</em> the
script. <code>./hello.sh</code> asks the system to run the file itself, which needs <code>x</code>. The shell&rsquo;s exit code for &ldquo;found
it, can&rsquo;t run it&rdquo; is 126, which you&rsquo;ll see again in <a href="/lessons/linux-cli-101/bash-scripting-basics/">lesson 7</a>.</p>
<hr>
<h2 id="whats-the-difference-between-owner-and-permission">What&rsquo;s the difference between owner and permission?</h2>
<p>The <strong>owner</strong> decides who the user set applies to. The <strong>permissions</strong> decide what each set may do. You change
permissions with <code>chmod</code>, and only on files you own. You change the owner with <code>chown</code>, and only root can do
that, even for your own files:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ chown root notes.txt
</span></span><span class="line"><span class="cl">chown: changing ownership of &#39;notes.txt&#39;: Operation not permitted
</span></span></code></pre></div><p>When a file has the right owner and the wrong access, fix the mode. When it has the wrong owner (usually root,
after a careless <code>sudo</code>), that&rsquo;s a job for <code>sudo chown</code>.</p>
<hr>
<h2 id="why-does-ssh-refuse-a-key-file-others-can-read">Why does SSH refuse a key file others can read?</h2>
<p>An SSH <strong>private key</strong> is the secret half of a key pair: whoever has the file can log in as you wherever the
matching public key is accepted. <a href="/lessons/linux-cli-101/ssh-and-keys/">Lesson 6</a> covers the pair. The
permission part is simple: if anyone but you can read the file, SSH assumes it may already be copied, and it
refuses to use it. Here&rsquo;s a freshly made key set to <code>644</code>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">Permissions 0644 for &#39;demo_key&#39; are too open.
</span></span><span class="line"><span class="cl">It is required that your private key files are NOT accessible by others.
</span></span><span class="line"><span class="cl">This private key will be ignored.
</span></span><span class="line"><span class="cl">Load key &#34;demo_key&#34;: bad permissions
</span></span></code></pre></div><p>Above those lines, SSH prints a box of <code>@</code> signs with a warning in capitals. The <a href="https://man.openbsd.org/ssh#FILES">ssh manual</a>
says it plainly: a private key file should be readable by you and &ldquo;not accessible by others&rdquo;, and ssh
simply ignores one that is.</p>
<p>The fix is <code>chmod 600</code> on the key. Nothing else. Don&rsquo;t copy the key, don&rsquo;t make a new one, don&rsquo;t change the
owner. SSH applies the same idea to the folder around it: in the codespace, <code>~/.ssh</code> is <code>drwx------</code> (700), and
on ws1 <code>authorized_keys</code> is <code>-rw-------</code> (600). SSH (OpenSSH 9.2 in the codespace, 10.0 on ws1) is strict on
purpose, because automation tools like Ansible and Git log in with these same files and nobody is watching.</p>
<hr>
<h2 id="what-is-root-and-what-does-sudo-do">What is root, and what does sudo do?</h2>
<p><strong>root</strong> is the user with UID 0, and the kernel skips permission checks for it. root can read <code>/etc/shadow</code>,
change any file&rsquo;s owner and write anywhere. That&rsquo;s why you don&rsquo;t work as root day to day: one typo has no
safety net.</p>
<p><code>sudo</code> (&ldquo;superuser do&rdquo;) runs <strong>one command</strong> as root, if the system&rsquo;s rules in <code>/etc/sudoers</code> say you may. The
rules file is itself readable only by root:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ls -l /etc/sudoers
</span></span><span class="line"><span class="cl">-r--r----- 1 root root 1714 Apr 11  2026 /etc/sudoers
</span></span><span class="line"><span class="cl">$ cat /etc/sudoers
</span></span><span class="line"><span class="cl">cat: /etc/sudoers: Permission denied
</span></span><span class="line"><span class="cl">$ whoami
</span></span><span class="line"><span class="cl">vscode
</span></span><span class="line"><span class="cl">$ sudo whoami
</span></span><span class="line"><span class="cl">root
</span></span></code></pre></div><p><code>sudo -l</code> lists what you&rsquo;re allowed. In the codespace the answer is everything, with no password:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">User vscode may run the following commands on codespace:
</span></span><span class="line"><span class="cl">    (root) NOPASSWD: ALL
</span></span></code></pre></div><p>That&rsquo;s a lab convenience. On a normal server, <code>sudo</code> asks for <strong>your</strong> password (not root&rsquo;s) and logs every
command. The codespace has sudo 1.9.13; the <a href="https://www.sudo.ws/docs/man/sudo.man/">sudo
manual</a> covers the options.</p>
<h3 id="why-doesnt-sudo-work-with-a-redirect">Why doesn&rsquo;t sudo work with a redirect?</h3>
<p>Because the redirect isn&rsquo;t part of the command. From <a href="/lessons/linux-cli-101/pipes-redirection-grep/">lesson 3</a>:
the shell sets up <code>&gt;</code> before the command starts, and your shell is running as you.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ sudo echo hi &gt; /etc/lesson4-demo.txt
</span></span><span class="line"><span class="cl">bash: line 1: /etc/lesson4-demo.txt: Permission denied
</span></span></code></pre></div><p><code>echo</code> ran as root. The file was opened by your shell, as <code>vscode</code>, and <code>/etc</code> doesn&rsquo;t let <code>vscode</code> write. The
error even says <code>bash:</code>, not <code>echo:</code>. The usual fix is <code>echo hi | sudo tee /etc/file</code>, so that the program
writing the file is the one running as root.</p>
<hr>
<h2 id="why-does-installing-software-need-root">Why does installing software need root?</h2>
<p>On Debian and Ubuntu, software comes in <strong>packages</strong>, and <code>apt</code> is the tool that fetches them from Debian&rsquo;s
servers (the <strong>repositories</strong>) and installs them, along with anything they depend on. Installing means writing
to folders like <code>/usr/bin</code> and to the package database in <code>/var/lib/dpkg</code>, and those belong to root:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ls -ld /usr/bin
</span></span><span class="line"><span class="cl">drwxr-xr-x 1 root root 4096 Nov 11 05:52 /usr/bin
</span></span><span class="line"><span class="cl">$ ls -l /var/lib/dpkg/lock-frontend
</span></span><span class="line"><span class="cl">-rw-r----- 1 root root 0 Nov  7 10:44 /var/lib/dpkg/lock-frontend
</span></span></code></pre></div><p>Anyone can read <code>/usr/bin</code> and run what&rsquo;s in it (<code>r-x</code> for other). Only root can add to it, because a program
there runs for every user. Hence <code>sudo apt</code>.</p>
<p>Two steps, always in this order. <code>sudo apt update</code> downloads the current list of packages and versions; it
installs nothing. <code>sudo apt install &lt;name&gt;</code> then installs a package from that list. Skip the update on a fresh
machine and <code>apt</code> may not find the package at all, because its list is empty or out of date. The <a href="https://manpages.debian.org/bookworm/apt/apt.8.en.html">apt
manual</a> has the rest.</p>
<p>Asking what&rsquo;s installed needs no root:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ apt list --installed 2&gt;/dev/null | grep -E &#34;^(grep|openssh-client|sudo)/&#34;
</span></span><span class="line"><span class="cl">grep/oldstable,now 3.8-5 arm64 [installed,automatic]
</span></span><span class="line"><span class="cl">openssh-client/oldstable,now 1:9.2p1-2+deb12u10 arm64 [installed]
</span></span><span class="line"><span class="cl">sudo/oldstable,now 1.9.13p3-1+deb12u4 arm64 [installed]
</span></span></code></pre></div><p><code>oldstable</code> is Debian&rsquo;s name for the previous release: the codespace runs Debian 12, while the lab&rsquo;s ws1 and
srv1 run Debian 13. <code>automatic</code> means <code>apt</code> installed it as something another package needed.</p>
<hr>
<h2 id="what-i-see-go-wrong">What I see go wrong</h2>
<p><strong><code>chmod 777</code> to make an error go away.</strong> Now every user can change the file or replace the script. Find the
bit that was missing and set only that.</p>
<p><strong><code>chmod -R</code> on a whole tree.</strong> <code>chmod -R 644</code> on a folder removes <code>x</code> from every directory inside it, and
they all become locked doors. Files and folders need different modes; set them separately.</p>
<p><strong><code>sudo</code> for everything.</strong> Running an editor, <code>git</code> or <code>pip</code> with <code>sudo</code> in your home folder leaves files
owned by root there. Later, as yourself, you get &ldquo;Permission denied&rdquo; on your own project. <code>ls -l</code> shows
<code>root</code> in the owner column; <code>sudo chown</code> puts it back. Use <code>sudo</code> for system changes, not to silence errors.</p>
<p><strong>A key that&rsquo;s 644 after a copy.</strong> Keys copied from a Windows share, unzipped, or pulled from a repo often
arrive with loose permissions. SSH then ignores them, and asks for a password instead if the server allows one. <code>chmod 600</code>.</p>
<h2 id="every-folder-on-the-path-with-ls--ld-without-x-on-one-of-them-you-cant-reach-it"><strong>Fixing the file when the folder is the problem.</strong> &ldquo;Permission denied&rdquo; on a file you can plainly read? Check
every folder on the path with <code>ls -ld</code>. Without <code>x</code> on one of them, you can&rsquo;t reach it.</h2>
<h2 id="try-it">Try it</h2>
<p><a href="/labs/linux-cli-101/task-4-1/">Chapter 4 of the lab</a> starts with reading <code>ls -l</code> on a file only root may
read, then has you make a private file and a runnable script with the right modes, and install a missing tool
with <code>apt</code>. The challenge breaks three files on purpose: a private key SSH won&rsquo;t use, a script that won&rsquo;t run,
and a notes file anyone can change. You fix all three without changing who owns them, and the checker logs in
to ws1 with the key to prove it.</p>
<p>If the checker or <code>make break</code> is new to you, <a href="/labs/basics/">Meet the lab</a> explains both. Permissions come
up again in every lab that uses SSH, including <a href="/lessons/python-101/">Python 101</a> and Ansible.</p>
<h2 id="whats-next">What&rsquo;s next</h2>
<p><a href="/lessons/linux-cli-101/processes-environment-path/">Lesson 5</a> is about the programs themselves once they&rsquo;re
running: processes and their IDs, stopping one that won&rsquo;t stop, environment variables, and <code>PATH</code>, the list
of folders the shell searches when you type a command&rsquo;s name.</p>
]]></content:encoded></item><item><title>Pipes, Redirection and grep: One Question, One Pipeline (Linux CLI 101, Lesson 3)</title><link>https://networkdoodles.com/lessons/linux-cli-101/pipes-redirection-grep/</link><pubDate>Mon, 16 Nov 2026 09:00:00 -0500</pubDate><dc:creator>David Henderson</dc:creator><guid>https://networkdoodles.com/lessons/linux-cli-101/pipes-redirection-grep/</guid><category>linux</category><category>networking-basics</category><category>syslog</category><category>troubleshooting</category><category>capeos</category><description>stdin, stdout and stderr, redirecting them to files, and pipes that join grep, cut, sort and uniq to answer real questions about a router's log in one line.</description><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<p>Every command you run in a Linux shell has three streams: one coming in (<strong>stdin</strong>), one for its results
(<strong>stdout</strong>) and one for its complaints (<strong>stderr</strong>). Redirection, with <code>&gt;</code>, <code>&gt;&gt;</code> and <code>2&gt;</code>, sends a stream to a
file. A pipe, <code>|</code>, connects one command&rsquo;s stdout to the next command&rsquo;s stdin. Put small tools in a row like that,
<code>grep</code> to pick lines, <code>sort</code> to order them, <code>uniq -c</code> to count them, and one line answers a question that would
take you ten minutes with a mouse.</p>
<p>This is the lesson where the shell stops being a place to look at files and starts being a tool for questions
about them. The examples all run on <code>r1.log</code>, a real syslog captured from the lab&rsquo;s CapeOS router.</p>
<hr>
<h2 id="what-are-stdin-stdout-and-stderr">What are stdin, stdout and stderr?</h2>
<p>A <strong>stream</strong> is a flow of text into or out of a program. Every program starts with three of them already open:</p>
<table>
  <thead>
      <tr>
          <th>Stream</th>
          <th>Number</th>
          <th>Default</th>
          <th>Used for</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><strong>stdin</strong> (standard input)</td>
          <td>0</td>
          <td>your keyboard</td>
          <td>what the program reads</td>
      </tr>
      <tr>
          <td><strong>stdout</strong> (standard output)</td>
          <td>1</td>
          <td>your terminal</td>
          <td>the results</td>
      </tr>
      <tr>
          <td><strong>stderr</strong> (standard error)</td>
          <td>2</td>
          <td>your terminal</td>
          <td>errors and warnings</td>
      </tr>
  </tbody>
</table>
<p>stdout and stderr both land on your screen, so they look like one thing. They aren&rsquo;t. Ask <code>ls</code> for one file
that exists and one that doesn&rsquo;t:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ls nosuch r1.log
</span></span><span class="line"><span class="cl">ls: cannot access &#39;nosuch&#39;: No such file or directory
</span></span><span class="line"><span class="cl">r1.log
</span></span></code></pre></div><p>The first line went to stderr, the second to stdout. The split is deliberate: it means you can save or process
the results without the error messages mixed in, and still see the errors on screen.</p>
<p>The numbers matter because redirection uses them. <code>2&gt;</code> means &ldquo;stream 2&rdquo;, which is stderr.</p>
<hr>
<h2 id="how-do-i-send-output-to-a-file">How do I send output to a file?</h2>
<p>With <strong>redirection</strong>: you tell the shell, before the command even starts, to point one of its streams somewhere
else. The program doesn&rsquo;t know or care. It writes to stdout as usual, and the shell has already connected stdout
to a file.</p>
<table>
  <thead>
      <tr>
          <th>You write</th>
          <th>What happens</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>cmd &gt; file</code></td>
          <td>stdout goes to <code>file</code>. The file is created, or <strong>emptied first</strong> if it exists</td>
      </tr>
      <tr>
          <td><code>cmd &gt;&gt; file</code></td>
          <td>stdout is added to the end of <code>file</code></td>
      </tr>
      <tr>
          <td><code>cmd 2&gt; file</code></td>
          <td>stderr goes to <code>file</code></td>
      </tr>
      <tr>
          <td><code>cmd &gt; file 2&gt;&amp;1</code></td>
          <td>stdout goes to <code>file</code>, then stderr goes wherever stdout goes: both in one file</td>
      </tr>
      <tr>
          <td><code>cmd 2&gt; /dev/null</code></td>
          <td>stderr is thrown away. <code>/dev/null</code> is a special file that swallows everything</td>
      </tr>
      <tr>
          <td><code>cmd &lt; file</code></td>
          <td>stdin comes from <code>file</code> instead of the keyboard</td>
      </tr>
  </tbody>
</table>
<p>Here&rsquo;s the same <code>ls</code> with stdout sent to a file. The error still reaches the screen, because only stream 1 moved:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ls nosuch r1.log &gt; out.txt
</span></span><span class="line"><span class="cl">ls: cannot access &#39;nosuch&#39;: No such file or directory
</span></span><span class="line"><span class="cl">$ cat out.txt
</span></span><span class="line"><span class="cl">r1.log
</span></span></code></pre></div><p>And with stderr sent to a file instead, the screen stays quiet and the complaint is in <code>err.txt</code>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ls nosuch 2&gt; err.txt
</span></span><span class="line"><span class="cl">$ cat err.txt
</span></span><span class="line"><span class="cl">ls: cannot access &#39;nosuch&#39;: No such file or directory
</span></span></code></pre></div><p><code>2&gt;&amp;1</code> reads as &ldquo;send stream 2 to wherever stream 1 is going right now&rdquo;. Order matters: the shell processes
redirections left to right, so <code>&gt; file 2&gt;&amp;1</code> puts both in the file, and <code>2&gt;&amp;1 &gt; file</code> doesn&rsquo;t (stderr was
pointed at the terminal before stdout moved). The <a href="https://www.gnu.org/software/bash/manual/html_node/Redirections.html">Bash manual&rsquo;s redirection
section</a> has the full list.</p>
<p>A real use for <code>2&gt; /dev/null</code>: <code>apt</code>, Debian&rsquo;s package tool, warns on stderr whenever its output goes anywhere
but a terminal:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ apt list --installed 2&gt;&amp;1 | head -3
</span></span><span class="line"><span class="cl">WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">Listing...
</span></span></code></pre></div><p>Add <code>2&gt;/dev/null</code> and the warning is gone, while the list itself is untouched.</p>
<hr>
<h2 id="what-does-a-pipe-do">What does a pipe do?</h2>
<p>A <strong>pipe</strong>, written <code>|</code>, connects the stdout of the command on its left to the stdin of the command on its right.
No file in between: the text flows straight from one program into the next. A row of commands joined by pipes is
a <strong>pipeline</strong>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ wc -l &lt; r1.log
</span></span><span class="line"><span class="cl">37
</span></span></code></pre></div><p><code>wc -l</code> counts lines. The log has 37. Now count only the successful logins: <code>grep</code> picks the matching lines and
hands them on.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ grep LOGIN_OK r1.log
</span></span><span class="line"><span class="cl">*Nov 11 05:42:32.023: %CAPEOS-5-LOGIN_OK: Login by admin from 172.80.102.1 port 60330
</span></span><span class="line"><span class="cl">*Nov 11 05:43:18.153: %CAPEOS-5-LOGIN_OK: Login by admin from 172.80.102.1 port 41092
</span></span><span class="line"><span class="cl">*Nov 11 05:44:03.607: %CAPEOS-5-LOGIN_OK: Login by admin from 172.80.102.1 port 35784
</span></span><span class="line"><span class="cl">$ grep LOGIN_OK r1.log | wc -l
</span></span><span class="line"><span class="cl">3
</span></span></code></pre></div><p>This is the Unix idea in one line: <strong>small tools that each do one job</strong>, joined together. <code>grep</code> doesn&rsquo;t count,
<code>wc</code> doesn&rsquo;t search, and neither needs to.</p>
<p>Only stdout goes through a pipe. stderr still goes to the screen, which is usually what you want:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ ls nosuch r1.log | wc -l
</span></span><span class="line"><span class="cl">ls: cannot access &#39;nosuch&#39;: No such file or directory
</span></span><span class="line"><span class="cl">1
</span></span><span class="line"><span class="cl">$ ls nosuch r1.log 2&gt;&amp;1 | wc -l
</span></span><span class="line"><span class="cl">2
</span></span></code></pre></div><p>The first <code>wc</code> counted one line, because the error never entered the pipe. With <code>2&gt;&amp;1</code> it counted both.</p>
<hr>
<h2 id="which-small-tools-are-worth-knowing-first">Which small tools are worth knowing first?</h2>
<p>Five do most of the work in log pipelines. All of them read stdin when you don&rsquo;t give them a file name, which is
what makes them pipe-friendly. They&rsquo;re part of <a href="https://www.gnu.org/software/coreutils/manual/">GNU coreutils</a>
(version 9.1 in the codespace).</p>
<table>
  <thead>
      <tr>
          <th>Tool</th>
          <th>Job</th>
          <th>Options you&rsquo;ll use</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>wc</code></td>
          <td>counts lines, words, bytes</td>
          <td><code>-l</code> lines only</td>
      </tr>
      <tr>
          <td><code>sort</code></td>
          <td>puts lines in order</td>
          <td><code>-n</code> by number, <code>-r</code> reverse, <code>-u</code> drop repeats</td>
      </tr>
      <tr>
          <td><code>uniq</code></td>
          <td>collapses <strong>adjacent</strong> repeated lines</td>
          <td><code>-c</code> prefix each with its count</td>
      </tr>
      <tr>
          <td><code>cut</code></td>
          <td>keeps some columns of each line</td>
          <td><code>-d</code> the separator, <code>-f</code> which fields</td>
      </tr>
      <tr>
          <td><code>tee</code></td>
          <td>copies stdin to a file <strong>and</strong> to stdout</td>
          <td><code>-a</code> append</td>
      </tr>
  </tbody>
</table>
<p><code>cut</code> treats each line as fields split by one character. Split the log&rsquo;s lines on <code>:</code> and the fourth field is the
message type:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ cut -d: -f4 r1.log | head -3
</span></span><span class="line"><span class="cl"> %CAPEOS-5-RESTART
</span></span><span class="line"><span class="cl"> %CAPEOS-5-LOGIN_OK
</span></span><span class="line"><span class="cl"> %CAPEOS-3-LINK_DOWN
</span></span></code></pre></div><p>(The time stamp holds two colons of its own, which is why the type is field 4, not 2. Counting fields is the
fiddly part of <code>cut</code>.)</p>
<p><code>tee</code> is for when you want to keep a copy of what&rsquo;s flowing through a pipe and keep going. Here it saves the list
of message types to a file while <code>wc</code> counts them:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ grep -o &#34;%CAPEOS-[0-9]-[A-Z_]*&#34; r1.log | sort -u | tee types.txt | wc -l
</span></span><span class="line"><span class="cl">8
</span></span></code></pre></div><hr>
<h2 id="how-does-sort--uniq--c-count-things">How does sort | uniq -c count things?</h2>
<p>This is the pattern you&rsquo;ll use more than any other: pull out the thing you want to count, sort it so that equal
lines sit together, count each run with <code>uniq -c</code>, then sort by the count, biggest first. Here&rsquo;s every message
type in the router&rsquo;s log, counted:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ grep -o &#34;%CAPEOS-[0-9]-[A-Z_]*&#34; r1.log | sort | uniq -c | sort -rn
</span></span><span class="line"><span class="cl">      9 %CAPEOS-5-CONFIG_DONE
</span></span><span class="line"><span class="cl">      8 %CAPEOS-4-LOGIN_FAILED
</span></span><span class="line"><span class="cl">      4 %CAPEOS-5-PROTO_UP
</span></span><span class="line"><span class="cl">      4 %CAPEOS-5-PROTO_DOWN
</span></span><span class="line"><span class="cl">      4 %CAPEOS-3-LINK_UP
</span></span><span class="line"><span class="cl">      4 %CAPEOS-3-LINK_DOWN
</span></span><span class="line"><span class="cl">      3 %CAPEOS-5-LOGIN_OK
</span></span><span class="line"><span class="cl">      1 %CAPEOS-5-RESTART
</span></span></code></pre></div><p>Read it left to right:</p>
<ol>
<li><code>grep -o</code> prints only the part of each line that matched, not the whole line: one message type per line.</li>
<li><code>sort</code> puts identical types next to each other.</li>
<li><code>uniq -c</code> turns each run of identical lines into one line with a count in front.</li>
<li><code>sort -rn</code> sorts by that number (<code>-n</code>), largest first (<code>-r</code>).</li>
</ol>
<p>In one line you&rsquo;ve learned something useful: eight failed logins in a log of 37 lines, more than the successful
ones. That&rsquo;s the kind of thing worth a second look on a real router. (The four ties come out Z to A because <code>-r</code>
reverses the whole comparison, including the alphabetical tie-break.)</p>
<p>Why the first <code>sort</code>? Because <code>uniq</code> only compares each line with the one before it. Skip the sort and it counts
runs, not totals:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ grep -o &#34;%CAPEOS-[0-9]-[A-Z_]*&#34; r1.log | uniq -c | head -5
</span></span><span class="line"><span class="cl">      1 %CAPEOS-5-RESTART
</span></span><span class="line"><span class="cl">      1 %CAPEOS-5-LOGIN_OK
</span></span><span class="line"><span class="cl">      1 %CAPEOS-3-LINK_DOWN
</span></span><span class="line"><span class="cl">      1 %CAPEOS-5-PROTO_DOWN
</span></span><span class="line"><span class="cl">      2 %CAPEOS-5-CONFIG_DONE
</span></span></code></pre></div><p>No error, just wrong numbers. That&rsquo;s the worst kind of mistake, so make <code>sort | uniq -c</code> a single habit in your
head.</p>
<hr>
<h2 id="what-can-grep-do-besides-find-a-word">What can grep do besides find a word?</h2>
<p><code>grep</code> prints the lines that match a pattern. The codespace has GNU grep 3.8, and the <a href="https://www.gnu.org/software/grep/manual/grep.html">GNU grep
manual</a> covers everything. These options cover most days:</p>
<table>
  <thead>
      <tr>
          <th>Option</th>
          <th>Means</th>
          <th>Example on the lab&rsquo;s files</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>-i</code></td>
          <td>ignore case</td>
          <td><code>grep -i &quot;login by&quot;</code> matches <code>Login by</code></td>
      </tr>
      <tr>
          <td><code>-v</code></td>
          <td>invert: lines that <strong>don&rsquo;t</strong> match</td>
          <td><code>grep -v CAPEOS-5</code> drops the severity 5 noise</td>
      </tr>
      <tr>
          <td><code>-c</code></td>
          <td>print a count of matching lines</td>
          <td><code>grep -c LOGIN_OK r1.log</code> prints <code>3</code></td>
      </tr>
      <tr>
          <td><code>-r</code></td>
          <td>search every file under a folder</td>
          <td><code>grep -r hostname files/configs</code></td>
      </tr>
      <tr>
          <td><code>-o</code></td>
          <td>print only the matching part</td>
          <td>used in the counting pipeline above</td>
      </tr>
      <tr>
          <td><code>-E</code></td>
          <td>extended regular expressions</td>
          <td><code>grep -E &quot;LINK_(UP|DOWN)&quot;</code></td>
      </tr>
      <tr>
          <td><code>-n</code></td>
          <td>add line numbers</td>
          <td>handy when you&rsquo;ll open the file next</td>
      </tr>
  </tbody>
</table>
<p>Case matters by default. The log says <code>Login by</code>, so this finds nothing:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ grep &#34;login by&#34; r1.log | wc -l
</span></span><span class="line"><span class="cl">0
</span></span><span class="line"><span class="cl">$ grep -i &#34;login by&#34; r1.log | wc -l
</span></span><span class="line"><span class="cl">3
</span></span></code></pre></div><p><code>-r</code> turns <code>grep</code> into a search across a whole tree, and it prefixes each match with the file it came from.
On the lab&rsquo;s saved configs:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ grep -r hostname files/configs
</span></span><span class="line"><span class="cl">files/configs/nyc-leaf1.cfg:hostname nyc-leaf1
</span></span><span class="line"><span class="cl">files/configs/nyc-leaf2.cfg:hostname nyc-leaf2
</span></span><span class="line"><span class="cl">files/configs/chi leaf2.cfg:hostname chi-leaf2
</span></span><span class="line"><span class="cl">files/configs/chi-leaf1.cfg:hostname chi-leaf1
</span></span></code></pre></div><p>That&rsquo;s &ldquo;which file has the config for chi-leaf2?&rdquo;, answered without opening any of them. Notice <code>chi leaf2.cfg</code>,
with a space in its name, and a hostname that doesn&rsquo;t match it. Real backup folders look like this.</p>
<p><code>-c</code> counts <strong>lines</strong>, not matches. A line that matches twice counts once. Usually that&rsquo;s what you want with
logs, where one event is one line.</p>
<hr>
<h2 id="what-is-a-regular-expression">What is a regular expression?</h2>
<p>A <strong>regular expression</strong>, or <strong>regex</strong>, is a pattern that describes text instead of spelling it out. Every
<code>grep</code> pattern is one: <code>LOGIN_OK</code> is a regex that happens to match only itself. The power is in the characters
that mean something else.</p>
<table>
  <thead>
      <tr>
          <th>Pattern</th>
          <th>Matches</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>.</code></td>
          <td>any one character</td>
      </tr>
      <tr>
          <td><code>[0-9]</code></td>
          <td>one digit; <code>[A-Z_]</code> one capital letter or underscore</td>
      </tr>
      <tr>
          <td><code>*</code></td>
          <td>the thing before it, any number of times, including none</td>
      </tr>
      <tr>
          <td><code>^</code> and <code>$</code></td>
          <td>the start and the end of the line</td>
      </tr>
      <tr>
          <td><code>(UP|DOWN)</code></td>
          <td><code>UP</code> or <code>DOWN</code> (with <code>-E</code>)</td>
      </tr>
      <tr>
          <td><code>+</code>, <code>?</code></td>
          <td>one or more; zero or one (with <code>-E</code>)</td>
      </tr>
  </tbody>
</table>
<p>So <code>%CAPEOS-[0-9]-[A-Z_]*</code> reads: a percent sign, <code>CAPEOS-</code>, one digit, a dash, then any run of capitals and
underscores. That&rsquo;s every message type, without listing them. And <code>^\*Nov 11 05:43</code> would match only lines whose
time stamp starts in that minute.</p>
<p>Two flavours matter. Plain <code>grep</code> uses <strong>basic</strong> regular expressions, where <code>+</code>, <code>?</code>, <code>|</code> and <code>()</code> are ordinary
characters unless you put a backslash before them. <code>grep -E</code> uses <strong>extended</strong> ones, where they&rsquo;re special
straight away. Use <code>-E</code> whenever your pattern has an &ldquo;or&rdquo; in it; it&rsquo;s easier to read:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ grep -cE &#34;LINK_(UP|DOWN)&#34; r1.log
</span></span><span class="line"><span class="cl">8
</span></span></code></pre></div><p>Quote your patterns. Without the quotes, the shell sees <code>*</code>, <code>|</code> and <code>(</code> first and tries to use them itself,
as a glob, a pipe and a syntax error.</p>
<p>This is a first taste. Python uses the same ideas, with more power (named groups, pulling fields out instead of
just matching lines), and <a href="/lessons/python-101/regex-and-textfsm/">Python 101&rsquo;s regex lesson</a> picks up from
here. The difference in practice: <code>grep</code> answers &ldquo;which lines?&rdquo;, and Python answers &ldquo;which values, as data?&rdquo;.</p>
<hr>
<h2 id="what-i-see-go-wrong">What I see go wrong</h2>
<p><strong><code>&gt;</code> when you meant <code>&gt;&gt;</code>.</strong> <code>&gt;</code> empties the file before the command runs. Running a report twice with <code>&gt;</code> keeps
only the second run. That&rsquo;s fine for a report you regenerate, and a disaster for a log you meant to add to.</p>
<p><strong>Redirecting a file onto itself.</strong> <code>sort r1.log &gt; r1.log</code> doesn&rsquo;t sort the file, it destroys it. The shell
empties <code>r1.log</code> to set up the redirect before <code>sort</code> starts reading, so <code>sort</code> reads an empty file:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ sort copy.log &gt; copy.log
</span></span><span class="line"><span class="cl">$ wc -l copy.log
</span></span><span class="line"><span class="cl">0 copy.log
</span></span></code></pre></div><p>Write to a new file, then rename it.</p>
<p><strong><code>uniq</code> without <code>sort</code>.</strong> Wrong counts, no warning. See above.</p>
<p><strong>Errors that vanish into a pipe, or don&rsquo;t.</strong> If a pipeline&rsquo;s output looks short, check whether the first
command wrote errors to the screen that you scrolled past. If you&rsquo;re saving to a file and the file has errors
in it, you used <code>2&gt;&amp;1</code> and maybe didn&rsquo;t mean to.</p>
<p><strong><code>grep -c</code> on several files.</strong> It prints one count per file (<code>r1.log:3</code>), not a total. For a total, pipe
<code>grep</code> into <code>wc -l</code>.</p>
<p><strong>Unquoted patterns.</strong> <code>grep LINK_(UP|DOWN) r1.log</code> without quotes is a shell syntax error, not a grep one.
If an error message mentions <code>bash:</code> and not <code>grep:</code>, the shell got there first.</p>
<p><strong>Case.</strong> The log says <code>LOGIN_FAILED</code> and <code>Login failed</code>. A pattern in the wrong case matches nothing, quietly.
<code>-i</code> when in doubt.</p>
<hr>
<h2 id="try-it">Try it</h2>
<p><a href="/labs/linux-cli-101/task-3-1/">Chapter 3 of the lab</a> gives you the same <code>r1.log</code> and a list of questions about
it: lines about one interface, lines that aren&rsquo;t config changes, link-down events per interface, the severe
lines saved to a report with nothing on the screen. Then a challenge: who keeps failing to log in to the router,
and how often. Each answer is a one-line pipeline, and the checker runs yours on logs it has never seen, so it
has to answer the question, not just match the numbers in this file.</p>
<p><a href="/lessons/linux-cli-101/working-with-files/">Lesson 2</a> covered reading these files with <code>less</code>, <code>head</code> and
<code>tail -f</code>; <code>tail -f r1.log | grep LINK</code> is the live version of everything above. If this was your first
time in a terminal, the <a href="/labs/basics/">Meet the lab</a> page explains the checker and <code>make</code>.</p>
<h2 id="whats-next">What&rsquo;s next</h2>
<p><a href="/lessons/linux-cli-101/users-permissions-sudo/">Lesson 4</a> is about who may read and change a file: users,
groups, the <code>rwx</code> columns in <code>ls -l</code>, and why some files, like a private SSH key, must be readable by you alone.</p>
]]></content:encoded></item><item><title>Working with Files: Read, Edit, Move and Find (Linux CLI 101, Lesson 2)</title><link>https://networkdoodles.com/lessons/linux-cli-101/working-with-files/</link><pubDate>Mon, 16 Nov 2026 09:00:00 -0500</pubDate><dc:creator>David Henderson</dc:creator><guid>https://networkdoodles.com/lessons/linux-cli-101/working-with-files/</guid><category>linux</category><category>networking-basics</category><category>network-automation</category><category>syslog</category><description>Read logs and configs with less, head and tail -f, edit in nano, copy, move and delete safely, match names with globs, quote spaces, and find files by name.</description><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<p>On Linux, nearly everything you care about is a <strong>text file</strong>: logs, configs, inventories, scripts. You read a
short file with <code>cat</code>, a long one with <code>less</code>, the start with <code>head</code>, the end with <code>tail</code>, and a log that&rsquo;s
still growing with <code>tail -f</code>. You change files in an editor (nano is the friendly one), move them with <code>cp</code>,
<code>mv</code> and <code>rm</code>, pick out groups of them with <strong>globs</strong> like <code>*.cfg</code>, and find them anywhere in the tree with
<code>find</code>.</p>
<p>There&rsquo;s no mouse and no recycle bin, so the habits matter as much as the commands. This lesson covers what
each tool is for, how the shell treats names, and the mistakes that cost people files.</p>
<hr>
<h2 id="what-is-the-difference-between-a-text-file-and-a-binary-file">What is the difference between a text file and a binary file?</h2>
<p>A <strong>text file</strong> is bytes that mean characters: letters, digits, spaces and line breaks. You can read it with
any tool, change it in any editor, and compare two versions line by line. A <strong>binary file</strong> is bytes that mean
something only to the program that reads it: a compiled program, an image, a packet capture.</p>
<p>The <code>file</code> command looks inside a file and tells you which it is. In the lab:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">files/logs/r1.log:           ASCII text
</span></span><span class="line"><span class="cl">files/configs/nyc-leaf1.cfg: ASCII text
</span></span><span class="line"><span class="cl">/usr/bin/ls:                 ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, ...
</span></span></code></pre></div><p>The router&rsquo;s log and a device config are plain text. <code>ls</code> itself is a program in <strong>ELF</strong>, Linux&rsquo;s format for
executables. Its first bytes are a marker, not words:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">0000000 177   E   L   F 002 001 001  \0  \0  \0  \0  \0  \0  \0  \0  \0
</span></span></code></pre></div><p>Linux doesn&rsquo;t care about extensions. <code>.cfg</code>, <code>.log</code> and <code>.txt</code> are for humans: <code>file</code> reads the content.
That&rsquo;s why a file with no extension at all, such as <code>/etc/hostname</code>, can still be a perfectly good text file.</p>
<p>Configs, syslog, YAML inventories and scripts are all text, which is why <code>grep</code>, Git and Ansible can work on
them. Print a binary file with <code>cat</code> and the terminal fills with garbage, sometimes enough
to scramble the display. If that happens, type <code>reset</code> and press <kbd>Enter</kbd>.</p>
<hr>
<h2 id="how-do-you-read-a-file-cat-less-head-or-tail">How do you read a file: cat, less, head or tail?</h2>
<p>They all print a file. They differ in how much and how you move around it.</p>
<table>
  <thead>
      <tr>
          <th>Tool</th>
          <th>Shows</th>
          <th>Use it for</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>cat file</code></td>
          <td>the whole file, all at once</td>
          <td>short files: a config, a hosts list</td>
      </tr>
      <tr>
          <td><code>less file</code></td>
          <td>one screen at a time, and you scroll</td>
          <td>anything longer than a screen</td>
      </tr>
      <tr>
          <td><code>head file</code></td>
          <td>the first 10 lines (<code>-n 5</code> for five)</td>
          <td>the start of a log, a CSV&rsquo;s header row</td>
      </tr>
      <tr>
          <td><code>tail file</code></td>
          <td>the last 10 lines</td>
          <td>the most recent log entries</td>
      </tr>
      <tr>
          <td><code>tail -f file</code></td>
          <td>the end, then new lines as they&rsquo;re written</td>
          <td>watching a log live</td>
      </tr>
  </tbody>
</table>
<p>Here&rsquo;s <code>head -n 6</code> on one of the lab&rsquo;s practice configs, a CapeOS <code>show running-config</code> saved to a file:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">Building configuration...
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">Current configuration : 411 bytes
</span></span><span class="line"><span class="cl">!
</span></span><span class="line"><span class="cl">hostname nyc-leaf1
</span></span><span class="line"><span class="cl">!
</span></span></code></pre></div><p>And <code>tail -n 3</code> on ws1&rsquo;s package log, the newest lines last:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">2026-11-11 05:33:18 trigproc ca-certificates:all 20250419+deb13u1 &lt;none&gt;
</span></span><span class="line"><span class="cl">2026-11-11 05:33:18 status half-configured ca-certificates:all 20250419+deb13u1
</span></span><span class="line"><span class="cl">2026-11-11 05:33:19 status installed ca-certificates:all 20250419+deb13u1
</span></span></code></pre></div><p>Log files are written in time order and only ever added to at the end, which is why <code>tail</code> is the first thing
you run on one. <code>wc -l</code> counts lines, so you know whether <code>cat</code> will fit on the screen: the lab&rsquo;s router log
is <code>37 files/logs/r1.log</code>, short enough for either tool.</p>
<h3 id="how-do-you-move-around-in-less">How do you move around in less?</h3>
<p><code>less</code> is a <strong>pager</strong>: a program that shows text one page at a time. <code>man</code> uses one too, so the keys are worth
learning once.</p>
<table>
  <thead>
      <tr>
          <th>Key</th>
          <th>Does</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><kbd>Space</kbd> / <kbd>b</kbd></td>
          <td>a page forward / back</td>
      </tr>
      <tr>
          <td><kbd>↓</kbd> <kbd>↑</kbd></td>
          <td>a line at a time</td>
      </tr>
      <tr>
          <td><kbd>g</kbd> / <kbd>G</kbd></td>
          <td>the start / the end of the file</td>
      </tr>
      <tr>
          <td><code>/text</code> then <kbd>Enter</kbd></td>
          <td>search forward for <em>text</em></td>
      </tr>
      <tr>
          <td><kbd>n</kbd> / <kbd>N</kbd></td>
          <td>the next / previous match</td>
      </tr>
      <tr>
          <td><kbd>F</kbd></td>
          <td>follow the end, like <code>tail -f</code> (<kbd>Ctrl</kbd>+<kbd>C</kbd> to stop)</td>
      </tr>
      <tr>
          <td><kbd>q</kbd></td>
          <td>quit</td>
      </tr>
  </tbody>
</table>
<p>Searching is the reason to use <code>less</code> on a log rather than scrolling: <code>/LINK_DOWN</code> jumps straight to the first
interface that went down. The lab has <code>less</code> 590.</p>
<h3 id="what-does-tail--f-do">What does tail -f do?</h3>
<p><code>tail -f</code> (<strong>follow</strong>) prints the last lines and then waits. Each time something adds a line to the file, it
appears on your screen. It doesn&rsquo;t end on its own: press <kbd>Ctrl</kbd>+<kbd>C</kbd> to stop watching. The
file is untouched.</p>
<p>It&rsquo;s how you watch a problem happen: run <code>tail -f</code> on a log in one terminal, bounce the interface or rerun the
job in another, and see the lines arrive. When a log file gets replaced rather than added to (log rotation
does this), <code>tail -F</code>, with a capital F, keeps following the new file under the same name.</p>
<hr>
<h2 id="which-editor-should-you-use-and-how-do-you-get-out-of-vim">Which editor should you use, and how do you get out of vim?</h2>
<p>You&rsquo;ll edit text files on machines that have no VS Code. There are three editors worth knowing, and you only
need to be good at one.</p>
<table>
  <thead>
      <tr>
          <th>Editor</th>
          <th>Where you&rsquo;ll find it</th>
          <th>Why</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><strong>VS Code</strong></td>
          <td>the codespace; <code>code file</code> opens a file in it</td>
          <td>the easiest: mouse, tabs, colors, undo</td>
      </tr>
      <tr>
          <td><strong>nano</strong></td>
          <td>almost every Linux machine, including ws1 and srv1</td>
          <td>works in any terminal, and shows its keys on screen</td>
      </tr>
      <tr>
          <td><strong>vim</strong> (or <code>vi</code>)</td>
          <td>most servers, often as the default editor</td>
          <td>powerful, but it has <em>modes</em>, which surprise everyone once</td>
      </tr>
  </tbody>
</table>
<p><strong>nano</strong> is the one to learn for remote machines. The two bottom lines list its commands, where <code>^</code> means
<kbd>Ctrl</kbd>: <code>^O</code> writes the file (<kbd>Ctrl</kbd>+<kbd>S</kbd> also saves), <code>^X</code> exits, <code>^W</code> searches,
<code>^K</code> cuts a line and <code>^U</code> pastes it. It asks before throwing away changes. The codespace has GNU nano 7.2.</p>
<p><strong>vim</strong> starts in <em>normal mode</em>, where letters are commands, not text. That&rsquo;s why typing into it seems to do
random things. If you land in vim by accident (a Git commit message, <code>crontab -e</code>, a <code>visudo</code>), this gets you
out:</p>
<table>
  <thead>
      <tr>
          <th>You want to</th>
          <th>Type</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>leave and throw away changes</td>
          <td><kbd>Esc</kbd>, then <code>:q!</code> and <kbd>Enter</kbd></td>
      </tr>
      <tr>
          <td>save and leave</td>
          <td><kbd>Esc</kbd>, then <code>:wq</code> and <kbd>Enter</kbd></td>
      </tr>
  </tbody>
</table>
<p><kbd>Esc</kbd> first, always: it gets you back to normal mode from wherever you are. In the codespace, <code>vi</code> is
<code>vim.tiny</code>, a small build of vim. The slim ws1 container has no <code>vi</code> at all, only nano.</p>
<hr>
<h2 id="how-do-you-copy-move-rename-and-delete-files">How do you copy, move, rename and delete files?</h2>
<p>Four commands, from GNU coreutils (9.1 in the codespace):</p>
<table>
  <thead>
      <tr>
          <th>Command</th>
          <th>Does</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>cp source target</code></td>
          <td>copies; <code>-r</code> to copy a folder and everything in it</td>
      </tr>
      <tr>
          <td><code>mv source target</code></td>
          <td>moves, and renames: a rename is a move within the same folder</td>
      </tr>
      <tr>
          <td><code>rm file</code></td>
          <td>deletes; <code>-r</code> for a folder and everything in it</td>
      </tr>
      <tr>
          <td><code>mkdir folder</code></td>
          <td>makes a folder; <code>-p</code> makes the parents too, and doesn&rsquo;t complain if it exists</td>
      </tr>
  </tbody>
</table>
<p>There&rsquo;s no separate rename command. <code>mv r1.bak r1-before.cfg</code> renames, and <code>mv r2.cfg old/</code> moves the file
into the folder <code>old</code>. The trailing <code>/</code> is a good habit: if <code>old</code> doesn&rsquo;t exist, <code>mv r2.cfg old</code> quietly
renames the file to <code>old</code> instead, and the slash makes <code>mv</code> fail loudly.</p>
<h3 id="why-is-rm-dangerous">Why is rm dangerous?</h3>
<p><strong>There is no recycle bin on the command line.</strong> <code>rm</code> removes the name, and the space is free for reuse
straight away. No undo, no Trash folder: in the codespace, <code>~/.Trash</code> doesn&rsquo;t exist. <code>rm -r</code> takes a whole
tree with it, without asking.</p>
<p><code>cp</code> and <code>mv</code> have a quieter version of the same problem: if the target already exists, they <strong>overwrite it
without asking</strong>. The <code>-i</code> (<strong>interactive</strong>) option makes them check first:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">mv: overwrite &#39;r10.cfg&#39;?
</span></span></code></pre></div><p>Answer <code>n</code> and nothing changes. Habits that help:</p>
<ul>
<li><code>ls</code> a pattern first, and only then <code>rm</code> the same pattern: you see exactly what will go.</li>
<li>Use <code>rm -i</code> when you&rsquo;re unsure, and <code>cp -i</code> and <code>mv -i</code> when the target might exist.</li>
<li>Keep anything you&rsquo;d miss in Git (<span class="link-soon">the GitOps course</span>): then a mistake is one
<code>git restore</code> away.</li>
</ul>
<hr>
<h2 id="what-are-globs-and-who-expands-them">What are globs, and who expands them?</h2>
<p>A <strong>glob</strong> is a pattern for file names. The two you&rsquo;ll use most:</p>
<table>
  <thead>
      <tr>
          <th>Pattern</th>
          <th>Matches</th>
          <th>Example</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>*</code></td>
          <td>any number of characters, including none</td>
          <td><code>*.cfg</code>: every name ending <code>.cfg</code></td>
      </tr>
      <tr>
          <td><code>?</code></td>
          <td>exactly one character</td>
          <td><code>r?.cfg</code>: <code>r1.cfg</code> and <code>r2.cfg</code>, but not <code>r10.cfg</code></td>
      </tr>
      <tr>
          <td><code>[12]</code></td>
          <td>one of the listed characters</td>
          <td><code>r[12].cfg</code>: <code>r1.cfg</code> and <code>r2.cfg</code></td>
      </tr>
  </tbody>
</table>
<p>The important part: <strong>the shell expands the glob, not the command</strong>. Before <code>ls</code> runs, Bash looks in the folder,
swaps the pattern for the matching names, and passes those. You can watch it happen with <code>set -x</code>, which makes
Bash print each command after expansion. In a scratch folder:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">+ ls &#39;nyc spine1.cfg&#39; r1.cfg r10.cfg r2.cfg sw1.cfg
</span></span><span class="line"><span class="cl">+ ls r1.cfg r2.cfg
</span></span></code></pre></div><p>That&rsquo;s <code>ls *.cfg</code> and <code>ls r?.cfg</code> after Bash had finished with them. <code>ls</code> never saw a <code>*</code>. It&rsquo;s why globs work
the same with every command: <code>cp *.cfg backup/</code>, <code>rm *.tmp</code>, <code>mv r*.cfg r1/</code>.</p>
<p>Two consequences:</p>
<ul>
<li><strong>A glob that matches nothing is left as it is.</strong> <code>echo *.yml</code> in a folder with no YAML files prints <code>*.yml</code>,
and the command then complains that the file <code>*.yml</code> doesn&rsquo;t exist.</li>
<li>Globs don&rsquo;t match hidden files: <code>*</code> skips names that start with a dot, the same way <code>ls</code> does.</li>
</ul>
<hr>
<h2 id="how-do-you-handle-file-names-with-spaces">How do you handle file names with spaces?</h2>
<p>The shell splits a command line at spaces, so a name with a space in it turns into two arguments. With a file
called <code>nyc spine1.cfg</code>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">ls: cannot access &#39;nyc&#39;: No such file or directory
</span></span><span class="line"><span class="cl">ls: cannot access &#39;spine1.cfg&#39;: No such file or directory
</span></span></code></pre></div><p><code>ls</code> went looking for two files that don&rsquo;t exist. Either of these keeps the name in one piece:</p>
<table>
  <thead>
      <tr>
          <th>Write</th>
          <th>How it works</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>&quot;nyc spine1.cfg&quot;</code></td>
          <td>quotes: everything inside is one argument</td>
      </tr>
      <tr>
          <td><code>nyc\ spine1.cfg</code></td>
          <td>a backslash: the next character is taken literally</td>
      </tr>
  </tbody>
</table>
<p>Tab completion writes the backslash for you, which is one more reason to let it finish names. Single quotes
work too, and are stricter: inside <code>'...'</code>, nothing is special at all. Better still, don&rsquo;t create names with
spaces: use <code>-</code> or <code>_</code>. Other people&rsquo;s files will still arrive with them.</p>
<hr>
<h2 id="how-do-you-find-a-file-when-you-dont-know-where-it-is">How do you find a file when you don&rsquo;t know where it is?</h2>
<p><code>find</code> walks down a folder tree and prints every path that matches the tests you give it. The shape is always
<code>find where tests</code>. The lab has GNU findutils 4.9.0.</p>
<table>
  <thead>
      <tr>
          <th>Test</th>
          <th>Matches</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>-name '*.cfg'</code></td>
          <td>the name matches the glob (<code>-iname</code> ignores case)</td>
      </tr>
      <tr>
          <td><code>-type f</code> / <code>-type d</code></td>
          <td>files only / folders only</td>
      </tr>
      <tr>
          <td><code>-mmin -60</code></td>
          <td>changed in the last 60 minutes</td>
      </tr>
      <tr>
          <td><code>-mtime -1</code></td>
          <td>changed in the last day</td>
      </tr>
      <tr>
          <td><code>-mtime +1</code></td>
          <td>changed longer ago (find counts whole days, so <code>+1</code> means at least two days)</td>
      </tr>
      <tr>
          <td><code>-empty</code></td>
          <td>empty files and folders</td>
      </tr>
      <tr>
          <td><code>-size +1M</code></td>
          <td>bigger than a mebibyte</td>
      </tr>
  </tbody>
</table>
<p>Several tests together must all match. In the scratch folder, after a few files were moved around:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ find . -name &#39;*.cfg&#39;
</span></span><span class="line"><span class="cl">./r1.cfg
</span></span><span class="line"><span class="cl">./r10.cfg
</span></span><span class="line"><span class="cl">./sw1.cfg
</span></span><span class="line"><span class="cl">./nyc spine1.cfg
</span></span><span class="line"><span class="cl">./old/r2.cfg
</span></span><span class="line"><span class="cl">./r1-before.cfg
</span></span><span class="line"><span class="cl">$ find . -type d
</span></span><span class="line"><span class="cl">.
</span></span><span class="line"><span class="cl">./old
</span></span><span class="line"><span class="cl">$ find . -mtime +1
</span></span><span class="line"><span class="cl">./old/r2.cfg
</span></span></code></pre></div><p><code>find</code> looks inside subfolders, which <code>ls *.cfg</code> doesn&rsquo;t: it found <code>old/r2.cfg</code>, the one file old enough to
match <code>-mtime +1</code>. Unlike <code>ls</code>, its output isn&rsquo;t sorted. It prints paths in the order it walks the tree.</p>
<p>Notice the quotes around <code>'*.cfg'</code>. Here the glob is for <code>find</code>, not the shell, so it must reach <code>find</code>
unexpanded. Leave them off in a folder that has <code>.cfg</code> files, and the shell expands the pattern first:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">find: paths must precede expression: `r1-before.cfg&#39;
</span></span><span class="line"><span class="cl">find: possible unquoted pattern after predicate `-name&#39;?
</span></span></code></pre></div><p>Searching by age is the one network engineers reach for most: <em>which configs changed since yesterday?</em> is
<code>find</code> with <code>-name</code> and <code>-mtime -1</code>. Searching <em>inside</em> files, for a line rather than a name, is <code>grep</code>, in
<a href="/lessons/linux-cli-101/pipes-redirection-grep/">lesson 3</a>.</p>
<hr>
<h2 id="what-i-see-go-wrong">What I see go wrong</h2>
<p><strong><code>rm</code> with a typo in the pattern.</strong> <code>rm * .cfg</code>, with a stray space, is two arguments: <code>*</code>, everything, and
<code>.cfg</code>. <code>ls</code> the pattern first.</p>
<p><strong>A copy that overwrote the original.</strong> <code>cp new.cfg old.cfg</code> replaces <code>old.cfg</code> silently. <code>-i</code>, or Git.</p>
<p><strong><code>mv</code> into a folder that wasn&rsquo;t there.</strong> The file is now called the folder&rsquo;s name. Write the trailing <code>/</code>.</p>
<p><strong>Stuck in vim.</strong> <kbd>Esc</kbd>, <code>:q!</code>, <kbd>Enter</kbd>.</p>
<p><strong><code>tail -f</code> that never ends.</strong> It&rsquo;s not broken; it&rsquo;s waiting for new lines. <kbd>Ctrl</kbd>+<kbd>C</kbd>.</p>
<p><strong>Unquoted names and patterns.</strong> A name with a space splits in two; a <code>find -name</code> pattern without quotes is
expanded by the shell. Quote both.</p>
<p><strong>Editing a config in Windows and copying it over.</strong> Windows ends lines with two characters, Linux with one.
<code>file</code> reports <em>with CRLF line terminators</em>, and some tools choke on the extra character. VS Code&rsquo;s status bar
shows <code>CRLF</code> or <code>LF</code>, and one click changes it.</p>
<hr>
<h2 id="try-it">Try it</h2>
<p><a href="/labs/linux-cli-101/task-2-1/">Chapter 2 of the lab</a> gives you a real CapeOS syslog from the lab&rsquo;s router.
You read it with <code>less</code> and its search, pick out the first and last events with <code>head</code> and <code>tail</code>, watch new
lines arrive with <code>tail -f</code>, and write a note in nano. The challenge is a messy folder of config backups,
saved by hand: you sort it into folders, fix a name with a space in it, clear out the empty files, and use
<code>find</code> to count what&rsquo;s there. The same skills come back in <a href="/lessons/python-101/files-and-errors/">Python 101&rsquo;s lesson on
files</a>, where a script does the reading.</p>
<h2 id="whats-next">What&rsquo;s next</h2>
<p><a href="/lessons/linux-cli-101/pipes-redirection-grep/">Lesson 3</a> connects commands together: send one command&rsquo;s
output into the next, save it to a file, and use <code>grep</code> to answer a question about a log in one line.</p>
]]></content:encoded></item><item><title>The Shell and the Filesystem: Reading Any Path (Linux CLI 101, Lesson 1)</title><link>https://networkdoodles.com/lessons/linux-cli-101/shell-and-filesystem/</link><pubDate>Mon, 16 Nov 2026 09:00:00 -0500</pubDate><dc:creator>David Henderson</dc:creator><guid>https://networkdoodles.com/lessons/linux-cli-101/shell-and-filesystem/</guid><category>linux</category><category>networking-basics</category><category>network-automation</category><category>capeos</category><description>What a shell is, how a command line is built, and how Linux finds any file in one tree: absolute and relative paths, ~, . and .., hidden files and help.</description><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<p>A <strong>shell</strong> is the program that reads what you type at the prompt, works out which command you mean, and runs
it. On almost every Linux system, including this course&rsquo;s lab, the shell is <strong>Bash</strong>. Every file Linux can see
sits in <strong>one tree</strong> that starts at <code>/</code>, the root. A <strong>path</strong> is the route through that tree to one file:
<strong>absolute</strong> if it starts at <code>/</code>, <strong>relative</strong> if it starts from the folder you&rsquo;re in now.</p>
<p>Once you can read a path and know where it points, the rest of the command line gets much easier. This lesson
covers the shell, the prompt, how a command line is put together, the tree and its paths, hidden files, where
Linux keeps things, and how to get help.</p>
<hr>
<h2 id="why-does-a-network-engineer-need-linux">Why does a network engineer need Linux?</h2>
<p>Because you&rsquo;re already using it, even if the box says something else. The lab&rsquo;s router, r1, runs
<a href="/posts/capeos-lab-network-os/">CapeOS</a>, and it looks like a router from its own CLI:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">CapeOS Software (CO-BOLT), Version 0.11.2, LAB USE ONLY
</span></span><span class="line"><span class="cl">CapeOS is open-source lab software. Not for production use.
</span></span></code></pre></div><p>Ask the container underneath the same question Linux answers, and it&rsquo;s Debian:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">PRETTY_NAME=&#34;Debian GNU/Linux 12 (bookworm)&#34;
</span></span></code></pre></div><p>That&rsquo;s the pattern across the industry. Nokia&rsquo;s <a href="https://learn.srlinux.dev/">SR Linux</a>, which other labs on
this site use, runs on a standard Linux kernel. Many network operating systems run on a Linux kernel and give you a way
to a Linux shell underneath. The automation tools are Linux-first too: Ansible&rsquo;s control node, Python
scripts, containerlab, Git servers and the CI runners that test your changes all run there. The codespace
you&rsquo;ll use for every lab on this site is a Linux container, Debian 12 in this one.</p>
<p>You don&rsquo;t need to become a Linux administrator. You need to be at home in a shell: find a file, read it, run a
tool, and understand the error when it doesn&rsquo;t work.</p>
<hr>
<h2 id="what-is-the-difference-between-a-terminal-and-a-shell">What is the difference between a terminal and a shell?</h2>
<p>They&rsquo;re two programs that people often mix up.</p>
<table>
  <thead>
      <tr>
          <th></th>
          <th>What it is</th>
          <th>In the lab</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><strong>Terminal</strong></td>
          <td>the window: it draws text, takes your keystrokes and passes them on</td>
          <td>the Terminal panel in VS Code</td>
      </tr>
      <tr>
          <td><strong>Shell</strong></td>
          <td>the program behind the window that reads each line and runs it</td>
          <td>Bash</td>
      </tr>
  </tbody>
</table>
<p>The terminal doesn&rsquo;t understand commands. It hands what you type to the shell, and shows what the shell and
the commands print back. You can swap one without the other: the same Bash runs in VS Code&rsquo;s panel, in a
Mac&rsquo;s Terminal app, or over SSH from anywhere.</p>
<p>Bash is the <strong>Bourne Again SHell</strong>, from the GNU project. It&rsquo;s a newer take on the original Unix shell by
Stephen Bourne, which is where the name comes from. The versions in the lab:</p>
<table>
  <thead>
      <tr>
          <th>Machine</th>
          <th>OS</th>
          <th><code>bash --version</code></th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>the codespace</td>
          <td>Debian 12 (bookworm)</td>
          <td>5.2.15</td>
      </tr>
      <tr>
          <td>ws1, srv1</td>
          <td>Debian 13 (trixie)</td>
          <td>5.2.37</td>
      </tr>
  </tbody>
</table>
<p>Everything in this course works the same on both. The <a href="https://www.gnu.org/software/bash/manual/">GNU Bash manual</a>
is the primary source when you want the full story.</p>
<hr>
<h2 id="what-is-the-prompt-telling-you">What is the prompt telling you?</h2>
<p>The <strong>prompt</strong> is what the shell prints when it&rsquo;s ready for your next command. It&rsquo;s not decoration: it tells
you who you are, where you are, and sometimes how the last command went.</p>
<p>In the codespace, the prompt is set up by the dev container and looks like this, with your GitHub name in
front:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">@yourname ➜ /workspaces/linux-cli-101 (main) $
</span></span></code></pre></div><p>On ws1, a plain Debian machine, it&rsquo;s the Debian default:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">netops@ws1:~$
</span></span></code></pre></div><table>
  <thead>
      <tr>
          <th>Part</th>
          <th>Means</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>@yourname</code> or <code>netops</code></td>
          <td>the user you are</td>
      </tr>
      <tr>
          <td><code>@ws1</code></td>
          <td>the machine you&rsquo;re on: worth checking before you type anything risky</td>
      </tr>
      <tr>
          <td><code>/workspaces/linux-cli-101</code> or <code>~</code></td>
          <td>the folder you&rsquo;re in now</td>
      </tr>
      <tr>
          <td><code>(main)</code></td>
          <td>the Git branch, when the folder is a Git repository</td>
      </tr>
      <tr>
          <td><code>➜</code></td>
          <td>in the codespace, it turns red when the last command failed</td>
      </tr>
      <tr>
          <td><code>$</code></td>
          <td>an ordinary user; <code>#</code> means you&rsquo;re root, the all-powerful user (<a href="/lessons/linux-cli-101/users-permissions-sudo/">lesson 4</a>)</td>
      </tr>
  </tbody>
</table>
<p>When you&rsquo;re logged in to three machines in three tabs, reading the prompt is how you avoid running a command
on the wrong one.</p>
<hr>
<h2 id="how-is-a-command-line-put-together">How is a command line put together?</h2>
<p>Every line you type has the same shape: a <strong>command</strong>, then <strong>options</strong> that change how it behaves, then
<strong>arguments</strong> that say what it works on. Spaces separate them.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">ls  -l  files/configs
</span></span><span class="line"><span class="cl">│   │   └── argument: what to work on
</span></span><span class="line"><span class="cl">│   └────── option: how to do it (long listing)
</span></span><span class="line"><span class="cl">└────────── command: what to run
</span></span></code></pre></div><p>Here&rsquo;s that line in the lab, with real output:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">total 20
</span></span><span class="line"><span class="cl">-rw-r--r-- 1 vscode vscode  63 Nov 11 05:44 README.txt
</span></span><span class="line"><span class="cl">-rw-r--r-- 1 vscode vscode 384 Nov 11 05:44 chi leaf2.cfg
</span></span><span class="line"><span class="cl">-rw-r--r-- 1 vscode vscode 411 Nov 11 05:44 chi-leaf1.cfg
</span></span><span class="line"><span class="cl">-rw-r--r-- 1 vscode vscode 411 Nov 11 05:44 nyc-leaf1.cfg
</span></span><span class="line"><span class="cl">-rw-r--r-- 1 vscode vscode 384 Nov 11 05:44 nyc-leaf2.cfg
</span></span></code></pre></div><p>Options come in two spellings: <strong>short</strong>, one dash and one letter (<code>-l</code>), and <strong>long</strong>, two dashes and a word
(<code>--all</code>). Short ones can be bundled: <code>ls -la</code> is <code>ls -l -a</code>. Most GNU tools accept both, and <code>--help</code> lists
them side by side.</p>
<p>Two things trip beginners up:</p>
<ul>
<li><strong>Linux is case-sensitive.</strong> <code>ls -l</code> and <code>ls -L</code> are different options, and <code>README.txt</code> and <code>readme.txt</code>
are different files.</li>
<li><strong>A space ends an argument.</strong> <code>chi leaf2.cfg</code> above is one file with a space in its name. Typed bare, the
shell sees two arguments. <a href="/lessons/linux-cli-101/working-with-files/">Lesson 2</a> covers quoting.</li>
</ul>
<p>Not every command is a program on disk. Some are built into the shell itself. <code>type</code> tells you which:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">cd is a shell builtin
</span></span><span class="line"><span class="cl">pwd is a shell builtin
</span></span></code></pre></div><p><code>cd</code> has to be a builtin: a separate program could change its own folder, but not the shell&rsquo;s.</p>
<hr>
<h2 id="what-is-the-linux-filesystem-tree">What is the Linux filesystem tree?</h2>
<p>Windows gives each disk a letter: <code>C:\</code>, <code>D:\</code>. Linux doesn&rsquo;t. There is <strong>one tree</strong>, and everything hangs off
its root, written <code>/</code>. Extra disks, USB sticks and network shares are attached somewhere inside that tree, at a
folder called a <strong>mount point</strong>, so they look like any other folder.</p>
<p>The top of the tree in the codespace (<code>ls /</code>) looks like this:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">bin   boot  dev  etc  home  lib  media  mnt  opt  proc  root
</span></span><span class="line"><span class="cl">run   sbin  srv  sys  tmp   usr  var  workspaces
</span></span></code></pre></div><p><code>workspaces</code> is the one Codespaces adds; the rest you&rsquo;ll find on any Debian machine. The layout is set out in
the <a href="https://refspecs.linuxfoundation.org/FHS_3.0/fhs/index.html">Filesystem Hierarchy Standard</a>. A handful of
folders matter from day one:</p>
<table>
  <thead>
      <tr>
          <th>Folder</th>
          <th>What&rsquo;s in it</th>
          <th>You&rsquo;ll go there to</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>/etc</code></td>
          <td>system configuration, as text files</td>
          <td>read how a service is set up (<code>/etc/hostname</code>, <code>/etc/os-release</code>)</td>
      </tr>
      <tr>
          <td><code>/var/log</code></td>
          <td>log files</td>
          <td>find out what a service did, and when</td>
      </tr>
      <tr>
          <td><code>/home</code></td>
          <td>one folder per user (<code>/home/vscode</code>, <code>/home/netops</code>)</td>
          <td>keep your own files</td>
      </tr>
      <tr>
          <td><code>/tmp</code></td>
          <td>temporary files, often emptied at reboot</td>
          <td>try things out</td>
      </tr>
      <tr>
          <td><code>/usr/bin</code></td>
          <td>most of the programs you run (<code>/usr/bin/ls</code>)</td>
          <td>see where a command lives</td>
      </tr>
      <tr>
          <td><code>/root</code></td>
          <td>the root user&rsquo;s home, not inside <code>/home</code></td>
          <td>rarely: it&rsquo;s root&rsquo;s</td>
      </tr>
  </tbody>
</table>
<p>Linux keeps its configuration in plain text, and it&rsquo;s one of the reasons it automates so well: a tool that can
read and write a text file can configure the system.</p>
<hr>
<h2 id="what-is-the-difference-between-an-absolute-and-a-relative-path">What is the difference between an absolute and a relative path?</h2>
<p>An <strong>absolute path</strong> starts with <code>/</code> and spells out the route from the root. It means the same thing wherever
you are: <code>/workspaces/linux-cli-101/files/site/nyc/README.txt</code> is one file, from any folder, on any shell in
that machine.</p>
<p>A <strong>relative path</strong> doesn&rsquo;t start with <code>/</code>. The shell starts it from your <strong>working directory</strong>, the folder
you&rsquo;re in now (<code>pwd</code> prints it). <code>files/configs</code> only means something because the terminal is sitting in
<code>/workspaces/linux-cli-101</code>.</p>
<p>Three short names help you move around:</p>
<table>
  <thead>
      <tr>
          <th>Name</th>
          <th>Means</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>.</code></td>
          <td>this folder</td>
      </tr>
      <tr>
          <td><code>..</code></td>
          <td>the folder above this one, its <strong>parent</strong></td>
      </tr>
      <tr>
          <td><code>~</code></td>
          <td>your home folder: <code>/home/vscode</code> in the codespace, <code>/home/netops</code> on ws1</td>
      </tr>
  </tbody>
</table>
<p>Every folder has a <code>.</code> and a <code>..</code> inside it, which is how relative paths can climb. Here&rsquo;s one from the lab&rsquo;s
practice tree, with <code>realpath</code>, which turns any path into the absolute path it points to:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ pwd
</span></span><span class="line"><span class="cl">/workspaces/linux-cli-101/files/site/nyc/rack2
</span></span><span class="line"><span class="cl">$ realpath ../README.txt
</span></span><span class="line"><span class="cl">/workspaces/linux-cli-101/files/site/nyc/README.txt
</span></span></code></pre></div><p>Read it one piece at a time: start in <code>rack2</code>; <code>..</code> climbs one level to <code>nyc</code>; <code>README.txt</code> is the file in
there. Each <code>..</code> is one folder up. Chain them (<code>../../</code>) to climb further, then name folders to go back down.</p>
<p><code>~</code> is a little different. It isn&rsquo;t a folder name: the <strong>shell</strong> replaces it with your home folder&rsquo;s absolute
path before the command sees it. <code>cd</code> on its own does the same thing and takes you home.</p>
<h3 id="which-kind-should-you-use">Which kind should you use?</h3>
<table>
  <thead>
      <tr>
          <th>Use</th>
          <th>When</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>absolute</td>
          <td>in scripts, config files, cron jobs: anything that might run from a different folder</td>
      </tr>
      <tr>
          <td>relative</td>
          <td>typing at the prompt, inside a project you&rsquo;re already in</td>
      </tr>
  </tbody>
</table>
<p>Most &ldquo;file not found&rdquo; errors from a script that works at the prompt are a relative path started from the wrong
folder.</p>
<hr>
<h2 id="what-are-hidden-files">What are hidden files?</h2>
<p>A file or folder whose name starts with a dot is <strong>hidden</strong>: <code>ls</code> leaves it out unless you add <code>-a</code>. It isn&rsquo;t
secret or protected. It&rsquo;s a convention for files you don&rsquo;t need to see every day, mostly configuration.</p>
<p>New users on ws1 get their starting files from <code>/etc/skel</code>, and <code>ls</code> alone shows nothing there. <code>ls -a</code> shows
what&rsquo;s really in it:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">.
</span></span><span class="line"><span class="cl">..
</span></span><span class="line"><span class="cl">.bash_logout
</span></span><span class="line"><span class="cl">.bashrc
</span></span><span class="line"><span class="cl">.profile
</span></span></code></pre></div><p><code>.bashrc</code> sets up your shell each time it starts (<a href="/lessons/linux-cli-101/processes-environment-path/">lesson 5</a>).
Your home folder also holds <code>.ssh</code>, where SSH keeps your keys (<a href="/lessons/linux-cli-101/ssh-and-keys/">lesson 6</a>).
Hidden files are where a lot of tools keep their settings, so when something behaves oddly, <code>ls -a</code> is worth a
look.</p>
<hr>
<h2 id="how-do-you-get-help-without-leaving-the-terminal">How do you get help without leaving the terminal?</h2>
<p>Two places, and you&rsquo;ll use both.</p>
<p><strong><code>--help</code></strong> is built into most commands. It prints a short summary: the usage line and every option, one line
each. It&rsquo;s quick, and it describes the exact version you have.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">Usage: mkdir [OPTION]... DIRECTORY...
</span></span><span class="line"><span class="cl">Create the DIRECTORY(ies), if they do not already exist.
</span></span></code></pre></div><p>Read the usage line as a pattern: <code>[ ]</code> means optional, <code>...</code> means you can give more than one, and capitals
are the things you fill in.</p>
<p><strong><code>man</code></strong> opens the <strong>manual page</strong>: the full reference, with descriptions and often examples. It opens in a
pager, so you scroll with the arrow keys or Space, search with <code>/</code>, and leave with <code>q</code>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">LS(1)                            User Commands                           LS(1)
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">NAME
</span></span><span class="line"><span class="cl">       ls - list directory contents
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">SYNOPSIS
</span></span><span class="line"><span class="cl">       ls [OPTION]... [FILE]...
</span></span></code></pre></div><p>The <code>(1)</code> is the manual&rsquo;s <strong>section</strong>. Section 1 is commands, section 5 is file formats, section 8 is admin
commands. When a name lives in more than one, ask for the section: <code>man 5 passwd</code> is the file, <code>man passwd</code>
the command. The same pages are online at <a href="https://man7.org/linux/man-pages/">man7.org</a>, which is handy when
you&rsquo;re reading on another screen.</p>
<p>Not every machine has <code>man</code>. The codespace does. ws1 is a slim container and doesn&rsquo;t (<code>bash: man: command not found</code>), so <code>--help</code> is your friend there. Builtins such as <code>cd</code> have no manual page of their own: <code>help cd</code> asks
Bash for its summary.</p>
<hr>
<h2 id="what-i-see-go-wrong">What I see go wrong</h2>
<p><strong>Working in the wrong folder.</strong> A command says <em>No such file or directory</em> for a file you can see in the
explorer. The file exists; you&rsquo;re just not where the relative path starts. Check the prompt, or <code>pwd</code>.</p>
<p><strong>Working on the wrong machine.</strong> Three tabs, three machines, one prompt that looks a lot like another. Read
<code>user@host</code> before anything that changes state.</p>
<p><strong>Reading <code>~</code> as a folder name.</strong> <code>~</code> works because the shell expands it. Inside quotes (<code>&quot;~/notes.txt&quot;</code>) it
doesn&rsquo;t, and some programs reading paths from a config file won&rsquo;t expand it either. Use the absolute path
there.</p>
<p><strong>Typing a Windows path.</strong> Linux uses <code>/</code>, not <code>\</code>, and there&rsquo;s no drive letter. A backslash means something
else to the shell entirely.</p>
<p><strong>Missing a hidden file.</strong> The config you&rsquo;re looking for &ldquo;isn&rsquo;t there&rdquo; because its name starts with a dot.
<code>ls -a</code>.</p>
<p><strong>Wrong case.</strong> <code>Files/</code> and <code>files/</code> are different folders. Tab completion avoids this: let the shell finish
names for you.</p>
<hr>
<h2 id="try-it">Try it</h2>
<p><a href="/labs/linux-cli-101/task-1-1/">Chapter 1 of the lab</a> starts in the codespace&rsquo;s terminal. You find your home
folder, the lab&rsquo;s folder and the operating system&rsquo;s name, go looking for a hidden file, and then read a small
practice tree of site and rack folders until you can reach any file in it by an absolute path and by the
shortest relative one. If the terminal itself is new, <a href="/labs/basics/">Meet the lab</a> shows the editor, the
terminal and the checker first.</p>
<h2 id="whats-next">What&rsquo;s next</h2>
<p>Now you can find a file, <a href="/lessons/linux-cli-101/working-with-files/">lesson 2</a> is about what to do with it:
reading logs and configs, editing in nano, copying, moving and deleting, and finding files by name and age.</p>
]]></content:encoded></item></channel></rss>