Ubuntu 22.04配置/etc/rc.local开机自启文件

1.查看系统版本
root@bogon-virtual-machine:~# lsb_release -a
No LSB modules are available.
Distributor ID:    Ubuntu
Description:    Ubuntu 22.04 LTS
Release:    22.04
Codename:    jammy
root@bogon-virtual-machine:~

2. 解决 /etc/rc.local 开机启动问题
看rc-local.service状态(当前是未启动的,启动也会失败,因为缺少/etc/rc.local文件)

root@bogon-virtual-machine:~# systemctl status rc-local.service
○ rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/lib/systemd/system/rc-local.service; static)
    Drop-In: /usr/lib/systemd/system/rc-local.service.d
             └─debian.conf
     Active: inactive (dead)
       Docs: man:systemd-rc-local-generator(8)


修改/lib/systemd/system/rc-local.service 文件

root@bogon-virtual-machine:~# vim /lib/systemd/system/rc-local.service

[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                #将此行注释
ExecStart=/bin/bash /etc/rc.local start        #添加此行配置
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

重新加载一下
root@bogon-virtual-machine:~# systemctl daemon-reload

创建/etc/rc.local文件并加上权限
root@bogon-virtual-machine:~# touch /etc/rc.local
root@bogon-virtual-machine:~# ls -l /etc/rc.local
root@bogon-virtual-machine:~# chmod +x /etc/rc.local


往文件中添加内容

root@bogon-virtual-machine:~# vim /etc/rc.local 
#!/bin/bash

启动rc-local.service服务

root@bogon-virtual-machine:~# systemctl start rc-local.service
root@bogon-virtual-machine:~# systemctl status rc-local.service
● rc-local.service - /etc/rc.local Compatibility
     Loaded: loaded (/lib/systemd/system/rc-local.service; static)
    Drop-In: /usr/lib/systemd/system/rc-local.service.d
             └─debian.conf
     Active: active (exited) since Wed 2023-10-25 10:31:29 CST; 7s ago
       Docs: man:systemd-rc-local-generator(8)
    Process: 1776 ExecStart=/bin/bash /etc/rc.local start (code=exited, status=0/SUCCESS)
        CPU: 1ms

10月 25 10:31:29 bogon-virtual-machine systemd[1]: Starting /etc/rc.local Compatibility...
10月 25 10:31:29 bogon-virtual-machine systemd[1]: Started /etc/rc.local Compatibility.
 


3. 测试一下
在开机文件中添加创建文件命令进行测试

root@bogon-virtual-machine:~#vim /etc/rc.local
#!/bin/bash
#
touch /usr/local/test.txt

重启服务器(生产可不能随便重启哈)

root@bogon-virtual-machine:~# reboot

查看rc-local.service服务
root@bogon-virtual-machine:~# systemctl status rc-local.service     #肯定是启动状态的

查看文件已创成功

root@bogon-virtual-machine:~# ll /usr/local/test.txt

你可能感兴趣的:(ubuntu,linux,运维)