Dịch vụ rc.local của systemd trên Linux VPS có gì vui?

rc.local là dịch vụ của systemd để tự động chạy các lệnh hoặc các shell script khi hệ điều hành Linux khởi động.

Nói cách khác khi hệ điều hành Linux khởi động lên nó sẽ tự động chạy các lệnh hoặc các shell script khai báo trong dịch vụ rc.local của systemd.

Khi mua hoặc xài thử Linux VPS bạn hãy kiểm tra các dịch vụ của systemd trên hệ điều hành Linux vừa khởi tạo coi có gì đặc biệt hay lỗi gì không?

Thật bất ngờ là tự nhiên thấy dịch vụ rc.local của systemd báo lỗi.

systemctl -t service --no-pager
  UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
  auditd.service                     loaded active running Security Auditing Service
  chronyd.service                    loaded active running NTP client/server
  crond.service                      loaded active running Command Scheduler
  dbus-broker.service                loaded active running D-Bus System Message Bus
  dracut-shutdown.service            loaded active exited  Restore /run/initramfs on shutdown
  firewalld.service                  loaded active running firewalld - dynamic firewall daemon
  getty@tty1.service                 loaded active running Getty on tty1
  irqbalance.service                 loaded active running irqbalance daemon
  kdump.service                      loaded active exited  Crash recovery kernel arming
  kmod-static-nodes.service          loaded active exited  Create List of Static Device Nodes
  NetworkManager-wait-online.service loaded active exited  Network Manager Wait Online
  NetworkManager.service             loaded active running Network Manager
  nis-domainname.service             loaded active exited  Read and set NIS domainname from /etc/sysconfig/network
● rc-local.service                   loaded failed failed  /etc/rc.local Compatibility
  rsyslog.service                    loaded active running System Logging Service
  sshd.service                       loaded active running OpenSSH server daemon
  systemd-fsck-root.service          loaded active exited  File System Check on Root Device
  systemd-journal-flush.service      loaded active exited  Flush Journal to Persistent Storage
  systemd-journald.service           loaded active running Journal Service
  systemd-logind.service             loaded active running User Login Management
  systemd-network-generator.service  loaded active exited  Generate network units from Kernel command line
  systemd-random-seed.service        loaded active exited  Load/Save Random Seed
  systemd-remount-fs.service         loaded active exited  Remount Root and Kernel File Systems
  systemd-sysctl.service             loaded active exited  Apply Kernel Variables
  systemd-tmpfiles-setup-dev.service loaded active exited  Create Static Device Nodes in /dev
  systemd-tmpfiles-setup.service     loaded active exited  Create Volatile Files and Directories
  systemd-udev-trigger.service       loaded active exited  Coldplug All udev Devices
  systemd-udevd.service              loaded active running Rule-based Manager for Device Events and Files
  systemd-update-utmp.service        loaded active exited  Record System Boot/Shutdown in UTMP
  systemd-user-sessions.service      loaded active exited  Permit User Sessions
  user-runtime-dir@1000.service      loaded active exited  User Runtime Directory /run/user/1000
  user@1000.service                  loaded active running User Manager for UID 1000

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
36 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

Coi kĩ tình trạng dịch vụ rc-local.service để biết thêm

systemctl status rc-local
× rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; preset: disabled)
     Active: failed (Result: exit-code) since Sun 2023-12-31 12:48:14 CET; 4h 23min ago
        CPU: 10ms

systemd[1]: rc-local.service: Failed with result 'exit-code'.
systemd[1]: Failed to start /etc/rc.local Compatibility.
systemd[1]: /etc/systemd/system/rc-local.service:11: Support for option SysVStartPriority= has been removed and it is ignored
systemd[1]: /etc/systemd/system/rc-local.service:11: Support for option SysVStartPriority= has been removed and it is ignored
systemd[1]: /etc/systemd/system/rc-local.service:11: Support for option SysVStartPriority= has been removed and it is ignored
systemd[1]: /etc/systemd/system/rc-local.service:11: Support for option SysVStartPriority= has been removed and it is ignored
systemd[1]: /etc/systemd/system/rc-local.service:11: Support for option SysVStartPriority= has been removed and it is ignored
systemd[1]: /etc/systemd/system/rc-local.service:11: Support for option SysVStartPriority= has been removed and it is ignored
systemd[1]: /etc/systemd/system/rc-local.service:11: Support for option SysVStartPriority= has been removed and it is ignored
systemd[1]: /etc/systemd/system/rc-local.service:11: Support for option SysVStartPriority= has been removed and it is ignored

Coi trong /etc/rc.local để biết dịch vụ rc.local chạy những bash script gì mà bị lỗi

Không có /virt/swfix.sh nên lỗi.

swfix.sh là để làm gì cũng chẳng biết vì không có file này.

cat /etc/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
sh /virt/swfix.sh

ll /virt/swfix.sh
ls: cannot access '/virt/swfix.sh': No such file or directory

Cách sửa lỗi rc.local trong trường hợp lỗi này là bỏ dòng sh /virt/swfix.sh bằng cách thêm ghi chú (comment) là dấu # ở đầu dòng.

tail -2 /etc/rc.local
touch /var/lock/subsys/local
# sh /virt/swfix.sh

systemctl restart rc-local

systemctl status rc-local
● rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; preset: disabled)
     Active: active (exited) since Sun 2023-12-31 17:18:37 CET; 2s ago
    Process: 8777 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
        CPU: 7ms

Dec 31 17:18:37 .1704019704 systemd[1]: Starting /etc/rc.local Compatibility...
Dec 31 17:18:37 .1704019704 systemd[1]: Started /etc/rc.local Compatibility.

systemctl -t service
  UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
  auditd.service                     loaded active running Security Auditing Service
  chronyd.service                    loaded active running NTP client/server
  crond.service                      loaded active running Command Scheduler
  dbus-broker.service                loaded active running D-Bus System Message Bus
  dracut-shutdown.service            loaded active exited  Restore /run/initramfs on shutdown
  firewalld.service                  loaded active running firewalld - dynamic firewall daemon
  getty@tty1.service                 loaded active running Getty on tty1
  irqbalance.service                 loaded active running irqbalance daemon
  kdump.service                      loaded active exited  Crash recovery kernel arming
  kmod-static-nodes.service          loaded active exited  Create List of Static Device Nodes
  mariadb.service                    loaded active running MariaDB 10.5 database server
  NetworkManager-wait-online.service loaded active exited  Network Manager Wait Online
  NetworkManager.service             loaded active running Network Manager
  nginx.service                      loaded active running nginx - high performance web server
  nis-domainname.service             loaded active exited  Read and set NIS domainname from /etc/sysconfig/network
  php-fpm.service                    loaded active running The PHP FastCGI Process Manager
  rc-local.service                   loaded active exited  /etc/rc.local Compatibility
  redis.service                      loaded active running Redis persistent key-value database
  rsyslog.service                    loaded active running System Logging Service
  sshd.service                       loaded active running OpenSSH server daemon
  systemd-fsck-root.service          loaded active exited  File System Check on Root Device
  systemd-journal-flush.service      loaded active exited  Flush Journal to Persistent Storage
  systemd-journald.service           loaded active running Journal Service
  systemd-logind.service             loaded active running User Login Management
  systemd-network-generator.service  loaded active exited  Generate network units from Kernel command line
  systemd-random-seed.service        loaded active exited  Load/Save Random Seed
  systemd-remount-fs.service         loaded active exited  Remount Root and Kernel File Systems
  systemd-sysctl.service             loaded active exited  Apply Kernel Variables
  systemd-tmpfiles-setup-dev.service loaded active exited  Create Static Device Nodes in /dev
  systemd-tmpfiles-setup.service     loaded active exited  Create Volatile Files and Directories
  systemd-udev-trigger.service       loaded active exited  Coldplug All udev Devices
  systemd-udevd.service              loaded active running Rule-based Manager for Device Events and Files
  systemd-update-utmp.service        loaded active exited  Record System Boot/Shutdown in UTMP
  systemd-user-sessions.service      loaded active exited  Permit User Sessions
  user-runtime-dir@1000.service      loaded active exited  User Runtime Directory /run/user/1000
  user@1000.service                  loaded active running User Manager for UID 1000

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
36 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.