Low Orbit Flux Logo 2 F

Linux Command - sysctl

The linux command sysctl is used to modify kernel parameters at runtime.

Show everything:



sysctl -a

Load values from default config ( /etc/sysctl.conf )



sysctl -p

Load from another config file:



sysctl -p /usr/lib/sysctl.d/50-pid-max.conf

Show specific parameter:



sysctl vm.dirty_expire_centisecs

Set a parameter:



sysctl vm.dirty_expire_centisecs=3001

Just print the value ( good for scripts ):



sysctl vm.dirty_expire_centisecs -n

Just a list of parameter names:



sysctl -aN

Append a value to config after setting it:



sysctl net.ipv4.ip_forward=1 >> /etc/sysctl.conf

Couple more examples:



sysctl vm.swappiness=40
sysctl net.ipv4.ip_forward=1

Options:

-n don’t print key name
-e ignore unknown key errors
-N only print names
-q quiet, don’t display values set
-w write, when all arguments prescribe a key to be set ( useful???? needed ????)
-p load from /etc/sysctl.conf or specify a file, use ‘-‘ for stdin
-a display all values
–deprecated include deprecated params
-b binary, print values without a new line
–system Load settings from all system configuration files.
-r pattern only apply settings that matdch pattern ( regex )
-h help
-V version

More:

-A Alias of -a
-d Alias of -h
-f Alias of -p
-X Alias of -a
-o Does nothing, exists for BSD compatibility.
-x Does nothing, exists for BSD compatibility.

Configs may be read from these locations when using –system ( see man page for more details on how this works ).

Check with this:



ls -l /etc/sysctl.d/*.conf
ls -l /run/sysctl.d/*.conf
ls -l /usr/local/lib/sysctl.d/*.conf
ls -l /usr/lib/sysctl.d/*.conf
ls -l /lib/sysctl.d/*.conf
ls -l /etc/sysctl.conf

On my Debian 12 system:



root@host1:~# ls -l /etc/sysctl.d/*.conf
lrwxrwxrwx 1 root root 14 Jan 26 16:48 /etc/sysctl.d/99-sysctl.conf -> ../sysctl.conf
root@host1:~# 
root@host1:~# ls -l /run/sysctl.d/*.conf
ls: cannot access '/run/sysctl.d/*.conf': No such file or directory
root@host1:~# 
root@host1:~# ls -l /usr/local/lib/sysctl.d/*.conf
ls: cannot access '/usr/local/lib/sysctl.d/*.conf': No such file or directory
root@host1:~# 
root@host1:~# ls -l /usr/lib/sysctl.d/*.conf
-rw-r--r-- 1 root root  68 Jan 19  2023 /usr/lib/sysctl.d/30-tracker.conf
-rw-r--r-- 1 root root 403 Feb 28  2023 /usr/lib/sysctl.d/50-bubblewrap.conf
-rw-r--r-- 1 root root 649 Jan 26 16:35 /usr/lib/sysctl.d/50-pid-max.conf
-rw-r--r-- 1 root root 372 Dec 19  2022 /usr/lib/sysctl.d/99-protect-links.conf
root@host1:~# 
root@host1:~# 
root@host1:~# ls -l /lib/sysctl.d/*.conf
-rw-r--r-- 1 root root  68 Jan 19  2023 /lib/sysctl.d/30-tracker.conf
-rw-r--r-- 1 root root 403 Feb 28  2023 /lib/sysctl.d/50-bubblewrap.conf
-rw-r--r-- 1 root root 649 Jan 26 16:35 /lib/sysctl.d/50-pid-max.conf
-rw-r--r-- 1 root root 372 Dec 19  2022 /lib/sysctl.d/99-protect-links.conf
root@host1:~# 
root@host1:~# 
root@host1:~# 
root@host1:~# ls -l /etc/sysctl.conf
-rw-r--r-- 1 root root 2403 Apr 24 16:20 /etc/sysctl.conf
root@host1:~#