# 前言
因爲在工作中,Ubuntu系統經常使用到開機啓動,爲了方便之後使用,特此介紹下如何使用/etc/rc.local來開機啓動程序,步驟如下,針對Ubuntu20.04或Ubuntu22.04系統都有效。
- 使用root權限編輯
/lib/systemd/system/rc-local.service,添加以下內容:
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
- 並賦予該文件權限:
sudo chmod 777 /lib/systemd/system/rc-local.service
- 使用root權限創建
/etc/rc.local,添加以下內容:
#!/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
- 並賦予
/etc/rc.local該文件執行權限:
sudo chmod +x /etc/rc.local
- 創建服務的軟鏈接:
sudo ln -s /lib/systemd/system/rc-local.service /etc/systemd/system/
- 設置服務爲開機啓動:
sudo systemctl enable rc-local.service
- 檢測服務狀態:
sudo systemctl status rc-local.service
- 重啓設備,檢測是否成功。如果已經創建了文件
/home/test.log,並寫入內容測試成功,則說明已經成功。