Alibaba Cloud Linux 4 uses systemd as its init system and does not ship the traditional /etc/rc.local file by default. To run a custom script automatically at system startup, create an rc.local file and register it as a systemd service.
Why /etc/rc.local is missing
Cause
Alibaba Cloud Linux 4 uses systemd instead of SysV init. In a systemd-based environment, /etc/rc.local and /etc/rc.d/rc.local are not provided by default. Scripts that depend on these files do not run at boot.
[root@iZ2zeeczb8vi6l8sot5webZ ~]# cat /etc/rc.d/init.d/README
You are looking for the traditional init scripts in /etc/rc.d/init.d,
and they are gone?
Here's an explanation on what's going on:
You are running a systemd-based OS where traditional init scripts have
been replaced by native systemd services files. Service files provide
very similar functionality to init scripts. To make use of service
files simply invoke "systemctl", which will output a list of all
currently running services (and other units). Use "systemctl
list-unit-files" to get a listing of all known unit files, including
stopped, disabled and masked ones. Use "systemctl start
foobar.service" and "systemctl stop foobar.service" to start or stop a
service, respectively. For further details, please refer to
systemctl(1).
Note that traditional init scripts continue to function on a systemd
system. An init script /etc/rc.d/init.d/foobar is implicitly mapped
into a service unit foobar.service during system initialization.
Thank you!
Further reading:
man:systemctl(1)
man:systemd(1)
https://0pointer.de/blog/projects/systemd-for-admins-3.html
https://www.freedesktop.org/wiki/Software/systemd/Incompatibilities
This behavior will change in the upcoming Alibaba Cloud Linux 4.0.3 release, after which a systemd component update will restore rc.local compatibility automatically.
Set up a startup script
The following procedure creates /etc/rc.local and registers it as a systemd service so that it runs automatically at each boot.
Before you begin, verify that you have sudo access on the instance.
Step 1. Create /etc/rc.local.
sudo cat >/etc/rc.local <<'EOF'
#!/bin/bash
# Add your startup commands here
# Example:
# echo "System booted at $(date)" >> /var/log/boot.log
# /usr/local/bin/my-custom-service start
exit 0
EOF
Step 2. Grant executable permissions.
chmod +x /etc/rc.local
Step 3. Create the rc-local.service unit file.
This file tells systemd to run /etc/rc.local after the system reaches multi-user.target.
sudo cat >/etc/systemd/system/rc-local.service <<'EOF'
[Unit]
Description=/etc/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.local
After=network-online.target
Wants=network-online.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
StandardOutput=tty
StandardError=tty
TTYPath=/dev/tty1
[Install]
WantedBy=multi-user.target
EOF
The After=network-online.target and Wants=network-online.target directives ensure your script runs only after the network is available. Remove them if your script does not require network access.
Step 4. Reload the systemd configuration.
systemctl daemon-reload
Step 5. Enable and start the service.
systemctl start rc-local
systemctl enable rc-local
Step 6. Verify the service.
systemctl status rc-local
journalctl -u rc-local -b
systemctl status should show Active: active (exited). journalctl lists log entries from your startup script on the most recent boot.
After these steps, the commands in /etc/rc.local run automatically at each startup.
Alternative: use the compatibility layer
On some Alibaba Cloud Linux 4 instances, a compatibility layer for /etc/rc.d/rc.local may already be present. When it is, create an rc.local file in the /etc/rc.d/ directory and grant it executable permissions — systemd registers the service automatically.
sudo touch /etc/rc.d/rc.local
sudo chmod +x /etc/rc.d/rc.local
Add your startup commands to /etc/rc.d/rc.local. If this does not work on your instance, follow the full procedure above.