概述
本文檔主要記錄Ubuntu
系統(tǒng)使用/etc/rc.local
設(shè)置開機(jī)指令的方法。
系統(tǒng)版本
Ubuntu 20.04
設(shè)置開機(jī)啟動(dòng)命令
1. 查看rc-local服務(wù)狀態(tài)
sudo systemctl status rc-local
輸出結(jié)果如下:
root@localhost:~$ systemctl status rc-local
● rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled)
Drop-In: /usr/lib/systemd/system/rc-local.service.d
└─debian.conf
Active: inactive (dead)
Docs: man:systemd-rc-local-generator(8)
2. 設(shè)置rc-local服務(wù)開機(jī)啟動(dòng)
sudo systemctl enable rc-local
注: Ubuntu 20.04不能直接啟用
rc-local
服務(wù),會(huì)報(bào)錯(cuò),需要手動(dòng)創(chuàng)建rc-local
服務(wù)文件。指令報(bào)錯(cuò)如下:
root@localhost:~$ sudo systemctl enable rc-local
[sudo] password for simple:
The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.
Possible reasons for having this kind of units are:
? A unit may be statically enabled by being symlinked from another unit's
.wants/ or .requires/ directory.
? A unit's purpose may be to act as a helper for some other unit which has
a requirement dependency on it.
? A unit may be started when needed via activation (socket, path, timer,
D-Bus, udev, scripted systemctl call, ...).
? In case of template units, the unit is meant to be enabled with some
instance name specified.
3. 手動(dòng)創(chuàng)建系統(tǒng)自啟動(dòng)服務(wù)
sudo vim /etc/systemd/system/rc-local.service
輸入以下內(nèi)容:
[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
4. 創(chuàng)建rc.local文件
sudo vim /etc/rc.local
輸入以下內(nèi)容:
#!/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.
# Print the IP address
# _IP=$(hostname -I) || true
# if [ "$_IP" ]; then
# printf "My IP address is %s\n" "$_IP"
# fi
exit 0
5. 設(shè)置rc.local文件權(quán)限
sudo chmod +x /etc/rc.local
6.添加開機(jī)啟動(dòng)命令
以刪除某條冗余靜態(tài)路由為例,例如,現(xiàn)在有雙網(wǎng)卡環(huán)境,需要將其中一張網(wǎng)卡用于內(nèi)網(wǎng)數(shù)據(jù)傳輸,另外一張網(wǎng)卡用于訪問互聯(lián)網(wǎng)數(shù)據(jù)傳輸,但是在配置完成后,發(fā)現(xiàn)內(nèi)網(wǎng)數(shù)據(jù)傳輸正常,但是互聯(lián)網(wǎng)數(shù)據(jù)傳輸異常,經(jīng)過排查發(fā)現(xiàn),是因?yàn)樵谂渲脙?nèi)網(wǎng)網(wǎng)卡時(shí),誤將內(nèi)網(wǎng)網(wǎng)卡的默認(rèn)網(wǎng)關(guān)設(shè)置為了內(nèi)網(wǎng)網(wǎng)關(guān),導(dǎo)致互聯(lián)網(wǎng)數(shù)據(jù)傳輸異常,因此需要?jiǎng)h除該條冗余靜態(tài)路由。刪除指令為sudo route del -net 0.0.0.0 dev enp0s8
,現(xiàn)在希望在開機(jī)時(shí)自動(dòng)執(zhí)行該指令,因此需要將該指令添加到rc.local
文件中。
sudo vim /etc/rc.local
在exit 0
前添加以下內(nèi)容:文章來源:http://www.zghlxwxcb.cn/news/detail-500140.html
sudo route del -net 0.0.0.0 dev enp0s8
7.啟用rc-local服務(wù)
sudo systemctl start rc-local
sudo systemctl enable rc-local
8.查看rc-local服務(wù)狀態(tài)
sudo systemctl status rc-local
9.重啟系統(tǒng)
sudo reboot
系統(tǒng)完成重啟后,可查看路由表,發(fā)現(xiàn)冗余靜態(tài)路由已被刪除。文章來源地址http://www.zghlxwxcb.cn/news/detail-500140.html
到了這里,關(guān)于ubuntu設(shè)置開機(jī)啟動(dòng)命令的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!