• [Ubuntu-20.04.x]让服务的启动命令放在/etc/rc.local文件中可以开机自启动


    1.1 系统版本

    root@vm7-101:~# head -5 /etc/os-release 
    NAME="Ubuntu"
    VERSION="20.04.4 LTS (Focal Fossa)"
    ID=ubuntu
    ID_LIKE=debian
    PRETTY_NAME="Ubuntu 20.04.4 LTS"
    root@vm7-101:~# 
    root@vm7-101:~# 
    root@vm7-101:~# uname -r
    5.4.0-100-generic
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.2 解决 /etc/rc.local 开机启动问题

    查看/lib/systemd/system/rc-local.service文件并进行修改

    #### 查看rc-local.service状态(当前是未启动的,你启动也会失败,因为缺少/etc/rc.local文件)
    root@vm7-101:~#
    root@vm7-101:~# systemctl status rc-local.service
    ● rc-local.service - /etc/rc.local Compatibility
         Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled)
        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@vm7-101:~#
    root@vm7-101:~# grep -v "#" /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
    TimeoutSec=0
    RemainAfterExit=yes
    GuessMainPID=no
    
    ### 修改 /lib/systemd/system/rc-local.service 文件中的内容
    sed 's#ExecStart=/etc/rc.loca#ExecStart=/bin/bash /etc/rc.loca#g' /lib/systemd/system/rc-local.service 
    sed -i 's#ExecStart=/etc/rc.loca#ExecStart=/bin/bash /etc/rc.loca#g' /lib/systemd/system/rc-local.service 
    
    ### 重新加载一下
    systemctl daemon-reload
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    创建/etc/rc.local文件

    #### 创建文件并加上权限
    touch /etc/rc.local
    ls -l /etc/rc.local
    chmod +x /etc/rc.local
    
    #### 往文件中添加内容
    [ $(wc -l /etc/rc.local  | cut -d " "  -f1) -eq 0 ] &&  echo -e '#!/bin/bash\n#\n' >>/etc/rc.local \
                                                   ||  sed  -i '1i #!/bin/bash\n#\n' /etc/rc.local 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    启动rc-local.service服务

    root@vm7-101:~# systemctl start rc-local.service
    root@vm7-101:~# systemctl status rc-local.service
    ● rc-local.service - /etc/rc.local Compatibility
         Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled)
        Drop-In: /usr/lib/systemd/system/rc-local.service.d
                 └─debian.conf
         Active: active (exited) since Thu 2022-11-17 15:32:20 CST; 4s ago
           Docs: man:systemd-rc-local-generator(8)
        Process: 1460 ExecStart=/bin/bash /etc/rc.local start (code=exited, status=0/SUCCESS)
    
    Nov 17 15:32:20 vm7-101.host.com systemd[1]: Starting /etc/rc.local Compatibility...
    Nov 17 15:32:20 vm7-101.host.com systemd[1]: Started /etc/rc.local Compatibility.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    1.3 测试一下

    #### 在iptables中添加规则
    iptables -t filter -I INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 -m tcp --dport 22 -j ACCEPT
    iptables -t filter -I OUTPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 -m tcp --sport 22 -j ACCEPT
    
    #### 将iptables中的规则进行保存到文件中
    iptables-save >/iptables
    
    #### 让开机启动时加载关机前保存的iptables数据
    cat >>/etc/rc.local<<'EOF'
    ## Boot restore iptables rules
    iptables-restore /iptables
    EOF
    
    #### 重启服务器(生产可不能随便重启哈)
    reboot
    
    #### 查看rc-local.service服务
    systemctl status rc-local.service   # 肯定是启动状态的
    
    #### 查看iptables中是否有规则
    ## 第一种方法:要用人眼看
    iptables -t filter -L INPUT -vn --line-numbers
    iptables -t filter -L OUTPUT -vn --line-numbers
    
    ## 第二种方法:不报错就Ok
    iptables -t filter -C INPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 -m tcp --dport 22 -j ACCEPT
    iptables -t filter -C OUTPUT -p tcp -s 0.0.0.0/0 -d 0.0.0.0/0 -m tcp --sport 22 -j ACCEPT
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    【LeetCode】1403.非递增顺序的最小子序列
    使用VSCode编辑与编译WSL2下源代码
    JavaScript面试必问 上
    【无标题】
    Expression 数学表达式求值
    Flink MySQL CDC
    休闲娱乐 - PS4游戏 Journey 风之旅人
    JMU第1届程序设计金秋赛题解
    从C语言到C++_40(多线程相关)C++线程接口+线程安全问题加锁(shared_ptr+STL+单例)
    【每日一题】—— 最大素因子
  • 原文地址:https://blog.csdn.net/weixin_43733154/article/details/127904058