🐧如何在Linux启动时在systemd中启用rc.local shell脚本
当Linux系统启动时,我们可以轻松地在systemd中启用rc.local shell脚本支持。 传统上,Linux开发人员和sysadmin使用/etc/rc.local shell脚本在加载所有服务后调用其他脚本或命令。 通常,当Linux初始化切换到多用户运行级别时,在最后调用/etc/rc.local。 但是,默认情况下,/ etc / rc.local支持在systemd中被禁用。 在本文中,我们将向您展示如何在Linux上使用systemd在启动时启用和执行rc.local shell脚本。
在Linux系统启动时在systemd中启用rc.local shell脚本
与/etc/rc.local的兼容性是通过systemd使用称为rc-local.service的特殊服务来实现的。
如果/etc/rc.local是可执行的,则在systemd-rc-local-generator中在multi-user.target中自动调用此模块。
在启动时使用systemd执行rc.local shell脚本
自然地,使用您喜欢的文本编辑器创建或更新/编辑一个名为/etc/rc.local的文件。
我将使用vim命令:
$ sudo vim /etc/rc.local## RHEL/CentOS/Fedora Linux отредактируйте файл /etc/rc.d/rc.local ##
$ sudo vim /etc/rc.d/rc.local
添加所需的命令或调用脚本。 这是我的文件:
#!/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
使用vim时保存并关闭文件。
确保使用chmod命令设置文件的可执行权限:
$ sudo chmod -v +x /etc/rc.local
在systemd启动时在Linux上设置rc-local.service
我们需要做的就是输入以下systemctl命令:
$ sudo systemctl enable rc-local.service
重新启动Linux:
$ sudo reboot
重新启动后检查状态:
$ sudo systemctl status rc-local.service
这是我们在屏幕上看到的:
● 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 itsecforu systemd[1]: Starting /etc/rc.local Compatibility Nov 04 13:29:54 itsecforu systemd[1]: Started /etc/rc.local Compatibility.
如何查看服务配置
打开终端应用程序,然后键入:
$ sudo systemctl cat rc-local.service
我们将看到systemd配置如下:
# /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
注意。 当脚本加载失败时,运行sudo SYSTEMD_LOG_LEVEL = debug / usr / lib / systemd / system-generators / systemd-rc-local-generator调试/etc/rc.local的问题。
Linux运行级别信息
默认情况下使用init时,我们具有以下运行级别:
- S-启动Linux系统
- 0-关闭Linux
- 6-重新启动Linux
- 1-用于Linux系统紧急恢复的Linux单用户模式
- 2-5-支持CLI和GUI以及完全网络连接的通用多用户系统
通常,Linux发行版和Unix系统更改了这些运行级别值以适应其需求。
但是,当系统通过运行级别2到5进入多用户模式时,将调用/etc/rc.local。
但是,当大多数Linux发行版切换到systemd时,将删除此默认设置。
因此,我为Linux开发人员和系统管理员编写了此快速指南。
在Linux上通过systemd轻松创建自己的服务
代替/etc/rc.d/中的Shell脚本或对/etc/rc.local的调用,我们将以不同的方式进行。
它仅适用于Linux,不适用于其他Unix版本。 然后做:
# /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
例如,以下是我们如何设置Wireguard或openvpn iptables规则的方法:
# /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
然后:
$ sudo systemctl enable wireguard-iptables.service
$ sudo systemctl start wireguard-iptables.service
$ sudo systemctl stop wireguard-iptables.service
结论
我希望您能找到启用/etc/rc.local支持的快速指南,该指南有助于向后兼容并易于与systemd一起使用。