Introduction¶
In work, the Ubuntu system is frequently used for startup and automatic program execution. To facilitate future reference, this guide explains how to use /etc/rc.local for startup programs. The steps are applicable to Ubuntu 20.04 or Ubuntu 22.04 systems.
- Edit
/lib/systemd/system/rc-local.servicewith root privileges and add the following content:
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
- Assign permissions to this file:
sudo chmod 777 /lib/systemd/system/rc-local.service
- Create
/etc/rc.localwith root privileges and add the following content:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
echo "测试成功" > /home/test.log
exit 0
- Assign execution permissions to
/etc/rc.local:
sudo chmod +x /etc/rc.local
- Create a symbolic link for the service:
sudo ln -s /lib/systemd/system/rc-local.service /etc/systemd/system/
- Set the service to start on boot:
sudo systemctl enable rc-local.service
- Check the service status:
sudo systemctl status rc-local.service
- Restart the device and verify success. If the file
/home/test.logis created with the content “测试成功”, the setup is successful.