Linux上的sysctl
2024-07-21 02:37:39
供稿:網友
我們經常在 linux 的 /PRoc/sys 目錄下,手動設定一些 kernel 的參數或是直接 echo 特定的值給一個 proc下的虛擬檔案,俾利某些檔案之開啟,常見的例如設定開機時自動啟動 ip Forwarding:
echo “1” > /proc/sys/net/ipv4/ip_forward
其實,在 Linux 我們還可以用 sysctl command 便可以簡易的去檢視、設定或自動配置 特定的 kernel 設定。我們可以在系統提示符號下輸入「sysctl -a」,摘要如后:abi.defhandler_coff = 117440515
dev.raid.speed_limit_max = 100000
net.ipv4.conf.default.send_redirects = 1
net.ipv4.conf.default.secure_redirects = 1
net.ipv4.conf.default.accept_redirects = 1
net.ipv4.conf.default.mc_forwarding = 0
net.ipv4.neigh.lo.delay_first_probe_time = 5
net.ipv4.neigh.lo.base_reachable_time = 30
net.ipv4.icmp_ratelimit = 100
net.ipv4.inet_peer_gc_mintime = 10
net.ipv4.igmp_max_memberships = 20
net.ipv4.ip_no_pmtu_disc = 0
net.core.no_cong_thresh = 20
net.core.netdev_max_backlog = 300
net.core.rmem_default = 65535
net.core.wmem_max = 65535
vm.kswapd = 512 32 8
vm.overcommit_memory = 0
vm.bdflush = 30 64 64 256 500 3000 60 0 0
vm.freepages = 351 702 1053
kernel.sem = 250 32000 32 128
kernel.panic = 0
kernel.domainname = (none)
kernel.hostname = pc02.shinewave.com.tw
kernel.version = #1 Tue Oct 30 20:11:04 EST 2001
kernel.osrelease = 2.4.9-13
kernel.ostype = Linux
fs.dentry-state = 1611 969 45 0 0 0
fs.file-nr = 1121 73 8192
fs.inode-state = 1333 523 0 0 0 0 0
從上述的語法我們大概可看出 sysctl 的表示法乃把目錄結構的「/」以「.」表示,一層一層的連結下去。當然以echo 特定的值給一個 proc下的虛擬檔案也是可以用 sysctl加以表示,例如:
#sysctl –w net.ipv4.ip_forward =”1”
或是直接在 /etc/sysctl.conf 增刪修改特定檔案的 0,1值亦可:
# Enables packet forwarding
net.ipv4.ip_forward = 1
# Enables source route verification
net.ipv4.conf.default.rp_filter = 1
# Disables the magic-sysrq key
kernel.sysrq = 0
當然假如考慮 reboot 后仍有效, 直接在 /etc/sysctl.conf 增刪修改特定檔案的 0,1值才可使之保留設定(以RedHat 為例,每次開機系統啟動后, init 會執行 /etc/rc.d/rc.sysinit,便會使用 /etc/sysctl.conf 的預設值去執行 sysctl)。
相關參考檔案:
/sbin/sysctl
/etc/sysctl.conf
sysctl 及sysctl.conf manpage
/usr/src/linux-x.y.z/Documentation/sysctl/*
/usr/share/doc/kernel-doc-x.y.z/sysctl/* (RedHat) right">(出處:清風軟件下載學院)