ubuntu服务器有多个网卡时设置网卡的优先级

有时候服务器会有多个网卡,分别连接局域网和外网,当局域网的优先级更高时会出现无法访问外网的情况

只需要设置默认网关就行了

首先查看默认网关

ip route show

然后通过

sudo route del default gw 192.168.1.1

这里是不需要的局域网网关

但是这样做是不够的,重启之后会自动重制,这里我们需要设置开机自启项目。

编写开机自启脚本

sudo 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
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target
Alias=rc-local.service
sudo vim /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.
sudo route del default gw 192.168.1.1
exit 0

最后加入服务

sudo ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值