1

Infrastructure : Microsoft Azure OS : Ubuntu 18.04

Issue : We have assigned 2 NIC card (same subnet). We are able to SSH with primary IP (20.71.0.21) address but unable to connect with secondary address (20.71.0.22). This is working on Ubuntu 16.04. I would like to set the route.

> :~# ip r l  
> default via 10.171.0.1 dev eth0 proto dhcp src 20.71.0.21
> metric 100
> 20.71.0.0/24 dev eth0 proto kernel scope link src 20.71.0.21
> 20.71.0.0/24 dev eth1 proto kernel scope link src 20.71.0.22
> 168.63.129.16 via 20.71.0.1 dev eth0 proto dhcp src 20.71.0.21 metric 100
> 169.254.169.254 via 20.71.0.1 dev eth0 proto dhcp src 20.71.0.21 metric 100

> :~# route -n
> Kernel IP routing table Destination    Gateway        
> Genmask         Flags Metric Ref    Use Iface
> 0.0.0.0         20.71.0.1       0.0.0.0         UG    100    0        0 eth0
> 20.71.0.0       0.0.0.0         255.255.255.0   U     0      0        0 eth0
> 20.71.0.0       0.0.0.0         255.255.255.0   U     0      0        0 eth1
> 168.63.129.16   20.71.0.1       255.255.255.255 UGH   100    0        0 eth0
> 169.254.169.254 20.71.0.1       255.255.255.255 UGH   100    0        0 eth0

:~# cat /etc/netplan/50-cloud-init.yaml

ethernets:
    eth0:
        dhcp4: true
        dhcp4-overrides:
            route-metric: 100
        dhcp6: false
        match:
            macaddress: 00:0d:3a:ab:xx:xx
        set-name: eth0
    eth1:
        dhcp4: true
        dhcp4-overrides:
            route-metric: 200
        dhcp6: false
        match:
            macaddress: 00:0d:3a:xx:xx:xx
        set-name: eth1
version: 2
> :~# cat /etc/netplan/60-static.yaml network:
>     version: 2
>     ethernets:
>         eth0:
>             addresses:
>                 - 20.71.0.21/24
>         eth1:
>             addresses:
>                 - 20.71.0.22/24

How can we resolve this issue?

  • "We are able to SSH with 1st IP address" PLZ add info : connect from / to. – cmak.fr Jan 17 at 7:01
1

Now, I got the solution to 2nd NIC routable to the internet, please run the below commands to configure routes for the 2nd NIC.

#et_ip=`ip addr show eth1 | awk '/inet / {split($2, a, "/"); print a[1]}'`
#def_gw=`ip route show | awk '$1 ~ "default" {print $3}'`
#sudo ip route add default via $def_gw dev eth1 table 11
#sudo ip rule add from $et_ip table 11

The above steps, will configure the 2nd NIC and also make the IP routable over Internet(if the 2nd NIC is configured with any Public IP), the above routes are not persistent across reboots. You either need to create a systemd service to run these routes during boot or you can add the above route options mentioned in the Step3 to your .bashrc profile file(~/.bashrc)

Persistent Routes Option via a Systemd service:

1) Create a file with the below contents at any directory you desire. As an example, I created in the /sbin directory.

root@ubuntu18-2:/sbin# cat route.sh

#!/bin/bash

et_ip=`ip addr show eth1 | awk '/inet / {split($2, a, "/"); print a[1]}'`

def_gw=`ip route show | awk '$1 ~ "default" {print $3}'`

ip route add default via $def_gw dev eth1 table 11

ip rule add from $et_ip table 11

2) After creating the file, make sure to set the proper permission

#chmod 777 **/sbin/route.sh**

3) Now create a file named route.service in /etc/systemd/system with below contents

root@ubuntu18-2:/etc/systemd/system# cat route.service

[Unit]

Description=To configure routes for 2nd NIC

[Service]

ExecStart=/sbin/route.sh

Type=oneshot

RemainAfterExit=yes

[Install]

WantedBy=multi-user.target

4) Once /etc/systemd/system/route.service file is created, run the below command to make sure the service is set to start during boot.

#sudo systemctl enable route

#sudo systemctl start route

Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.