Checking and Changing Kernel Parameters
sysctl reads and changes settings of the running kernel. Memory behaviour, networking and limits such as the number of file watches can be changed without rebuilding the kernel or rebooting.
The settings appear as files under /proc/sys/, and the sysctl command reads and writes them. The dots in a parameter name map to directories: vm.swappiness is /proc/sys/vm/swappiness.
Reading the Current Values
sysctl -a lists every parameter. This system has about 1,200 of them.
sysctl -aName one or more parameters to read them individually.
sysctl [PARAMETER]| Field | Value |
|---|---|
| [PARAMETER] | Parameter name, such as vm.swappiness |
kazulog@sv1:~$ sysctl -a 2>/dev/null | wc -l
1189
kazulog@sv1:~$ sysctl net.ipv4.ip_forward vm.swappiness fs.file-max fs.inotify.max_user_instances
net.ipv4.ip_forward = 0
vm.swappiness = 60
fs.file-max = 9223372036854775807
fs.inotify.max_user_instances = 128
kazulog@sv1:~$ cat /proc/sys/vm/swappiness
60
kazulog@sv1:~$sysctl vm.swappiness and cat /proc/sys/vm/swappiness print the same value because they read the same thing.
Changing a Value Temporarily
sysctl -w sets a value. Writing to the file under /proc/sys/ does the same.
sudo sysctl -w [PARAMETER]=[VALUE]| Field | Value |
|---|---|
| [PARAMETER] | Parameter to change |
| [VALUE] | Value to set |
kazulog@sv1:~$ sudo sysctl -w vm.swappiness=10
vm.swappiness = 10
kazulog@sv1:~$ sysctl vm.swappiness
vm.swappiness = 10
kazulog@sv1:~$ cat /proc/sys/vm/swappiness
10
kazulog@sv1:~$ echo 20 | sudo tee /proc/sys/vm/swappiness
20
kazulog@sv1:~$ sysctl vm.swappiness
vm.swappiness = 20kazulog@sv1:~$ uptime -p
up 0 minutes
kazulog@sv1:~$ sysctl vm.swappiness
vm.swappiness = 60Making a Setting Permanent
To keep a setting across reboots, create a .conf file under /etc/sysctl.d/. Ubuntu 26.04 has no /etc/sysctl.conf.
printf 'vm.swappiness = 10\nnet.core.somaxconn = 1024\n' | sudo tee /etc/sysctl.d/99-[NAME].conf| Field | Value |
|---|---|
| [NAME] | A name describing the settings (free choice) |
Creating the file is not enough. Run sysctl --system to read every configuration file again.
sudo sysctl --systemkazulog@sv1:~$ printf 'vm.swappiness = 10\nnet.core.somaxconn = 1024\n' | sudo tee /etc/sysctl.d/99-kazulog.conf
vm.swappiness = 10
net.core.somaxconn = 1024
kazulog@sv1:~$ sudo sysctl --system 2>&1 | tail -4
net.ipv6.conf.all.use_tempaddr = 0
net.ipv6.conf.default.use_tempaddr = 0
vm.swappiness = 10
net.core.somaxconn = 1024
kazulog@sv1:~$ sysctl vm.swappiness net.core.somaxconn
vm.swappiness = 10
net.core.somaxconn = 1024
kazulog@sv1:~$The values survive a reboot.
kazulog@sv1:~$ uptime -p
up 0 minutes
kazulog@sv1:~$ sysctl vm.swappiness net.core.somaxconn
vm.swappiness = 10
net.core.somaxconn = 1024The Order the Files Are Read
sysctl --system reads settings from several directories and prints each file as it applies it.
kazulog@sv1:~$ sudo sysctl --system 2>&1 | head -14
* Applying /usr/lib/sysctl.d/10-apparmor.conf ...
* Applying /usr/lib/sysctl.d/10-coredump-debian.conf ...
* Applying /usr/lib/sysctl.d/50-default.conf ...
* Applying /usr/lib/sysctl.d/50-pid-max.conf ...
* Applying /usr/lib/sysctl.d/55-bufferbloat.conf ...
* Applying /usr/lib/sysctl.d/55-console-messages.conf ...
* Applying /usr/lib/sysctl.d/55-ipv6-privacy.conf ...
* Applying /usr/lib/sysctl.d/55-kernel-hardening.conf ...
* Applying /usr/lib/sysctl.d/55-magic-sysrq.conf ...
* Applying /usr/lib/sysctl.d/55-map-count.conf ...
* Applying /usr/lib/sysctl.d/55-network-security.conf ...
* Applying /usr/lib/sysctl.d/55-ptrace.conf ...
* Applying /usr/lib/sysctl.d/55-zeropage.conf ...
* Applying /etc/sysctl.d/99-cloudimg-ipv6.conf ...
kazulog@sv1:~$| Directory | Purpose |
|---|---|
/usr/lib/sysctl.d/ | Settings shipped by packages. Do not edit |
/run/sysctl.d/ | Settings generated at run time |
/etc/sysctl.d/ | Where an administrator puts settings |
99- are common in /etc/sysctl.d/.Below, vm.swappiness is set in both a 10- and a 99- file. Both are applied, and the value from 99- is the one that remains.
kazulog@sv1:~$ echo 'vm.swappiness = 30' | sudo tee /etc/sysctl.d/10-kazulog-test.conf
vm.swappiness = 30
kazulog@sv1:~$ grep -H . /etc/sysctl.d/10-kazulog-test.conf /etc/sysctl.d/99-kazulog.conf
/etc/sysctl.d/10-kazulog-test.conf:vm.swappiness = 30
/etc/sysctl.d/99-kazulog.conf:vm.swappiness = 10
/etc/sysctl.d/99-kazulog.conf:net.core.somaxconn = 1024
kazulog@sv1:~$ sudo sysctl --system 2>&1 | grep -E 'kazulog|swappiness'
* Applying /etc/sysctl.d/10-kazulog-test.conf ...
* Applying /etc/sysctl.d/99-kazulog.conf ...
vm.swappiness = 30
vm.swappiness = 10
kazulog@sv1:~$ sysctl vm.swappiness
vm.swappiness = 10
kazulog@sv1:~$Confirming That a Setting Has an Effect
You can confirm more than the number: the behaviour changes too. The example uses fs.inotify.max_user_instances, which limits how many file watches one user may create at a time.
The script below starts three file watches and reports whether each one was created.
kazulog@sv1:~$ cat ino_test.sh
#!/bin/bash
# 監視を3つ起動して、それぞれ作成できたかを表示する
for i in 1 2 3; do
inotifywait -q -m /tmp > /dev/null 2>/tmp/ino$i.err &
sleep 1
if [ -s /tmp/ino$i.err ]; then echo "watcher $i: $(head -1 /tmp/ino$i.err)"; else echo "watcher $i: OK"; fi
done
sleep 1; pkill -f "inotifywait -q -m /tmp"; rm -f /tmp/ino*.err
kazulog@sv1:~$Lowering the limit to 2 makes the watches impossible to create; restoring the default of 128 makes them work again.
kazulog@sv1:~$ sysctl fs.inotify.max_user_instances
fs.inotify.max_user_instances = 128
kazulog@sv1:~$ sudo sysctl -w fs.inotify.max_user_instances=2
fs.inotify.max_user_instances = 2
kazulog@sv1:~$ ./ino_test.sh
watcher 1: Couldn't initialize inotify: Too many open files
watcher 2: Couldn't initialize inotify: Too many open files
watcher 3: Couldn't initialize inotify: Too many open files
kazulog@sv1:~$ sudo sysctl -w fs.inotify.max_user_instances=128
fs.inotify.max_user_instances = 128
kazulog@sv1:~$ ./ino_test.sh
watcher 1: OK
watcher 2: OK
watcher 3: OKReaching the limit produces Too many open files. Servers running many programs that watch files (backup and sync tools, container platforms) often need this limit raised.
Undoing a Setting
Delete the configuration file you created.
sudo rm /etc/sysctl.d/99-[NAME].confkazulog@sv1:~$ sudo rm /etc/sysctl.d/99-kazulog.conf
kazulog@sv1:~$ sudo sysctl --system > /dev/null 2>&1; sysctl vm.swappiness net.core.somaxconn
vm.swappiness = 10
net.core.somaxconn = 1024
kazulog@sv1:~$ ls /etc/sysctl.d/
99-cloudimg-ipv6.conf README.sysctlsysctl --system does not restore the old value. sysctl --system only applies what the configuration files contain; it does not reset parameters whose settings disappeared. In the example above vm.swappiness is still 10. Reboot, or set the default explicitly, to restore it.kazulog@sv1:~$ uptime -p
up 0 minutes
kazulog@sv1:~$ sysctl vm.swappiness net.core.somaxconn
vm.swappiness = 60
net.core.somaxconn = 4096Parameters in Common Use
| Parameter | Meaning |
|---|---|
net.ipv4.ip_forward | Enables packet forwarding. Set to 1 to use the server as a router |
net.ipv4.conf.all.rp_filter | Checks a packet’s source address against the routing table (reverse path filter) |
net.core.somaxconn | Upper bound of the connection backlog. Raised on servers with many connections |
net.ipv4.tcp_max_syn_backlog | Upper bound of the SYN queue |
vm.swappiness | How readily swap is used (0-100). Lower values avoid swapping |
vm.max_map_count | Memory maps one process may hold. Raised for databases and search engines |
fs.file-max | System-wide limit on open files |
fs.inotify.max_user_instances | File watches one user may create |
fs.inotify.max_user_watches | Files and directories one user may watch |
Test Environment and Session Logs
The examples were captured on Ubuntu 26.04 LTS Server running on CML. The session log of each step can be downloaded below.
| Step | Session log |
|---|---|
| Initial state (defaults and configuration files) | log |
| Temporary change and the order files are read | log |
| The value is lost after a reboot | log |
| Making a setting permanent | log |
| The setting survives a reboot | log |
| Which configuration file wins | log |
| Behaviour at different inotify limits | log |
| Deleting the configuration file | log |
| The state after deleting and rebooting | log |
Reference
sysctl.d(5) - systemd documentation
Related articles
- Changing the Hostname on Ubuntu 26.04 LTS Server (hostnamectl)
- Updating Packages on Ubuntu 26.04 LTS Server (apt update / upgrade)
- Setting the Timezone and Time Synchronisation on Ubuntu 26.04 LTS Server
- Creating Users and Granting sudo Privileges on Ubuntu 26.04 LTS Server
- Configuring the SSH Server on Ubuntu 26.04 LTS Server
- Managing Services with systemctl and Reading Logs with journalctl on Ubuntu 26.04 LTS Server
- Configuring Automatic Updates on Ubuntu 26.04 LTS Server (unattended-upgrades)
- Automating the Initial Setup of Ubuntu 26.04 LTS Server with cloud-init
- Configuring Kernel Parameters on Ubuntu 26.04 LTS Server (sysctl)
- Configuring the Network on Ubuntu 26.04 LTS Server (Netplan)
- Configuring Name Resolution on Ubuntu 26.04 LTS Server (systemd-resolved)
- Changing the NTP Source on Ubuntu 26.04 LTS Server (chrony)
- Static Routes on Ubuntu 26.04 LTS Server (Netplan)
- How to Install the Latest neovim from the Official Site on Ubuntu 26.04 LTS Server