共有三种办法向内核传递参数以控制其行为:
- 在编译内核时:在内核的
config文件中。详见内核#编译。 - 在内核启动时:通过命令行参数(通常通过启动管理器设置)。
- 在运行时:通过
/proc/sys/(参见 sysctl)和/sys/中的文件。
/etc/modprobe.d/ 中的 .conf 文件设置。参见 Kernel module#Using files in /etc/modprobe.d/。不同的方式有不同的命令名称、是否可见以及设置方式。本文仅说明第二种方式(内核命令行),并记录适用 Arch linux 内核的常用参数。
大部分参数和某个子系统相关,需要在编译或加载时加入子系统,而且相关硬件需要存在。
内核命令行参数的格式为 parameter 或 parameter=value。
配置
内核参数可以在启动时临时修改,也可以永久性写到启动管理器的配置文件中,永远起作用。
下面示例:把参数quiet 和 splash 加到启动管理器 Syslinux, GRUB, GRUB Legacy 和 LILO中.
Syslinux
- 当出现启动选择菜单的时候,按
Tab进入修改模式:
linux /boot/vmlinuz-linux root=UUID=0a3407de-014b-458b-b5c1-848e92a327a3 rw initrd=/boot/initramfs-linux.img quiet splash
- 按
Enter键以当前设置启动.
- 要将设置永久生效,编辑
/boot/syslinux/syslinux.cfg并添加APPEND行:
APPEND root=UUID=0a3407de-014b-458b-b5c1-848e92a327a3 rw quiet splash
更多详情请见Syslinux 。
systemd-boot
- 当启动菜单出现时 按
e进入编辑界面:
initrd=\initramfs-linux.img root=UUID=0a3407de-014b-458b-b5c1-848e92a327a3 rw quiet splash
- 按
Enter即可按照输入的参数启动。
- 如果没有设置显示启动菜单, 你需要按住
Space启动电脑来进入启动菜单 。 - 如果不能够从启动菜单上进行编辑,修改
/boot/loader/loader.conf加入editor 1来开启编辑功能。
- 如果想永久加入参数,编辑
/boot/loader/entries/arch.conf(假设你已经设置好了 EFI system partition (简体中文)) 的options行:
options root=UUID=0a3407de-014b-458b-b5c1-848e92a327a3 rw quiet splash
更多信息请参见 systemd-boot .
GRUB
- 在菜单出现后按
e然后将它们添加至linux行:
linux /boot/vmlinuz-linux root=UUID=0a3407de-014b-458b-b5c1-848e92a327a3 rw quiet splash
- 按
b以便用这些参数启动。
- 要使改变在重启后仍生效,您可以手动编辑
/boot/grub/grub.cfg中的如上内容。对于初学者,建议:
- 编辑
/etc/default/grub并将您的内核选项添加至GRUB_CMDLINE_LINUX_DEFAULT行:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
- 然后重新生成
grub.cfg文件:
# grub-mkconfig -o /boot/grub/grub.cfg
有关配置GRUB的更多信息,请参阅 GRUB 。
GRUB Legacy
- Press
ewhen the menu shows up and add them on thekernelline:
kernel /boot/vmlinuz-linux root=UUID=0a3407de-014b-458b-b5c1-848e92a327a3 rw quiet splash
- Press
bto boot with these parameters.
- To make the change persistent after reboot, edit
/boot/grub/menu.lstand add them to thekernelline, exactly like above.
For more information on configuring GRUB Legacy, see the GRUB Legacy article.
LILO
- Add them to
/etc/lilo.conf:
image=/boot/vmlinuz-linux ... quiet splash
For more information on configuring LILO, see the LILO article.
rEFInd
- Press
+,F2, orInserton the desired menu entry and press it again on the submenu entry. Add kernel parameters at the end of the string:
root=UUID=0a3407de-014b-458b-b5c1-848e92a327a3 rw initrd=\boot\initramfs-linux.img quiet splash
- Press
Enterto boot with these parameters.
- To make the change persistent after reboot, edit
/boot/refind_linux.confand append them between the quotes in all required lines, for example
"Boot using default options" "root=UUID=0a3407de-014b-458b-b5c1-848e92a327a3 rw quiet splash"
- If you have disabled auto-detection of OSes in rEFInd and are defining OS stanzas instead in
esp/EFI/refind/refind.confto load your OSes, you can edit it like:
menuentry "Arch Linux" { ... options "root=UUID=0a3407de-014b-458b-b5c1-848e92a327a3 rw quiet splash" ... }
For more information on configuring rEFInd, see the rEFInd article.
EFISTUB
参阅 EFISTUB#Using UEFI directly.
dracut
dracut 可以将内核参数嵌入 initramfs, 跳过启动加载器的配置,参考: dracut#Kernel command line options.
Hijacking cmdline
Even without access to your bootloader it is possible to change your kernel parameters to enable debugging (if you have root access). This can be accomplished by overwriting /proc/cmdline which stores the kernel parameters. However /proc/cmdline is not writable even as root, so this hack is accomplished by using a bind mount to mask the path.
First create a file containing the desired kernel parameters
/root/cmdline
root=UUID=0a3407de-014b-458b-b5c1-848e92a327a3 ro console=tty1 logo.nologo debug
Then use a bind mount to overwrite the parameters
# mount -n --bind -o ro /root/cmdline /proc/cmdline
The -n option skips adding the mount to /etc/mtab, so it will work even if root is mounted read-only. You can cat /proc/cmdline to confirm that your change was successful.
参数列表
本表并不完整。所有选项的完整列表请参见内核文档。
| 参数 | 描述 |
|---|---|
| init | Run specified binary instead of /sbin/init as init process. The systemd-sysvcompat package symlinks /sbin/init to /usr/lib/systemd/systemd to use systemd. Set it to /bin/sh to boot to the shell.
|
| initrd | Specify the location of the initial ramdisk. For UEFI boot managers and EFISTUB, the path must be specified using backslashes (\) as path separators.
|
| cryptdevice | Specify the location of a dm-crypt-encrypted partition plus a device mapper name. |
| debug | Enable kernel debugging (events log level). |
| lsm | Set the initialisation order of the Linux security modules, used to enable AppArmor, SELinux or TOMOYO. |
| maxcpus | Maximum number of processors that an SMP kernel will bring up during bootup. |
| mem | Force usage of a specific amount of memory to be used. |
| netdev | Network devices parameters. |
| nomodeset | Disable Kernel mode setting. |
| panic | Time before automatic reboot on kernel panic. |
| resume | Specify a swap device to use when waking from hibernation. |
| ro | Mount root device read-only on boot (default1). |
| root | Root filesystem. See init/do_mounts.c for kernel's supported device name formats. Note that an initramfs with udev supports more name formats. |
| rootflags | Root filesystem mount options. Useful for setting options that cannot be applied by remounting (i.e. by systemd-remount-fs.service(8)). For example, the discard option of an XFS root volume.
|
| rw | Mount root device read-write on boot. |
| systemd.unit | Boot to a specified target. |
| video | Override framebuffer video defaults. |
1 mkinitcpio uses ro as default value when neither rw or ro is set by the boot loader. Boot loaders may set the value to use, for example GRUB uses rw by default (see FS#36275 as a reference).