【Linux】rc.local 如何生效

The Problem

The scripts/commands in the configuration file /etc/rc.d/rc.local could not work at boot time in a CentOS/RHEL 7 system. Same used to work in earlier CentOS/RHEL versions. Is it depricated or is there a workaround to still use this method?

The Answer

The rc.local service is stopped by default in CentOS/RHEL 7. If you check the etc/rc.d/rc.local configuration file, there are hints about this.

# cat /etc/rc.d/rc.local
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

The WorkAround (解决方案)

  1. With systemd, the init scripts are not there any more. Consequently, the execution of tasks at boot time had to change. In CentOS/RHEL 7, the /etc/rc.d/rc.local file is controlled by rc-local service.
Description=/etc/rc.d/rc.local Compatibility

ExecStart=/etc/rc.d/rc.local start
  1. By default, the file /etc/rc.d/rc.local don’t have execution permission. Please append the execution permissions to this file.
# ls -l /etc/rc.d/rc.local
-rw-r--r--. 1 root root 473 Nov  8 00:20 /etc/rc.d/rc.local
# chmod +x /etc/rc.d/rc.local
# ls -l /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 Nov  8 00:20 /etc/rc.d/rc.local
  1. Enable rc.local service, to make sure it starts every time after a reboot.
# systemctl enable rc-local

Confirm whether the service is enabled:

# systemctl status rc-local.service
  1. Then, please start the rc-local service.
# systemctl start rc-local