Ubuntu 20.04開機啟動配置


Ubuntu升級到了20.04之后,默認的rc.local是不啟動的,這也就導致了,我配置的一些自定義的腳本是沒有辦法在開機的時候就自動啟動了。

rc-local.service默認內容如下

#  SPDX-License-Identifier: LGPL-2.1+
# 
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

這個時候啟動的時候會提示沒有Install,所以在如上的腳本上增加【Install】,下面是增加完【install】的全量腳本

#  SPDX-License-Identifier: LGPL-2.1+
# 
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target
Alias=rc-local.service

根據上面代碼中ExecStart,我們編輯/etc/rc.local腳本

#!/bin/sh

iptables-restore < /etc/iptables.up.rules  #你需要開機自啟動的腳本或者命令

exit 0

這里一定要有exit的退出,否則會報錯的。

然后把rc-local.service設置為開機啟動

systemctl start rc-local # 啟動
systemctl restart rc-local # 重啟
systemctl status rc-local # 查看狀態,如果是active就表示沒問題了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM