赞助商广告

linux操作系统 sysctl命令手册

sysctl

时动态地修改内核的运行参数

补充说明

sysctl命令 被用于在内核运行时动态地修改内核的运行参数,可用的内核参数在目录/proc/sys中。它包含一些TCP/ip堆栈和虚拟内存系统的高级选项, 这可以让有经验的管理员提高引人注目的系统性能。用sysctl可以读取设置超过五百个系统变量。

语法

sysctl(选项)(参数)

选项

-n:打印值时不打印关键字;
-e:忽略未知关键字错误;
-N:仅打印名称;
-w:当改变sysctl设置时使用此项;
-p:从配置文件“/etc/sysctl.conf”加载内核参数设置;
-a:打印当前所有可用的内核参数变量和值;
-A:以表格方式打印当前所有可用的内核参数变量和值。

参数

变量=值:设置内核参数对应的变量值。

实例

查看所有可读变量:

sysctl -a

读一个指定的变量,例如kern.maxproc

sysctl kern.maxproc kern.maxproc: 1044

要设置一个指定的变量,直接用variable=value这样的语法:

sysctl kern.maxfiles=5000
kern.maxfiles: 2088 -> 5000

您可以使用sysctl修改系统变量,也可以通过编辑sysctl.conf文件来修改系统变量。sysctl.conf看起来很像rc.conf。它用variable=value的形式来设定值。指定的值在系统进入多用户模式之后被设定。并不是所有的变量都可以在这个模式下设定。

sysctl变量的设置通常是字符串、数字或者布尔型。(布尔型用 1 来表示'yes',用 0 来表示'no')。

sysctl -w kernel.sysrq=0
sysctl -w kernel.core_uses_pid=1
sysctl -w net.ipv4.conf.default.accept_redirects=0
sysctl -w net.ipv4.conf.default.accept_source_route=0
sysctl -w net.ipv4.conf.default.rp_filter=1
sysctl -w net.ipv4.tcp_syncookies=1
sysctl -w net.ipv4.tcp_max_syn_backlog=2048
sysctl -w net.ipv4.tcp_fin_timeout=30
sysctl -w net.ipv4.tcp_synack_retries=2
sysctl -w net.ipv4.tcp_keepalive_time=3600
sysctl -w net.ipv4.tcp_window_scaling=1
sysctl -w net.ipv4.tcp_sack=1

配置sysctl

编辑此文件:/etc/sysctl.conf

如果该文件为空,则输入以下内容,否则请根据情况自己做调整:

# Controls source route verification
# Default should work for all interfaces
net.ipv4.conf.default.rp_filter = 1
# net.ipv4.conf.all.rp_filter = 1
# net.ipv4.conf.lo.rp_filter = 1
# net.ipv4.conf.eth0.rp_filter = 1

# Disables IP source routing
# Default should work for all interfaces
net.ipv4.conf.default.accept_source_route = 0
# net.ipv4.conf.all.accept_source_route = 0
# net.ipv4.conf.lo.accept_source_route = 0
# net.ipv4.conf.eth0.accept_source_route = 0

# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0

# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1

# Increase maximum amount of memory allocated to shm
# Only uncomment if needed!
# kernel.shmmax = 67108864

# Disable ICMP Redirect Acceptance
# Default should work for all interfaces
net.ipv4.conf.default.accept_redirects = 0
# net.ipv4.conf.all.accept_redirects = 0
# net.ipv4.conf.lo.accept_redirects = 0
# net.ipv4.conf.eth0.accept_redirects = 0

# enable Log Spoofed Packets, Source Routed Packets, Redirect Packets
# Default should work for all interfaces
net.ipv4.conf.default.log_martians = 1
# net.ipv4.conf.all.log_martians = 1
# net.ipv4.conf.lo.log_martians = 1
# net.ipv4.conf.eth0.log_martians = 1

# Decrease the time default value for tcp_fin_timeout connection
net.ipv4.tcp_fin_timeout = 25

# Decrease the time default value for tcp_keepalive_time connection
net.ipv4.tcp_keepalive_time = 1200

# Turn on the tcp_window_scaling
net.ipv4.tcp_window_scaling = 1

# Turn on the tcp_sack
net.ipv4.tcp_sack = 1

# tcp_fack should be on because of sack
net.ipv4.tcp_fack = 1

# Turn on the tcp_timestamps
net.ipv4.tcp_timestamps = 1

# Enable TCP SYN Cookie Protection
net.ipv4.tcp_syncookies = 1

# Enable ignoring broadcasts request
net.ipv4.icmp_echo_ignore_broadcasts = 1

# Enable bad error message Protection
net.ipv4.icmp_ignore_bogus_error_responses = 1

# make more local ports available
# net.ipv4.ip_local_port_range = 1024 65000

# set TCP Re-Ordering value in kernel to ‘5′
net.ipv4.tcp_reordering = 5

# Lower syn retry rates
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 3

# Set Max SYN Backlog to ‘2048′
net.ipv4.tcp_max_syn_backlog = 2048

# Various Settings
net.core.netdev_max_backlog = 1024

# Increase the maximum number of skb-heads to be cached
net.core.hot_list_length = 256

# Increase the tcp-time-wait buckets pool size
net.ipv4.tcp_max_tw_buckets = 360000

# This will increase the amount of memory available for socket input/output queues
net.core.rmem_default = 65535
net.core.rmem_max = 8388608
net.ipv4.tcp_rmem = 4096 87380 8388608
net.core.wmem_default = 65535
net.core.wmem_max = 8388608
net.ipv4.tcp_wmem = 4096 65535 8388608
net.ipv4.tcp_mem = 8388608 8388608 8388608
net.core.optmem_max = 40960

如果希望屏蔽别人 ping 你的主机,则加入以下代码:

# Disable ping requests
net.ipv4.icmp_echo_ignore_all = 1

编辑完成后,请执行以下命令使变动立即生效:

/sbin/sysctl -p
/sbin/sysctl -w net.ipv4.route.flush=1

linux命令手册

btrfs subvolume 管理 btrfs 子卷和快照。
aurvote 为 AUR 中的包投票。
apt-add-repository 管理 apt 仓库。
cpuid 显示有关所有 CPU 的详细信息。
blkdiscard 丢弃存储设备上的设备扇区。对 SSD 有用。
adduser 添加用户的工具。
i3 一款动态平铺窗口管理器。
chage 更改用户账户和密码到期信息。
unix2mac 将 Unix 样式的行尾更改为 macOS 样式。
btrfs 一种基于写时复制(COW)原理的 Linux 文件系统。
anbox 在任何 GNU/Linux 操作系统上运行安卓应用。
asterisk 电话和交换(手机)服务器。
ul 执行文本的下划线。
grub-install 安装 GRUB 到设备。
sacctmgr 查看、配置、管理 Slurm 账户。
cp 复制文件和目录。
ubuntu-bug 这是 `apport-bug` 命令的一个别名。
acpi 显示电池状态或热量信息。
apt-cache Debian 和 Ubuntu 的包查询工具。
konsole Konsole: KDE 终端模拟器。
ascii 显示 ASCII 字符的别名。
cal 打印日历信息,高亮显示当前日期。
avahi-browse 显示通过 mDNS/DNS-SD 暴露在本地网络的服务和主机。
readelf 显示 EFI 文件信息。
archlinux-java 提供 Java 环境设置功能的一个帮助脚本。
mac2unix 将 macOS 样式的行尾更改为 Unix 样式。
a2disconf 在基于 Debian 的操作系统上禁用 Apache 配置文件。
archinstall Arch Linux 引导安装程序。
amixer ALSA 声卡驱动程序的混合器。
addr2line 将二进制文件地址转换成文件名和行数。
bluetoothd 管理蓝牙设备的守护进程。
arithmetic 测试见到你的算术问题。
a2ensite 在基于 Debian 的操作系统上启用 Apache 虚拟主机。
btrfs filesystem 管理 btrfs 文件系统。
openvpn3 OpenVPN 3 Linux 客户端。
ark KDE 归档工具。
apport-bug 在 Ubuntu 上提交错误报告。
poweroff 关闭系统。
betterlockscreen 简洁的锁屏程序。
grub-mkconfig 生成GRUB配置文件。
apt-mark 修改已安装软件包状态的工具。
bluetoothctl 从命令行管理蓝牙设备。
lvs 显示逻辑卷信息。
bpftrace Linux eBPF 的高级跟踪语言。
dmesg 显示或控制内核环形缓冲区。
aura Aura 包管理器:一个安全且支持多语言的 Arch Linux 和 AUR 的包管理器。
unix2dos 将 Unix 样式的行尾更改为 DOS 样式。
bitwise 支持动态基数转换和位操作的多基数交互式计算器。
apache2ctl Apache HTTP web 服务器命令行管理工具。
archey 一个可以以新颖的方式显示系统信息的简单工具。
batcat 这是 `bat` 命令的一个别名。
a2dismod 在基于 Debian 的操作系统上禁用 Apache 模块。
line 读取单行输入。
a2query 在基于 Debian 的操作系统上查看 Apache 运行配置。
aptitude Debian 和 Ubuntu 上的软件包管理工具。
btrfs device 管理 btrfs 文件系统中的设备。
at 在指定时间执行命令。
arecored ALSA 声卡驱动的声音录制器。
megadl 这是 `megatools-dl` 命令的一个别名。
pacman Arch Linux 的软件包管理器工具。
as 一个可移植的 GUN 汇编器。
boltctl 控制雷电(thunderbolt)设备。
apt-file 在 apt 软件包中查找文件,其中也包括未安装的软件。
w 显示登录者及其进程。
yay Yet Another Yogurt: 一个用于 Arch Linux 的工具,用于从 Arch User Repository 中构建和安装软件包。
apt-get Debian 和 Ubuntu 的软件包管理工具。
flatpak 构建、安装和运行 Flatpak 应用和运行时。
arch-chroot 辅助 Arch Linux 安装流程的更强 `chroot` 命令。
reboot 重新启动系统。
alpine 一个电子邮件客户端和 usenet 新闻组程序,具有 pico/nano 风格的界面。
aurman 用来构建和安装 AUR 包的 Arch Linux 实用工具。
autorandr 自动调节屏幕布局。
ac 打印用户连接时长数据。
ip-route-list 这是 `ip-route-show` 命令的一个别名。
iptables 可用于配置 Linux 内核防火墙提供的过滤表、规则链和规则的程序。
aplay ALSA 声卡驱动程序的命令行声音播放器。
dos2unix 将 DOS 样式的行尾更改为 Unix 样式。
xcowsay 在您的 Linux 桌面上显示一头可爱的牛和指定的消息。
asciiart 将图像转换为 ASCII.
apt-key Debian 和 Ubuntu 上的 APT 软件包管理器的密钥管理工具。
abbr 管理 fish shell 的缩写。
lsattr 列出 Linux 系统下的文件属性。
add-apt-repository apt 仓库管理。
a2enmod 在基于 Debian 的操作系统上启用 Apache 模块。
balooctl KDE Plasma 的文件索引和搜索框架。
diff3 逐行比较三个文件。
debuild 从源代码构建 `Debian` 软件包的工具。
dmenu 动态菜单。
ncal 这是 `cal` 命令的一个别名。
cc 这是 `gcc` 命令的一个别名。
yaourt Arch Linux 中用于从 Arch User Repository 中构建软件包的工具。
apt 基于 Debian 的发行版上的软件包管理工具。
zypper SUSE & openSUSE 的软件包管理工具。
auracle 用来和 Arch Linux 用户仓库交互的命令行工具,这个仓库通常被称作 AUR.
brctl 以太网桥管理。
a2dissite 在基于 Debian 的操作系统上禁用 Apache 虚拟主机。
aspell 交互式拼写检查工具。
authconfig 用于设置系统认证资源的命令行界面。
btrfs scrub 清理 btrfs 文件系统以验证数据完整性。
bmon 监控带宽并捕获网络相关统计信息。
beep 让电脑扬声器发出哔哔声的实用程序。
brightnessctl GUN/Linux 操作系统上用来读取和控制设备亮度的实用工具。
addpart 将特定分区的存在告知 Linux 内核。
debootstrap 创建一个基本的 `Debian` 系统。
alternatives 这是 `update-alternatives` 命令的一个别名。
bpytop 用图形显示有关正在运行的进程的动态实时信息。类似于 `gtop` 和 `htop`.
flameshot 带有 GUI 界面的 Screenshot 工具。
bspwm 这是 `bspc` 命令的一个别名。
cgroups 这是 `cgclassify` 命令的一个别名。
logsave 将一个命令的输出保存在日志文件中。
a2enconf 在基于 Debian 的操作系统上启用 Apache 配置文件。
apk Alpine Linux 的包管理工具。
blkid 列出所有已识别的分区及其通用唯一标识符 (UUID)。