42

When I attempt to ping google's dns or any outside the network I get connect: Network is unreachable?

I can't update either which I put down to this

I am new to networking... And Ubuntu. But these are results of some commands I thought might help

$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:0e:7f:a9:10:54 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.5/24 brd 192.168.0.255 scope global eth0
    inet6 fe80::20e:7fff:fea9:1054/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN 
    link/ether 86:0b:cb:43:63:a5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
mcserver@helloworld:~$ 

$ vi /etc/resolv.conf:
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 192.168.0.5
nameserver 8.8.8.8

$ vi /etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
        address 192.168.0.5
        netmask 255.255.255.0
        network 192.168.1.0
        broadcast 192.168.0.255
        post-up iptables-restore < /etc/iptables.up.rules
        dns-nameservers 192.168.0.5 8.8.8.
chaos
  • 27,106
  • 12
  • 74
  • 77
user240010
  • 675
  • 3
  • 8
  • 14
  • 2
    What's the output of `ip route show` – GnP Feb 03 '14 at 22:08
  • 2
    192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.5 192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 – user240010 Feb 03 '14 at 22:59
  • 18
    `ip route add default via 192.168.0.1 dev eth0` should get you connectivity, assuming your routers ip address is 192.168.0.1. If this works (try `ping 8.8.8.8` first, then google.com) check my answer for persisting the changes. – GnP Feb 04 '14 at 14:49
  • @user240010, You wrote an excellent solution. May I ask how you figured it out? Thanks. – Frank Aug 16 '16 at 20:54
  • @GnP, You wrote an excellent and clever solution. I discovered just now that it does not persist after logout. Could you please tell me how to persist it? Thanks. – Frank Aug 16 '16 at 21:03
  • @Frank I'm not sure I understand the question. Network configuration isn't affected by login/logout except in very particular circumstances (nm+wifi maybe). The answer I posted below is a `/etc/network/interfaces` entry, which is persistent accross reboots. – GnP Aug 16 '16 at 22:02
  • @GnP, Thank you for your reply tonight. I tested the 1 answer below and it had no effect after logout and login. I really admired your ip route add default via 192.168.0.1 dev eth0 idea that it should get you connectivity, assuming your routers ip address is 192.168.0.1. . How might we make it persist? – Frank Aug 17 '16 at 01:09
  • @GnP, I made the mistake of editing my network connection to automatic DHCP which forces a renewal of DHCP lease after logout and relogin and destroys persistence across reboots. How could I fix that mistake on my part? Thank you.' – Frank Aug 17 '16 at 01:23
  • @Frank depends on what you did, what type of connection, etc. You should ask a new question, posting your current config and whatever else you find relevant. – GnP Aug 17 '16 at 13:00
  • https://superuser.com/a/667800/476079 – Kiran Reddy Feb 19 '19 at 06:34
  • Finally, the below helped me resolve the issue https://datawookie.netlify.com/blog/2018/10/dns-on-ubuntu-18.04/ – Kiran Reddy Feb 28 '19 at 06:14

4 Answers4

21

The following line is wrong:

iface eth0 inet static
    address 192.168.0.5
    netmask 255.255.255.0
    network 192.168.1.0 <<<<<<<<<<<<<<<<<<<<<<<< It should be 192.168.0.0
    broadcast 192.168.0.255
    post-up iptables-restore < /etc/iptables.up.rules
    dns-nameservers 192.168.0.5 8.8.8.8

Also, there doesn't seem to be any default gateway setup.

Not having more info about your network, I would suggest adding the following line at the end:

    gateway <YOUR.ROUTER.IP.ADDRESS>

So, assuming your router ip address is 192.168.0.1, this would be the whole entry:

iface eth0 inet static
    address 192.168.0.5
    netmask 255.255.255.0
    network 192.168.0.0
    broadcast 192.168.0.255
    post-up iptables-restore < /etc/iptables.up.rules
    gateway 192.168.0.1
    dns-nameservers 192.168.0.5 8.8.8.8
GnP
  • 367
  • 2
  • 7
12

The problem is that you need to set your default gateway

  1. Take terminal

  2. Type in

    sudo ip route add default via <your gateway>(eg:192.168.136.1) dev eth0
    
  3. Sometimes you will be able to ping (ping 8.8.8.8) but no internet connection in the browser, then:

  4. Go to sudo nano /etc/resolv.conf

  5. Add nameserver 8.8.8.8

  6. Add nameserver <your gateway> or nameserver 127.0.1.1

  7. sudo /etc/init.d/networking restart or sudo service networking restart

cocomac
  • 3,043
  • 3
  • 16
  • 49
akhil kumar
  • 121
  • 1
  • 2
  • 3
    Welcome to askubuntu. Two remarks: 1. Refrain answering questions with an accepted answer unless you really come with something different. 2. Never advise to `sudo su`, as this is considered bad practice here for many reasons. – Marc Vanhoomissen Nov 28 '17 at 11:33
3

You may need to add a default gateway. As a root user, execute this command.

E.g.

root@localhost:~# route add default gw 172.23.5.1

You can get the first 3 octets, 172.23.5 from eth0/eoM

Then ping an IP to see if connection works.

root@localhost:~# ping 10.56.94.81    
PING 10.56.94.81 (10.56.94.81) 56(84) bytes of data.    
64 bytes from 10.56.94.81: icmp_seq=1 ttl=62 time=0.203 ms    
64 bytes from 10.56.94.81: icmp_seq=2 ttl=62 time=0.197 ms    
64 bytes from 10.56.94.81: icmp_seq=3 ttl=62 time=0.210 ms    
^C    
--- 10.56.94.81 ping statistics ---    
3 packets transmitted, 3 received, 0% packet loss, time 2033ms    
rtt min/avg/max/mdev = 0.197/0.203/0.210/0.012 ms
Stephen Rauch
  • 1,156
  • 6
  • 14
  • 20
0

In my case, if your system is behind a firewall, that can't interact with internet. Unless you specify a proxy server for HTTP/HTTPS/FTP, it is not reachable.

Try this:

export  http_proxy=http://yourcompanyproxy.company.com:1234   
export  ftp_proxy=ftp://yourcompanyproxy.company.com:1234  
export  https_proxy=https://yourcompanyproxy.company.com:1234