Linux – Systemd Unit Files

以前會把需要在開機後執行的指令寫在 /etc/rc.local ,但在 CentOS7 / CentOS 8 會發現系統不會去執行該檔案的內容.需要設定後才會去執行,詳細方式請參考 http://benjr.tw/102545

另外一個方式就是自己寫 Systemd Unit Files. 參考文章 – https://www.redhat.com/sysadmin/replacing-rclocal-systemd

測試環境 CentOS 8 x86_64 (虛擬機)

先編輯要執行的程式,以下範例使用 .sh (Bash) 檔案,裡面指令為新增一筆 Routing Table.

[root@localhost ~]# vi /usr/local/bin/route.sh
#!/bin/bash
route add -net 172.16.0.0 netmask 255.255.255.0 gw 192.168.111.161

需修改其執行權限 chmod a+x (Execute)

[root@localhost ~]# chmod a+x /usr/local/bin/route.sh
[root@localhost ~]# ll /usr/local/bin/route.sh
-rwxr-xr-x 1 root root 76 Feb  7 19:24 /usr/local/bin/route.sh

可以先確定一下執行沒有問題.

[root@localhost ~]# /usr/local/bin/route.sh
[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

編輯 Systemd Unit Files.

[root@localhost ~]# vi /usr/lib/systemd/system/route.service
[Unit]
Description=Runs /usr/local/bin/route.sh

[Service]
ExecStart=/usr/local/bin/route.sh

[Install]
WantedBy=multi-user.target

只簡單設定3個區塊
[unit] Description 敘述該 Systemd Unit Files 目的.
[Service] ExecStart 指定執行程式
[Install] WantedBy 指定哪一個 runlevel 執行.

其他更多設定請參考 – https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_basic_system_settings/managing-services-with-systemd_configuring-basic-system-settings#working-with-systemd-unit-files_managing-services-with-systemd

編輯設定檔後就可以直接執行 #systemctl enable (開機時啟動), start (立即啟動) route ,更多關於 systemctl 請參考 – http://benjr.tw/94315 .

[root@localhost ~]# systemctl enable route
Created symlink /etc/systemd/system/multi-user.target.wants/route.service → /usr/lib/systemd/system/route.service.
[root@localhost ~]# systemctl start route
[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: inactive (dead) since Fri 2020-02-07 19:30:11 CST; 2s ago
  Process: 2560 ExecStart=/usr/local/bin/route.sh (code=exited, status=0/SUCCESS)
 Main PID: 2560 (code=exited, status=0/SUCCESS)

Feb 07 19:30:11 localhost.localdomain systemd[1]: Started Runs /usr/local/bin/route.sh.

剛剛已經執行過 /usr/local/bin/route.sh 所以 Routing Table 一樣,下次重新開機注意一下,可以看到的確有多一筆 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
沒有解決問題,試試搜尋本站其他內容

發佈留言

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

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