CentOS 7,8 – /etc/rc.local

以前會把需要在開機後執行的指令寫在 /etc/rc.local ,但在 CentOS7 / CentOS 8 會發現系統沒有去執行該檔案的內容.需要以下設定才會執行 /etc/rc.local 的內容.

測試環境 CentOS 7 / CentOS 8 x86_64 (虛擬機)
rc.local 有兩個地方,/etc/rc.local 是 Link 檔案,實際儲存在 /etc/rc.d/rc.local

[root@localhost ~]# ll /etc/rc.local 
lrwxrwxrwx 1 root root 13 Dec  9 20:43 /etc/rc.local -> rc.d/rc.local

[root@localhost ~]# ll /etc/rc.d/rc.local 
-rw-r--r-- 1 root root 473 Oct 19 00:48 /etc/rc.d/rc.local

把需要執行的內容寫在 /etc/rc.d/rc.local , 以下範例是增加 Routing Table .

[root@localhost ~]# cat /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
route add -net 172.16.0.0 netmask 255.255.255.0 gw 172.16.0.254

還需要把 rc.local 改成可執行 +x(Execute)

[root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]# ll /etc/rc.d/rc.local 
-rwxr-xr-x 1 root root 473 Oct 19 00:48 /etc/rc.d/rc.local

CentOS 7 與 CentOS 8 設定檔 /usr/lib/systemd/system/rc-local.service 內容稍微不同,以下分開說明.

CentOS 7

設定檔如下.

[root@localhost ~]# cat /usr/lib/systemd/system/rc-local.service
#  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.d/rc.local is executable.
[Unit]
Description=/etc/rc.d/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes

不需修改設定檔就可以直接執行 #systemctl enable (開機時啟動), start (立即啟動) rc-local .

[root@localhost ~]# systemctl enable rc-local
[root@localhost ~]# systemctl start rc-local
[root@localhost ~]# systemctl status rc-local.service
* rc-local.service - /etc/rc.d/rc.local Compatibility
   Loaded: loaded (/usr/lib/systemd/system/rc-local.service; static; vendor preset: disabled)
   Active: active (exited) since Thu 2020-02-06 18:35:46 CST; 5s ago
  Process: 3431 ExecStart=/etc/rc.d/rc.local start (code=exited, status=0/SUCCESS)

Feb 06 18:35:46 localhost.localdomain systemd[1]: Starting /etc/rc.d/rc.local Compatibility...
Feb 06 18:35:46 localhost.localdomain systemd[1]: Started /etc/rc.d/rc.local Compatibility.

的確多一筆 172.16.0.0 的 Routing Table.

[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         _gateway        0.0.0.0         UG    100    0        0 ens33
172.16.0.0      localhost.local 255.255.255.0   UG    0      0        0 ens33
192.168.111.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

CentOS 8

方式同上 ,但 rc-local.service 設定檔需要多新增 [Install] 區塊,內容為 WantedBy=multi-user.target ,完整設定如下.

[root@localhost ~]# cat /usr/lib/systemd/system/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.d/rc.local is executable.
[Unit]
Description=/etc/rc.d/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target

不需修改設定檔就可以直接執行 #systemctl enable (開機時啟動), start (立即啟動) rc-local .

[root@localhost ~]# systemctl enable rc-local.service
Created symlink /etc/systemd/system/multi-user.target.wants/rc-local.service → /usr/lib/systemd/system/rc-local.service.
[root@localhost ~]# systemctl start rc-local.service
[root@localhost ~]# systemctl status rc-local.service
● rc-local.service - /etc/rc.d/rc.local Compatibility
   Loaded: loaded (/usr/lib/systemd/system/rc-local.service; enabled; vendor preset: disabled)
   Active: active (exited) since Fri 2020-02-07 14:20:06 CST; 2s ago
     Docs: man:systemd-rc-local-generator(8)
  Process: 2741 ExecStart=/etc/rc.d/rc.local start (code=exited, status=0/SUCCESS)

Feb 07 14:20:06 localhost.localdomain systemd[1]: Starting /etc/rc.d/rc.local Compatibility...
Feb 07 14:20:06 localhost.localdomain systemd[1]: Started /etc/rc.d/rc.local Compatibility.

的確多一筆 172.16.0.0 的 Routing Table.

[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         _gateway        0.0.0.0         UG    100    0        0 ens33
172.16.0.0      localhost.local 255.255.255.0   UG    0      0        0 ens33
192.168.111.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

常見問題

[root@localhost ~]# systemctl enable rc-local.service
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:
1) A unit may be statically enabled by being symlinked from another unit's
   .wants/ or .requires/ directory.
2) A unit's purpose may be to act as a helper for some other unit which has
   a requirement dependency on it.
3) A unit may be started when needed via activation (socket, path, timer,
   D-Bus, udev, scripted systemctl call, ...).
4) In case of template units, the unit is meant to be enabled with some
   instance name specified.

rc-local.service 設定檔需新增 [Install] 區塊,內容為 WantedBy=multi-user.target ,完整設定如下.

[root@localhost ~]# vi /usr/lib/systemd/system/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.d/rc.local is executable.
[Unit]
Description=/etc/rc.d/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料