分类 Linux 下的文章

在较新的 Ubuntu 版本(如 16.04 及以后)中,rc.local 文件默认不存在,因为这些系统使用 systemd 作为初始化系统,取代了传统的 SysVinit 机制。如果需要使用 rc.local 方法,可以按照以下步骤操作:

手动创建并配置 rc.local
创建 rc.local 文件

sudo nano /etc/rc.local

添加以下内容

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/path/to/your/script.sh &  # 添加你的脚本路径

exit 0

保存并退出编辑器,然后设置文件权限:

sudo chmod +x /etc/rc.local

创建并配置 systemd 服务

sudo nano /etc/systemd/system/rc-local.service

添加以下内容到服务文件

[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target

启用并启动服务

sudo systemctl enable rc-local
sudo systemctl start rc-local.service

验证服务状态

sudo systemctl status rc-local.service

编辑/etc/sysctl.conf,添加或者编辑以下变量:

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
net.ipv6.conf.eth0.disable_ipv6 = 1

最后sysctl -p即可。

方法1:
rc.local文件中添加自启动命令
执行命令: 编辑"/etc/rc.local",添加你想开机运行的命令
运行程序脚本:然后在文件最后一行添加要执行程序的全路径。
例如,每次开机时要执行一个hello.sh,这个脚本放在/usr下面,那就可以在"/etc/rc.local"中加一行"/usr/./hello.sh",或者" cd /opt && ./hello.sh "
注意,你的命令应该添加在:exit 0 之前

方法2:
在/etc/init.d目录下添加自启动脚本
linux在"/etc/rc.d/init.d"下有很多的文件,每个文件都是可以看到内容的,其实都是一些shell脚本或者可执行二进制文件
Linux开机的时候,会加载运行/etc/init.d目录下的程序,因此我们可以把想要自动运行的脚本放到这个目录下即可。
系统服务的启动就是通过这种方式实现的。

方法3:
systemctl命令
使某服务自动启动 systemctl enable httpd.service
使某服务不自动启动 systemctl disable httpd.service
检查服务状态 systemctl status httpd.service (服务详细信息) systemctl is-active httpd.service (仅显示是否 Active)
显示所有已启动的服务 systemctl list-units --type=service
启动某服务 systemctl start httpd.service
停止某服务 systemctl stop httpd.service
重启某服务 systemctl restart httpd.service
例子:
启动nfs服务 systemctl start nfs-server.service
设置开机自启动 systemctl enable nfs-server.service
停止开机自启动 systemctl disable nfs-server.service
查看服务当前状态 systemctl status nfs-server.service
重新启动某服务 systemctl restart nfs-server.service
查看所有已启动的服务 systemctl list -units --type=service
开启防火墙22端口 iptables -I INPUT -p tcp --dport 22 -j accept
彻底关闭防火墙:
sudo systemctl status firewalld.service
sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.service

方法4:(接方法2)
安装sysv-rc-conf
apt-get update
apt-get install sysv-rc-conf
运行sysv-rc-conf可视化启动init.d目录下的脚本

参考链接:
https://m.php.cn/article/480523.html
https://blog.csdn.net/ieeso/article/details/110920105
https://blog.csdn.net/love_521_/article/details/123484257