1. 程式人生 > 其它 >Ubuntu 20.04開機啟動配置

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就表示沒問題了