Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

sysctl

Read and change kernel parameters at run time (the files under /proc/sys), and load them from config files.

Level
RHCSA exam
Group
Kernel and hardware
Needs root
For changes
Package
procps-ng

What it does

Every file in /proc/sys/ is a kernel setting; the dots in the name are slashes: net.ipv4.ip_forward = /proc/sys/net/ipv4/ip_forward. sysctl NAME reads it, sysctl -w NAME=VALUE changes it until reboot. For permanent values put NAME = VALUE lines in a file in /etc/sysctl.d/ and run sysctl --system.

How to write it

sysctl [OPTION]... [NAME[=VALUE]]...
sysctl -p [FILE]
sysctl --system

Options 14

Most used

-w, --write
Change a value: -w NAME=VALUE.
-a, --all
Show all settings (-A, -X are aliases).
-p[FILE], --load[=FILE]
Load settings from a file (default /etc/sysctl.conf).
--system
Load all files from all sysctl.d folders.
-n, --values
Only the values.
-N, --names
Only the names.
-r, --pattern REGEX
Only names that match.
-e, --ignore
Ignore unknown names.
-q, --quiet
Do not print values when setting.
-b, --binary
Values without a newline.
--deprecated
Also show deprecated names.
-f
Same as -p.
-h, --help
Show help.
-V, --version
Show the version.

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Read a value

    sysctl net.ipv4.ip_forward
    net.ipv4.ip_forward = 0

    Same as cat /proc/sys/net/ipv4/ip_forward.

  2. 2Change it now

    sysctl -w net.ipv4.ip_forward=1

    Lost at reboot.

  3. 3Make it permanent

    echo "net.ipv4.ip_forward = 1" > /etc/sysctl.d/90-forward.conf
    sysctl --system

    Files are read in name order; later wins.

  4. 4Only the value

    sysctl -n vm.swappiness
    60

    For scripts.

  5. 5Search settings

    sysctl -a 2>/dev/null | grep -i swappiness

    Or sysctl -a -r swap.

  6. 6Lower swapping on a database server

    echo "vm.swappiness = 10" > /etc/sysctl.d/90-swap.conf; sysctl --system

    The kernel keeps more in RAM.

  7. 7Network hardening (security)

    cat /etc/sysctl.d/90-hardening.conf
    net.ipv4.conf.all.accept_redirects = 0
    net.ipv4.conf.all.send_redirects = 0
    net.ipv4.conf.all.rp_filter = 1
    net.ipv4.tcp_syncookies = 1
    kernel.kptr_restrict = 2
    kernel.dmesg_restrict = 1

    Common CIS benchmark settings.

  8. 8Load one file

    sysctl -p /etc/sysctl.d/90-hardening.conf

    Test a new file.

  9. 9More open files for big servers

    sysctl -w fs.file-max=2097152

    System-wide limit (per-process limits are in limits.conf/systemd).

  10. 10Where does a setting come from?

    grep -r swappiness /etc/sysctl.d/ /usr/lib/sysctl.d/ /etc/sysctl.conf

    tuned can also set values (check the tuned profile).

Files

/etc/sysctl.d/*.conf
Your permanent settings.
/usr/lib/sysctl.d/
Defaults from packages.
/etc/sysctl.conf
Old main file (still read).
/proc/sys/
The live values.

Exit codes

0 :: Done. 1 :: Error. 255 :: Unknown setting or permission denied.