ubuntu22上实现开机添加IP路由(华为云服务器添加新的网卡和绑定弹性IP后,需要配置策略路由)

前提:我测试的服务器状态如下(启动eth1为新添加网卡):

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.183  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::f816:3eff:fe59:698  prefixlen 64  scopeid 0x20<link>
        ether fa:16:3e:59:06:98  txqueuelen 1000  (Ethernet)
        RX packets 576  bytes 121732 (121.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 663  bytes 100971 (100.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.139  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::f816:3eff:fecc:24af  prefixlen 64  scopeid 0x20<link>
        ether fa:16:3e:cc:24:af  txqueuelen 1000  (Ethernet)
        RX packets 381  bytes 29751 (29.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 253  bytes 22258 (22.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

第一步:创建一个在 Ubuntu系统启动时执行的脚本。
/root/.ssh/addroute.sh

ip route add default via 192.168.0.1 dev eth0 table 10
ip route add 192.168.0.0/24 dev eth0 table 10
ip rule add from 192.168.0.183 table 10
ip route add default via 192.168.0.1 dev eth1 table 30
ip route add 192.168.0.0/24 dev eth1 table 30
ip rule add from 192.168.0.139 table 30

第二步:新建
/lib/systemd/system/myiproute.service

[Unit]
Description=iproute
After=syslog.target network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
ExecStart=/bin/bash /root/.ssh/addroute.sh
PrivateTmp=true

[Install]
WantedBy=multi-user.target


第三步:设置开机启动

systemctl enable  myiproute.service


第四步:手动启动

service  myiproute start


第五步:查看状态

systemctl status myiproute

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. 在华为防火墙上配置IPSec VPN 首先,在华为防火墙上配置IPSec VPN,以便与Ubuntu 20.04公网服务器建立安全连接。 1.1 创建IKE策略 IKE是IPSec的关键协议之一,用于建立安全的VPN隧道。在华为防火墙上,我们需要创建IKE策略来定义IKE协议的参数。 在命令行界面下,输入以下命令: [Huawei] ipsec ike proposal ike-proposal1 [Huawei-ike-proposal-ike-proposal1] encryption-algorithm aes-cbc-256 [Huawei-ike-proposal-ike-proposal1] integrity-algorithm sha1 [Huawei-ike-proposal-ike-proposal1] dh group14 该命令创建了一个IKE策略,指定了加密算法、完整性算法和Diffie-Hellman密钥交换组。这些参数应该与Ubuntu 20.04公网服务器上的设置相匹配。 1.2 创建IPSec策略 IPSec是用于加密数据的另一个重要协议。我们需要在华为防火墙上创建IPSec策略,以定义加密参数。 在命令行界面下,输入以下命令: [Huawei] ipsec proposal ipsec-proposal1 [Huawei-ipsec-proposal-ipsec-proposal1] esp encryption-algorithm aes-cbc-256 [Huawei-ipsec-proposal-ipsec-proposal1] esp integrity-algorithm sha1 该命令创建了一个IPSec策略,指定了加密和完整性算法。这些参数应该与Ubuntu 20.04公网服务器上的设置相匹配。 1.3 创建IPSec VPN 现在,我们可以使用上述IKE和IPSec策略创建IPSec VPN。 在命令行界面下,输入以下命令: [Huawei] ipsec vpn vpn1 [Huawei-ipsec-vpn-vpn1] ike proposal ike-proposal1 [Huawei-ipsec-vpn-vpn1] ipsec proposal ipsec-proposal1 [Huawei-ipsec-vpn-vpn1] remote-address 1.2.3.4 [Huawei-ipsec-vpn-vpn1] quit 该命令创建了一个名为vpn1的IPSec VPN,指定了IKE和IPSec策略,并将其绑定Ubuntu 20.04公网服务器的公共IP地址。 2. 在Ubuntu 20.04公网服务器配置IPSec VPN 接下来,在Ubuntu 20.04公网服务器配置IPSec VPN,以便与华为防火墙建立安全连接。 2.1 安装StrongSwan StrongSwan是一个流行的开源IPSec VPN实现,我们将使用它来配置Ubuntu 20.04公网服务器。 在终端中,输入以下命令: $ sudo apt-get update $ sudo apt-get install strongswan 这将安装StrongSwan。 2.2 配置IPSec VPN 现在,我们需要配置StrongSwan以与华为防火墙建立IPSec VPN。 在终端中,打开/etc/ipsec.conf文件: $ sudo nano /etc/ipsec.conf 将以下内容添加到文件末尾: conn vpn1 keyexchange=ikev1 authby=secret ike=3des-sha1-modp1024 esp=aes256-sha1 left=2.3.4.5 # Ubuntu 20.04公网服务器的公共IP地址 leftsubnet=192.168.1.0/24 # Ubuntu 20.04公网服务器的本地子网 right=1.2.3.4 # 华为防火墙的公共IP地址 rightsubnet=192.168.2.0/24 # 华为防火墙的本地子网 auto=start 这将创建一个名为vpn1的IPSec连接,指定了IKE和IPSec参数,并将其绑定Ubuntu 20.04公网服务器和华为防火墙的本地子网。 2.3 配置PSK 我们还需要IPSec VPN配置预共享密钥(PSK)。 在终端中,打开/etc/ipsec.secrets文件: $ sudo nano /etc/ipsec.secrets 将以下内容添加到文件末尾: 2.3.4.5 1.2.3.4 : PSK "mysecretpassword" 这将为Ubuntu 20.04公网服务器和华为防火墙之间的IPSec连接配置PSK。 3. 测试IPSec VPN 现在,我们可以测试IPSec VPN是否正常工作。 在Ubuntu 20.04公网服务器上,输入以下命令以启动IPSec连接: $ sudo ipsec up vpn1 如果一切正常,您将看到以下输出: initiating Main Mode IKE_SA vpn1[1] to 1.2.3.4 generating ID_PROT request 0 [ SA V V V V V ] sending packet: from 2.3.4.5[500] to 1.2.3.4[500] (184 bytes) received packet: from 1.2.3.4[500] to 2.3.4.5[500] (184 bytes) parsed ID_PROT response 0 [ SA V V V V V ] received NAT-T (RFC 3947) vendor ID received XAuth vendor ID received DPD vendor ID received FRAGMENTATION vendor ID received unknown vendor ID: 1f:07:17:00:00:00:00:00:00:00:00:00:00:00:00:00 generating ID_PROT request 0 [ KE No NAT-D NAT-D ] sending packet: from 2.3.4.5[500] to 1.2.3.4[500] (244 bytes) received packet: from 1.2.3.4[500] to 2.3.4.5[500] (244 bytes) parsed ID_PROT response 0 [ KE No NAT-D NAT-D ] local host is behind NAT, sending keep alives generating ID_PROT request 0 [ ID HASH ] sending packet: from 2.3.4.5[500] to 1.2.3.4[500] (100 bytes) received packet: from 1.2.3.4[500] to 2.3.4.5[500] (68 bytes) parsed ID_PROT response 0 [ ID HASH ] IKE_SA vpn1[1] established between 2.3.4.5[2.3.4.5]...1.2.3.4[1.2.3.4] scheduling reauthentication in 10046s maximum IKE_SA lifetime 10586s generating QUICK_MODE request 2499452192 [ HASH SA No ID ID ] sending packet: from 2.3.4.5[500] to 1.2.3.4[500] (188 bytes) received packet: from 1.2.3.4[500] to 2.3.4.5[500] (188 bytes) parsed INFORMATIONAL_V1 request 4026938035 [ HASH D ] received packet: from 1.2.3.4[500] to 2.3.4.5[500] (76 bytes) generating INFORMATIONAL_V1 response 4026938035 [ HASH D ] sending packet: from 2.3.4.5[500] to 1.2.3.4[500] (76 bytes) 这表示IPSec连接已成功建立。 现在,您可以从Ubuntu 20.04公网服务器上访问华为防火墙的本地子网,并从华为防火墙上访问Ubuntu 20.04公网服务器的本地子网。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北京橙溪科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值