Fix: Failed No Route to Host On Ubuntu With Multiple Networks

When you have a complex network setup, it is frustrating to have the “No Route to Host” start appearing after raising a new interface.  This is frequently due to the default route being either changed or dropped.

To check your default route enter

ip route show

no route to host-1

As show above I have three physical interfaces, one outward facing and two to internal networks.  The default route dropped on the network when a connection was reset, causing the rest of my network to operate normally, with only this machine not knowing where to reach the Internet.

For the machine to be able to reach the Internet it needs to have a default route which can be added with the following command

sudo ip route add default via 136.63.176.1

If you have this problem arise more than once or twice, it can become a nuisance, so I have a written a couple of shell scripts to quickly rectify the situation.

The first script will fetch your current ip for you.  I have $INTF set as STDIN, you change it to your specific interface if you don’t want to enter the interface name.

Using your favorite text editor, I like vim and have a source folder in my home directory

mkdir ~/src && cd ~/src

vim getip.sh

#!/bin/bash

#fetch ip address set for given device

INTF=${1}

PTH=(~/src/)

declare TMP_IP2

TMP_IP2=$(ip a | grep ${INTF} | grep inet | cut -d” ” -f6 | cut -d “/” -f1 > ${PTH}current_${INTF}.ip)

ip a | grep ${INTF} | grep inet | cut -d” ” -f6 | cut -d “/” -f1 | cut -d”.” -f1-3 | awk ‘{print $0″.0/24″}’ > ${PTH}net_${INTF}.ip

ip a | grep ${INTF} | grep inet | cut -d” ” -f6 | cut -d “/” -f1 | cut -d”.” -f1-3 | awk ‘{print $0″.1″}’ > ${PTH}gw_${INTF}.ip

declare TMP_IP

TMP_IP=$(cat ${PTH}current_${INTF}.ip)

echo “current wan ip $TMP_IP”

Save and exit, make it executable, and then test it.

sudo chmod a+x getip.sh

bash getip.sh eth0.2

no route to host-2

Assuming that worked properly for you go ahead and copy it into /usr/local/bin/ to make it globally executable.

sudo cp getip.sh /usr/local/bin/getip

Now I can simply type getip eth0.2 and the ip will be returned.

The second script will assign the default route, as well as running the first script.

vim setip.sh

#!/bin/bash

getip ${1}

#fetch ip address set for given device

PTH=(~/src/)

declare RT_IP

RT_IP=$(cat ${PTH}gw_${1}.ip)

ip route change default via $RT_IP

ip route add default via $RT_IP

echo “ip route — default via $RT_IP”

Save and quit.  You might have noticed that I have both change and add default route, this is for occasions that you have the wrong gateway set.

sudo chmod a+x setip.sh

sudo setip.sh eth0.2

no route to host-3

You can check the routing table again to double check that all is good.

sudo ip route show

You can copy the scripts into /usr/local/bin/ if you would like them to be globally executable.  It will still require sudo, as you are modifying and interface.

sudo cp setip.sh /usr/local/bin/setip

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.