cheat-sheets

Linux Basics

Network and host configuration on Ubuntu. For everyday commands see Linux.

Change Hostname #

bash
sudo hostnamectl set-hostname newhostname

Also update the 127.0.1.1 line in /etc/hosts. Show the result with hostnamectl.

Netplan Basics #

Netplan reads YAML from /etc/netplan/*.yaml and drives systemd-networkd or NetworkManager.

COMMANDDESCRIPTION
ip -br aFind the interface name (e.g. ens3, eth0)
sudo netplan tryApply with automatic rollback after 120 s if you lose connectivity
sudo netplan applyApply the configuration
sudo netplan getShow the merged config
sudo netplan generateValidate and generate backend config
sudo chmod 600 /etc/netplan/*.yamlNetplan warns if files are world-readable

Change IP Address in Ubuntu 20.04 LTS #

  1. Create a new file /etc/netplan/01-netcfg.yaml
yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: no
      addresses:
        - 192.168.121.221/24
      gateway4: 192.168.121.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]
  1. Apply changes
bash
sudo netplan apply

Change IP Address in Ubuntu 22.04 LTS and later #

gateway4 was deprecated in Ubuntu 22.04 and routes is used instead #

  1. Create a new file /etc/netplan/01-netcfg.yaml
yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: no
      addresses:
        - 192.168.121.221/24
      routes:
        - to: default
          via: 192.168.121.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]
  1. Apply changes
bash
sudo netplan apply

DHCP #

yaml
network:
  version: 2
  ethernets:
    ens3:
      dhcp4: true

Other Basics #

COMMANDDESCRIPTION
sudo timedatectl set-timezone Europe/LondonSet the timezone
timedatectlTime, NTP and timezone status
sudo timedatectl set-ntp trueEnable time sync
sudo localectl set-locale LANG=en_GB.UTF-8Set the locale
resolvectl statusDNS servers in use
sudo ufw allow OpenSSH && sudo ufw enableBasic firewall, see UFW (uncomplicated firewall)
sudo apt update && sudo apt full-upgradeUpdate the system
sudo reboot / sudo poweroffReboot / shut down
sudo unattended-upgrades --dry-run -dTest automatic security updates
sudo dpkg-reconfigure -plow unattended-upgradesEnable automatic security updates
Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.