The command is used to configure kernel parameters at runtime
Most frequent usage:
- Enable/disable IP forwarding
- Change/Edit network configuration for ipv4/ipv6
- Change/Edit kernel parameters like hostaname, domainname, max_lock_depth. ostype, pid_max etc
- Change/edit file system configurations like file-max, dir-notify-enable, overflowgid, inode-nr etc
- We can quickly set the kernel parameters in /proc/sys/ but it is useful for testing, these special settings within /proc/sys/ are lost when the machine is rebooted.
- To save these custom settings permanently, add them to the /etc/sysctl.conf file.
- Each time the system is booted, the init program runs the /etc/rc.d/rc.sysinit script. This script executes sysctl using /etc/sysctl.conf to determine the values passed to the kernel. Any changed made in /etc/sysctl.conf therefore take effect each time the system is booted.
Format:
sysctl [-n] [-e] variable ...
sysctl [-n] [-e] [-q] -w variable=value ...
sysctl [-n] [-e] [-q] -p <filename>
sysctl [-n] [-e] -a
sysctl [-n] [-e] -A
DESCRIPTION
sysctl is used to modify kernel parameters at runtime. It can edit values for following parameters:
localhost:/proc/sys:>ls
abi crypto debug dev fs kernel net sunrpc vm xen
|
To change a kernel parameter:
To set a key, use the form variable=value, where variable is the key and value is the value to set it to. If the value contains quotes or characters which are parsed by the shell, you may need to enclose the value in double quotes. This requires the -w parameter to use.
Options with their examples:
-n Use this option to disable printing of the key name when printing values.
[shanky@HYDBMW fs]$ sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1
[shanky@HYDBMW fs]$ sysctl -n net.ipv4.ip_forward
1
Note:- '/' can also be used in place of '.' for a variable. See below:
[shanky@HYDBMW sys]$ sysctl net/ipv4/ip_forward
net.ipv4.ip_forward = 1
-e Use this option to ignore errors about unknown keys.
[shanky@HYDBMW fs]$ sysctl -n net.ipv4.ip_forwar
error: "net.ipv4.ip_forwar" is an unknown key
[shanky@HYDBMW fs]$ sysctl -e net.ipv4.ip_forwar
[shanky@HYDBMW fs]$
-N Use this option to only print the names. It may be useful with shells that have programmable completion.
[shanky@HYDBMW fs]$ sysctl -N net.ipv4.ip_forward
net.ipv4.ip_forward
-q Use this option to not display the values set to stdout.
-w Use this option when you want to change a sysctl setting.
[shanky@HYDBMW sys]$ sysctl -w net.ipv4.ip_forward=0
error: permission denied on key 'net.ipv4.ip_forward'
-p Load in sysctl settings from the file specified or /etc/sysctl.conf if none given. All these setting can be done only by root user.See the file below:
[shanky@HYDBMW sys]$ head /etc/sysctl.conf
# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and
# sysctl.conf(5) for more details.
# Controls IP packet forwarding
net.ipv4.ip_forward = 0
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
-a Display all values currently available.
kernel.sched_child_runs_first = 0
kernel.sched_min_granularity_ns = 2000000
kernel.sched_latency_ns = 10000000
kernel.sched_wakeup_granularity_ns = 2000000
kernel.sched_tunable_scaling = 1
kernel.sched_features = 3183
kernel.sched_migration_cost = 500000
kernel.sched_nr_migrate = 32
kernel.sched_time_avg = 1000
kernel.sched_shares_window = 10000000
kernel.timer_migration = 1
-A Display all values currently available in table form
|