One morning ssh [email protected] just hangs. Did the VM's address change? Is its network card down? Is it the Mac? You cannot fix what you cannot name. This lesson gives you the words - address, interface, gateway - and the three commands that show them.
What you need to know already: 1.1 Where you are (host, guest, SSH), 1.7 Driving the shell.
Addresses and networks
An IP address is a machine's number on a network, like a phone number. The common kind, IPv4, is four numbers from 0 to 255: 10.64.0.2. (There is also a longer kind, IPv6, written with colons: fe80::3ca1:.... You will see both; this lesson is about IPv4.)
A network (or subnet) is a group of addresses that can reach each other directly. It is written as an address plus a prefix length: 10.64.0.2/24. The /24 says "the first 24 bits - the first three numbers - name the network". So 10.64.0.0 to 10.64.0.255 are all on the same local network, and your VM is number 2 in it.
To reach anything outside its own network, a machine sends traffic to a gateway (a router: a machine that forwards traffic between networks). Which traffic goes where is the routing table; the catch-all entry is the default route.
Where 10.64.0.2 came from
You never typed it. DHCP (Dynamic Host Configuration Protocol) is how a machine asks the network for an address. UTM runs a small DHCP server on the private network it made for the VM. At boot the VM shouted "anyone got an address for me?", and UTM answered with:
- an address,
10.64.0.2/24, - a gateway,
10.64.0.1- that is your Mac, on the same network, - a DNS server (the service that turns names like
github.cominto addresses), - a lease: how long the address is the VM's before it must ask again.
Your Mac being 10.64.0.1 on that same network is why ssh [email protected] works from the Mac and from nowhere else.
Interfaces, and reading the name
A network interface is one network connection as the OS sees it - usually one NIC (network interface card, the hardware that plugs into a network). Yours is called enp0s1:
enp0s1
│ │ └┴─ s1 = slot 1 on that bus
│ └┴─── p0 = PCI bus 0
└┴───── en = Ethernet
Ethernet is the standard wired network type. PCI is the internal connector system that cards plug into: a numbered bus, with numbered slots on it. So the name means "the Ethernet card in slot 1 of bus 0".
This is predictable interface naming. The old scheme (eth0, eth1) numbered cards in the order the kernel found them, so adding a card could rename your existing one and break settings that referred to it. Now the name comes from the physical position, so it never moves. lspci (list PCI devices) shows that network device at 00:01.0 - bus 00, slot 01 - which is literally where the name comes from.
lo is the loopback interface, address 127.0.0.1: a pretend network that never leaves the machine, so programs on the box can talk to each other.
The commands
$ ip -br a
lo UNKNOWN 127.0.0.1/8 ::1/128
enp0s1 UP 10.64.0.2/24 fd9b:...:2b09/64 fe80::...:2b09/64
ip is the tool for everything network. a is short for address (show addresses), and -br means brief: one line per interface. The columns: the interface name, its state (UP = connected and working; lo always says UNKNOWN, which is normal), then its addresses - the IPv4 one with its /24, then IPv6 ones.
$ ip r
default via 10.64.0.1 dev enp0s1 proto dhcp src 10.64.0.2 metric 100
10.64.0.0/24 dev enp0s1 proto kernel scope link src 10.64.0.2 metric 100
ip r (route) prints the routing table. Line 1: "for everything not listed below (default), send it via the gateway 10.64.0.1, out of device enp0s1; this was learned from dhcp". Line 2: "the 10.64.0.0/24 network is right here on enp0s1 - no gateway needed".
ip a full detail (hardware address, every setting)
hostname -I just the addresses, nothing else - handy in scripts
ifconfig, which you may know from the Mac, is not installed on Ubuntu Server (it lives in the old net-tools package). ip replaced it; learn that one.
The "use DHCP on enp0s1" decision itself is a file the installer wrote: /etc/netplan/50-cloud-init.yaml. Only root may read it (mode 600), so sudo cat it. Chapter 8 (networking) edits it.
Why "ssh learner@oncall-lab" fails from the Mac
The VM knows its own name. Your Mac does not - nothing tells it. To use a name instead of an address, something has to translate the name: a DNS server, a line in the Mac's /etc/hosts file (a local list of name = address pairs), or mDNS (multicast DNS: machines on the same local network announce their own names - Apple calls it Bonjour, and it answers for names ending in .local). A stock Ubuntu Server does not run an mDNS announcer, so the name never leaves the VM. Either use the IP, install avahi-daemon (the Linux mDNS announcer) on the VM, or add the line to the Mac's /etc/hosts.
DHCP leases can change. If the address moves, find it again from the UTM window (the VM's own screen, which works without the network) with ip -br a. That is also why servers other machines must reach are not left on a changing address: they get a static (fixed, typed-in) address, or a DHCP reservation - the DHCP server always hands that one machine the same address.
In an interview ("how does a machine get its IP?"): DHCP - it asks, a server answers with address, network size, gateway, DNS and a lease; ip -br a and ip r show the result, and proto dhcp in the route says where it came from.
What you can now do
- Read
10.64.0.2/24: address, network, which neighbours are local. - Find your addresses, interface state and default gateway with
ip -br aandip r. - Decode
enp0s1, and explain why a name you gave the VM is not reachable from the Mac.