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

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



#fetch ip address set for given device



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

bash 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 /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.



getip ${1}

#fetch ip address set for given device


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

sudo 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 /usr/local/bin/setip

Justin Henneberg
Justin is a long time Linux Admin, with extensive experience breaking systems – serious fun in tweaking 'til you kill it. If you break enough systems you can fix just about anything. A little nefarious fun adds the element of knowing how to help harden against security threats. Add a bit of hardware design, and you have the recipe for fun. Always remember “All electronics run on smoke. If you let the smoke out, they stop running.”

Expert Tip

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

If the issue is with your Computer or a Laptop you should try using Restoro which can scan the repositories and replace corrupt and missing files. This works in most cases, where the issue is originated due to a system corruption. You can download Restoro by clicking the Download button below.

Download Now

I'm not interested