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.

  1. Edit /lib/systemd/system/rc-local.service with root privileges and add the following content:
[Install]
WantedBy=multi-user.target  
Alias=rc-local.service
  1. Assign permissions to this file:
sudo chmod 777 /lib/systemd/system/rc-local.service
  1. Create /etc/rc.local with 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
  1. Assign execution permissions to /etc/rc.local:
sudo chmod +x /etc/rc.local
  1. Create a symbolic link for the service:
sudo ln -s /lib/systemd/system/rc-local.service /etc/systemd/system/ 
  1. Set the service to start on boot:
sudo systemctl enable rc-local.service
  1. Check the service status:
sudo systemctl status rc-local.service
  1. Restart the device and verify success. If the file /home/test.log is created with the content “测试成功”, the setup is successful.
Xiaoye