Swap, timezone and hostname on Ubuntu
Three small settings worth doing on every new server:
- swap: when RAM fills up, the system moves part of memory to disk temporarily instead of killing processes (the OOM killer). On low-memory servers (1 or 2 GB) it prevents sudden failures such as MariaDB being killed or
composer installfailing. - timezone: makes the times in logs, cron and application messages match your local time.
- hostname: the server's name in the terminal prompt, logs and system emails.
Prerequisites
- A cloud server or VPS with Ubuntu 24.04 (or 22.04) and a sudo user (first SSH login and hardening)
Part 1: swap
1. Check the current swap
swapon --show
free -h
df -h /
If swapon --show prints nothing, the server has no swap.
2. Create a swap file
Suggested size for a server: equal to RAM up to 2 GB, and 2 to 4 GB is enough for more RAM. Make sure the disk has enough free space. Example with 2 GB:
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
swapon --show
If fallocate fails, use sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress.
3. Make swap permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
4. Tune swappiness
vm.swappiness controls how eagerly the kernel uses swap (default 60). On a server a lower value is better, so swap is only used under real memory pressure:
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system
cat /proc/sys/vm/swappiness
Swap does not replace RAM; disk is far slower than memory. If free -h always shows a lot of swap in use, move to a bigger plan (Change plan on a cloud server).
Removing swap
sudo swapoff /swapfile
sudo sed -i '\|^/swapfile |d' /etc/fstab
sudo rm /swapfile
Part 2: timezone
1. Current state
timedatectl
Servers are usually delivered with Etc/UTC.
2. Switch to Tehran time
timedatectl list-timezones | grep -i tehran
sudo timedatectl set-timezone Asia/Tehran
timedatectl
date
The Time zone line should show Asia/Tehran (+0330, +0330). Use any other zone from timedatectl list-timezones the same way.
Many teams keep servers on UTC so logs from servers in different regions are comparable, and convert times only when showing them to users. If you have servers in several countries, UTC is the safer choice.
After changing the time zone, restart services that read the time zone at start (for example sudo systemctl restart cron rsyslog), and for PHP also set date.timezone in the PHP configuration.
3. Clock synchronization
timedatectl show -p NTPSynchronized
If it shows NTPSynchronized=no:
sudo timedatectl set-ntp true
systemctl status systemd-timesyncd --no-pager
An accurate clock is needed for TLS certificates, one-time codes (TOTP) and correctly ordered logs.
Part 3: hostname
1. Change the hostname
Pick a name with lowercase letters, digits and hyphens (for example web-1), or a fully qualified domain name such as web-1.example.com:
sudo hostnamectl set-hostname web-1
hostnamectl
2. Update /etc/hosts
So the new name resolves locally and sudo does not print unable to resolve host:
sudo sed -i '/^127\.0\.1\.1/d' /etc/hosts
echo '127.0.1.1 web-1' | sudo tee -a /etc/hosts
getent hosts web-1
3. Keeping it after reboots (cloud-init)
Ubuntu cloud images include cloud-init, which in some cases sets the hostname from the server's metadata at boot. To keep your name:
echo 'preserve_hostname: true' | sudo tee /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg
If cloud-init manages /etc/hosts (the file says so at the top), manual edits are lost on reboot; to keep them, edit the template /etc/cloud/templates/hosts.debian.tmpl, or add manage_etc_hosts: false to the same 99-preserve-hostname.cfg file.
To keep the server's name in the Arianet panel in line with the hostname, change it on the cloud server page's Settings tab.
Final check
After a reboot (sudo reboot):
swapon --show # /swapfile is active
cat /proc/sys/vm/swappiness # 10
timedatectl | grep 'Time zone' # Asia/Tehran
hostname # web-1
Common errors
| Error | Fix |
|---|---|
swapon: /swapfile: insecure permissions 0644 | sudo chmod 600 /swapfile |
swapon: /swapfile: swapon failed: Invalid argument | A file made with fallocate is not usable as swap on this filesystem; delete it and create it again with dd. |
sudo: unable to resolve host web-1 | The 127.0.1.1 web-1 line is missing from /etc/hosts. |
| The hostname reverts after a reboot | Set preserve_hostname: true for cloud-init (part 3). |
| cron still runs on UTC | sudo systemctl restart cron |
Next steps
- Firewall: ufw and the Arianet cloud firewall
- Install Docker and Docker Compose
- Create servers with Terraform and the CLI
Create your server
In Arianet Cloud you can apply these settings from the start with the "Startup script (cloud-init)" when creating a server, and move to a bigger plan whenever RAM runs short. In the Arianet panel, start from Cloud server / Cloud Computing → New server.