linux怎么让命令只在开机的时候加载

179 阅读3分钟

添加两个 table

image.png

怎么能让以下命令只在开机的时候加载?

dhclient

ip route add default via 10.67.40.254 dev enp0s20f0u6u1 table 100
ip route add default via 172.27.220.254 dev enp0s20f0u9 table 200
ip rule add from 10.67.40.90 table 100
ip rule add from 172.27.220.64 table 200

仅在开机时执行一次,而不是每次打开终端时都执行,你应该将这些命令从.bashrc文件中移除,并将它们添加到系统的启动脚本中。这样做的方法取决于你使用的是哪个Linux发行版和使用的初始化系统(如Systemd或init.d)。

Centos7 使用Systemd的系统

  1. 创建一个自定义的systemd服务。

首先,创建一个新的systemd服务文件,例如/etc/systemd/system/custom-network.service

vim /etc/systemd/system/custom-network.service

然后,在这个文件中添加如下内容:

[Unit]
Description=Custom Network Configuration
After=network.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/custom-network.sh
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
  1. 创建脚本执行网络命令。

接着,创建脚本/usr/local/bin/custom-network.sh,在脚本中放入你想在启动时执行的命令。

vim /usr/local/bin/custom-network.sh

添加以下内容:

#!/bin/bash

dhclient

ip route add default via 10.67.40.254 dev enp0s20f0u6u1 table 100
ip route add default via 172.27.220.254 dev enp0s20f0u9 table 200
ip rule add from 10.67.40.90 table 100
ip rule add from 172.27.220.64 table 200

然后,使脚本可执行:

chmod +x /usr/local/bin/custom-network.sh
  1. 启用并启动服务。
systemctl enable custom-network.service
systemctl start custom-network.service
systemctl status custom-network.service

这将使得你的网络配置命令在系统启动时执行一次,并且在之后打开终端时不会再执行。

完美解决!下面的就不用看了~


对于使用init.d的系统

如果你的系统使用的是较旧的init.d系统,你需要创建一个脚本在/etc/init.d目录下,并将其添加到合适的运行级别中。

  1. 创建脚本,例如/etc/init.d/custom-network,并在其中添加你的命令。
  2. 使脚本可执行:sudo chmod +x /etc/init.d/custom-network
  3. 使用update-rc.d custom-network defaults命令将其添加到启动脚本中。

请注意,根据你的具体Linux发行版,步骤可能会有所不同。上述步骤提供了一个通用的方法,但你可能需要根据你的系统调整路径或命令。

[root@localhost ~]# vim /etc/systemd/system/custom-network.service
[root@localhost ~]# vim  /usr/local/bin/custom-network.sh
[root@localhost ~]# chmod +x /usr/local/bin/custom-network.sh
[root@localhost ~]# systemctl enable custom-network.service
Created symlink from /etc/systemd/system/multi-user.target.wants/custom-network.service to /etc/systemd/system/custom-network.service.
[root@localhost ~]# systemctl start custom-network.service
[root@localhost ~]# systemctl status custom-network.service
● custom-network.service - Custom Network Configuration
   Loaded: loaded (/etc/systemd/system/custom-network.service; enabled; vendor preset: disabled)
   Active: active (exited) since Thu 2024-02-22 14:48:20 CST; 4s ago
  Process: 110591 ExecStart=/usr/local/bin/custom-network.sh (code=exited, status=0/SUCCESS)
 Main PID: 110591 (code=exited, status=0/SUCCESS)

Feb 22 14:48:20 localhost.localdomain custom-network.sh[110591]: This version of ISC DHCP is based o...e
Feb 22 14:48:20 localhost.localdomain custom-network.sh[110591]: on ftp.isc.org.  Features have been...s
Feb 22 14:48:20 localhost.localdomain custom-network.sh[110591]: have been made to the base software...e
Feb 22 14:48:20 localhost.localdomain custom-network.sh[110591]: it work better with this distribution.
Feb 22 14:48:20 localhost.localdomain custom-network.sh[110591]: Please report for this software via...:
Feb 22 14:48:20 localhost.localdomain custom-network.sh[110591]: http://bugs.centos.org/
Feb 22 14:48:20 localhost.localdomain custom-network.sh[110591]: exiting.
Feb 22 14:48:20 localhost.localdomain custom-network.sh[110591]: RTNETLINK answers: File exists
Feb 22 14:48:20 localhost.localdomain custom-network.sh[110591]: RTNETLINK answers: File exists
Feb 22 14:48:20 localhost.localdomain systemd[1]: Started Custom Network Configuration.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# 

可能会用到这个命令:

iptables -t nat -A POSTROUTING -o enp0s20f0u10 -j MASQUERADE

image.png