Help Center> Elastic Cloud Server> Troubleshooting> Linux ECS Issues> How Can I Make /etc/rc.local Run at Startup in CentOS 7?
Updated on 2022-07-15 GMT+08:00

How Can I Make /etc/rc.local Run at Startup in CentOS 7?

Symptom

The /etc/rc.local startup script does not run at startup of ECSs running CentOS 7 or EulerOS.

This section uses CentOS 7 as an example.

Possible Causes

The possible causes are as follows:

  • The /etc/rc.d/rc.local file is not executable in CentOS 7. For detailed measures, see Solution 1.

    /etc/rc.local is a symbolic link to /etc/rc.d/rc.local.

  • The route command in /etc/rc.local becomes invalid after CentOS 7 restart. This is because the network service is not started when the kernel reads /etc/rc.local to add a route during CentOS 7 startup. For detailed measures, see Solution 2.

Solution 1

The solution for non-executable /etc/rc.d/rc.local is as follows:

In CentOS 7, the /etc/rc.d/rc.local file is not executable by default.

  1. Run the following command to check whether file /etc/rc.d/rc.local is executable:

    # ls -l /etc/rc.d/rc.local

    -rw-r--r-- 1 root root 473 Sep 14 02:19 /etc/rc.d/rc.local

    As shown in the command output, the file is not executable.

  2. Run the following command to make /etc/rc.d/rc.local executable:

    # chmod +x /etc/rc.d/rc.local

Solution 2

The solutions for invalid route command in the /etc/rc.local after CentOS 7 reboot are as follows:

Method 1: Write the route into a static route configuration file. The route will not be lost even if the NIC is restarted.

  1. Go to the /etc/sysconfig/network-scripts/ directory and check whether file route-<interface> exists, where <interface> is the name of the interface related to the route.

    If it is, add the following content to the file.

    If it is not, create one and add the following content to the file:
    <network/prefix> via <gateway>

    where <network/prefix> is a remote network with prefix, and <gateway> is the next-hop IP address.

    For example, add a route to point to 10.20.30.0/24 from 192.168.100.10 to enable eth0 at system startup.

    # cat /etc/sysconfig/network-scripts/route-eth0

    10.20.30.0/24 via 192.168.100.10  
  2. Run the following command for the modifications to take effect:

    # ifup eth0

Method 2: The /etc/rc.d/rc.local file is controlled by the rc-local service. You can configure the rc-local service to start after network-online.target is started.

  1. Run the following command to view rc-local configuration file located in /usr/lib/systemd/system/rc-local.service:

    # cat /usr/lib/systemd/system/rc-local.service |grep -v "^#"

    Check whether parameters Requires and After exist in the [Unit] area.

    If they are, change their values to network-online.target.

    If they are not, add these two parameters and set their values to network-online.target.

    [Unit]
    Description=/etc/rc.d/rc.local Compatibility
    ConditionFileIsExecutable=/etc/rc.d/rc.local
    Requires=network-online.target
    After=network-online.target
    
    [Service]
    Type=forking
    ExecStart=/etc/rc.d/rc.local start
    TimeoutSec=0
    RemainAfterExit=yes

    network-online.target is a target that actively waits until the network is up, where the definition of up is defined by the network management software. Usually it indicates a configured, routable IP address of some kind. Its primary purpose is to actively delay activation of services until the network is set up.

  2. Run the following command to check whether file /etc/rc.d/rc.local is executable:

    # ls -l /etc/rc.d/rc.local

    If the output indicates that the file is not executable, make it executable by following the instructions provided in Solution 1.

  3. Run the following command to notify systemd to reload the configuration file:

    # systemctl daemon-reload

  4. Run the following command to restart rc-local.service to execute the /etc/rc.d/rc.local file:

    # systemctl restart rc-local.service