Hướng dẫn kích hoạt rc.local cho systemd trên Linux

Giới thiệu về rc.local #

/etc/rc.local là file shell script được thực thi bởi systemd khi khởi động bằng cách dùng một dịch vụ có sẵn là rc-local.service. Nhờ đó, có thể thể sử dụng rc.local để thực thi câu lệnh hoặc bật dịch vụ cùng hệ thống.

Tạo file rc.local #

Đầu tiên, bạn hãy tạo file /etc/rc.local như bên dưới:

$ sudo vim /etc/rc.local

## RHEL/CentOS/Fedora Linux edit the /etc/rc.d/rc.local ##

$ sudo vim /etc/rc.d/rc.local

Điền đoạn mã shell của bạn vào file để thực thi, trong bài viết này mình sẽ sử dụng đoạn script bên dưới:

#!/bin/sh
# add your commands 
# call your scripts here
 
# let us set stuff for my wifi
/sbin/iw phy0 wowlan enable magic-packet disconnect
 
# last line must be exit 0 
exit 0

Lưu lại thay đổi của file và gán quyền thực thi cho file:

$ sudo chmod -v +x /etc/rc.local

Kích hoạt rc-local.service #

Để kích hoạt, sử dụng systemctl như bên dưới:

$ sudo systemctl enable rc-local.service

Sau đó, hãy khởi động lại hệ thống Linux của bạn và dùng systemctl để kiểm tra trạng thái:

$ sudo systemctl status rc-local.service

Nếu rc-local.service được chạy thì trạng thái sẽ báo như bên dưới:

● rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/etc/systemd/system/rc-local.service; enabled-runtime; ven>
    Drop-In: /usr/lib/systemd/system/rc-local.service.d
             └─debian.conf
     Active: active (exited) since Wed 2020-11-04 13:29:54 IST; 1h 59min ago
       Docs: man:systemd-rc-local-generator(8)
      Tasks: 0 (limit: 37939)
     Memory: 0B
     CGroup: /system.slice/rc-local.service

Nov 04 13:29:54 nixcraft-wks01 systemd[1]: Starting /etc/rc.local Compatibility
Nov 04 13:29:54 nixcraft-wks01 systemd[1]: Started /etc/rc.local Compatibility.

Hiển thị các cài đặt của dịch vụ #

Để xem các cài đặt của dịch vụ, bạn có thể sử dụng systemctl như sau:

$ sudo systemctl cat rc-local.service

Lúc đó, các cài đặt của dịch vụ sẽ hiển thị:

# /etc/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.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
 
# /usr/lib/systemd/system/rc-local.service.d/debian.conf
[Unit]
# not specified by LSB, but has been behaving that way in Debian under SysV
# init and upstart
After=network-online.target
 
# Often contains status messages which users expect to see on the console
# during boot
[Service]
StandardOutput=journal+console
StandardError=journal+console

Tạo một dịch vụ với systemd #

Ngoài việc dùng rc.local để chạy script khi khởi động, bạn cũng có thể tạo một dịch vụ  riêng để chạy với systemd. Cấu trúc của file .service sẽ như bên dưới:

# /etc/systemd/system/my-service-name-goes-here.service
#
# Sample template to call your script or command when systemd boots into multi user mode
#
[Unit]
Before=network.target
[Service]
Type=oneshot
ExecStart=/path/to/command
ExecStart=/path/to/script arg1 arg2
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

Trong phần này, mình sẽ tạo một service để thực thi các rule hỗ trợ cho wireguard khi khởi động:

# /etc/systemd/system/wireguard-iptables.service
[Unit]
Before=network.target
[Service]
Type=oneshot
ExecStart=/usr/sbin/iptables -t nat -A POSTROUTING -s 10.8.1.0/24 ! -d 10.8.1.0/24 -j SNAT --to 123.x.x.x
ExecStart=/usr/sbin/iptables -I INPUT -p udp --dport 1194 -j ACCEPT
ExecStart=/usr/sbin/iptables -I FORWARD -s 10.8.1.0/24 -j ACCEPT
ExecStart=/usr/sbin/iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
ExecStop=/usr/sbin/iptables -t nat -D POSTROUTING -s 10.8.1.0/24 ! -d 10.8.1.0/24 -j SNAT --to 123.x.x.x
ExecStop=/usr/sbin/iptables -D INPUT -p udp --dport 1194 -j ACCEPT
ExecStop=/usr/sbin/iptables -D FORWARD -s 10.8.1.0/24 -j ACCEPT
ExecStop=/usr/sbin/iptables -D FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

Lưu lại file với tên wireguard-iptables.service trong thư mục /etc/systemd/system/ rồi kích hoạt cho dịch vụ khởi động cùng hệ thống:

$ sudo systemctl enable wireguard-iptables.service

$ sudo systemctl start wireguard-iptables.service

$ sudo systemctl stop wireguard-iptables.service

Kết luận #

Mình đã hướng dẫn bạn kích hoạt rc.local và tạo một dịch vụ để khởi động cùng hệ thống. Nếu bất kì thắc mắc nào, bạn hãy bình luận bên dưới để mình hỗ trợ nhé.

Ngoài ra, cũng với chủ đề về  Ubuntu, bạn có thể tham khảo bài viết khác ở bên dưới:

Nguồn bài viết: https://www.cyberciti.biz

 

Powered by BetterDocs

Để lại bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *