Ubuntu Server Cheat Sheet

2022-06-23

There's a ton of configuration you have to do to set up a new computer as an Ubuntu server. Whether its a rack server, a rasberry pi, or some embedded machine, here is a cheat sheet for the most common tasks.

Handy ISO download link

SSH

Install ssh server (if you didn't during OS install):

apt install openssh-server

Setup ssh key login (run on conncting machine, not the server)

ssh-copy-id user@ip

Sudo

run visudo, then add

myuser ALL=(ALL) NOPASSWD: ALL

to /etc/sudoers so you don't need to retype your password over and over again

Static IP

Use a netplan yaml file. Place it in /etc/netplan/.

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s1:
     addresses: [192.168.1.10/24]

Replace enp0s1 with your device name. Can see options with ls /sys/class/net.

DHCP enabled (for if you mess up the static IP and need network access):

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s1:
      dhcp4: yes

Systemd Services

See the post on systemd services here

Commands for start/stop/enable

systemctl start foo.service
systemctl stop foo.service
systemctl enable foo.service

You can also use new service <service_name> start syntax.

View service logs

journalctl -u <service_name> [-f]

Use -f to follow logs live

Use the following in /etc/systemd/journald.conf to make sure you don't fill up the whole disk with logs:

[Journal]
SystemMaxUse=1G
SystemKeepFree=1G

Cron

Put scripts in /etc/cron.daily/, /etc/cron.hourly/, etc... instead of having to use crontab Make sure to chown root:root and chmod +x all scripts added to these directories

Environment Variables

Any lines put in /etc/environment will be added to the environment on startup. It's like a bashrc for the entire system but only for env vars.

Docker

Install docker engine

apt-get install ca-certificates curl gnupg lsb-release

mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

Set groups so docker doesn't run as root

groupadd docker
usermod -aG docker $SUDO_USER

Install docker compose

curl -SL https://github.com/docker/compose/releases/download/v2.6.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

And don't fill up your SSD with docker logs either:

/etc/docker/daemon.json

{
  "log-driver": "local",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3",
  }
}

Node JS

Use the following script to setup apt for desired version of nodejs:

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -

then use apt install nodejs to install.

← Back