大网数据广告活动
服务器运维问答

sysctl vs writing directly to /proc/*

· 52CE 我爱测

On a typical linux machine you can change kernel configuration by modifying the files located at /proc/* . For example, for the IPv6 accept_dad parameter of a specific network interface (say, eth0 ), you'd modify the following fi…

问题原文

原帖链接 · Adelin · CC BY-SA 4.0

On a typical linux machine you can change kernel configuration by modifying the files located at /proc/*.

For example, for the IPv6 accept_dad parameter of a specific network interface (say, eth0), you'd modify the following file:

/proc/sys/net/ipv6/conf/eth0/accept_dad

But, as I recently discovered, there is the widely spread tool, sysctl, which has the same purpose, and works like so:

sysctl -w net.ipv6.conf.eth0.accept_dad=1

My question is, when should we use which tool? My instinct says that if you know what you're doing, you should write to file directly but, if you would like validations and what not, you should use sysctl.

Since sysctl is yet another layer over something that we can control directly, I think by using it we're exposing ourselves to potential bugs that are otherwise avoided by writing to files directly.

已采纳解答

原帖链接 · Grodriguez · CC BY-SA 4.0

sysctl is a tool for reading and modifying various kernel attributes. It is available in many Unix-like operating systems, including not only Linux, but also OpenBSD and FreeBSD, for example. sysctl is typically available both as a shell command and as a system call.

In Linux, the sysctl mechanism is additionally exposed as part of the procfs virtual filesystem, under /proc/sys.

Note that the sysctl syscall is deprecated in Linux; it is recommended to use /proc/sys instead (either directly or via the sysctl shell command).

References:

  • Manpage for the sysctl syscall in Linux
  • Manpage for the sysctl shell command in Linux

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-12-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。