以前會把需要在開機後執行的指令寫在 /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 (正統的作法是新增網卡的 Routing 檔案 /etc/sysconfig/network-scripts/route-ifname ,詳細方式請參考 – https://benjr.tw/102607 )
[root@localhost ~]# vi /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 設定檔需要修改.
- [Unit] 區塊新增
Requires=network-online.target
After=network-online.target - 新增 [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 Requires=network-online.target After=network-online.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
常見問題
- rc-local.service 設定檔需新增 [Install] 區塊,內容為 WantedBy=multi-user.target ,請參考前面的設定.
[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.
- 需注意 Systemd Unit Files 的 Requires 與 After 需設定為 network-online.target ,請參考前面的設定. .
[root@localhost ~]# systemctl status route ● route.service - Runs /usr/local/bin/route.sh Loaded: loaded (/usr/lib/systemd/system/route.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since Fri 2020-03-06 01:04:20 CST; 39s ago Process: 972 ExecStart=/usr/local/bin/route.sh (code=exited, status=7) Mar 06 01:04:20 localhost.localdomain systemd[1]: Starting Runs /usr/local/bin/route.sh... Mar 06 01:04:20 localhost.localdomain route.sh[972]: SIOCADDRT: Network is unreachable Mar 06 01:04:20 localhost.localdomain systemd[1]: route.service: Control process exited, code=exited status=7 Mar 06 01:04:20 localhost.localdomain systemd[1]: route.service: Failed with result 'exit-code'. Mar 06 01:04:20 localhost.localdomain systemd[1]: Failed to start Runs /usr/local/bin/route.sh.
沒有解決問題,試試搜尋本站其他內容