328

I have Ubuntu server 12.04 installed, so I have no GUI. When I do the command ifconfig, I cannot find my internal IP address. It says: inet addr: 127.0.0.1.

Here is the output of ifconfig -a:

eth0   link encap:Ethernet  HWaddr 00:06:4f:4a:66:f0
    BROADCAST MULTICAST  MTU:1500  Metric:1
    RX packets:0 errors:0 dropped:0 overruns:0 frame:0
    TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth1 link encap:Ethernet HWaddr 00:16:ec:05:c8:9c BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

lo Link encap:Local Loopback inet addr 127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:1800 errors:0 dropped:0 overruns:0 frame:0 Tx packets:1800 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:143896 (143.b KB) TX bytes:143896 (143.8 KB)

here are the contents of /etc/network/interfaces:

# The loopback network interface
auto lo
iface lo inet loopback

The primary network interface

auto eth0 iface eth0 inet dhcp

If someone could edit this for me, the contents of etc/network/interfaces should be on separate lines.

The output of host askubuntu.com was:

;; connection timed out; no servers could be reached.

I set up owncloud and webmin a few months ago and was using them for a month with no problems. I think the power went off one day 2 months ago and I never turned the server back on until yesterday. I haven't done anything that would have affected the internet setup So i'm not sure why it doesn't work anymore. As far as my network topology goes, I have a pci-e network card for the pc. The ethernet line goes from the network card to a switch, and then to a modem/router from there.

Pilot6
  • 92,041
Cam Jones
  • 3,891

10 Answers10

442
hostname -I

This will give you just ip address without any extra information.

260

These commands will tell you all network info

ip address

or

ifconfig -a

If as you say it only gives you 127.0.0.1 then there are two options:

  1. Your network card is not attached or not recognized by the system

  2. Your network DHCP server is not runnning or not connected

Flimm
  • 44,031
kamil
  • 7,397
196

This is what I currently recommend:

ip route get 8.8.8.8 | awk '{print $NF; exit}'

The advantage of that command is that you don't have to know which interface you are using (eth0? eth1? or maybe wlan0?), you don't have to filter out localhost addresses, or Docker addresses, or VPN tunnels etc. and you will always get the IP address that is currently used for Internet connections at that very moment (important when e.g. you are connected with both ethernet and wifi or via VPN etc.).

This will test not only that you have a correct IP configured on some interface (like with parsing the output of ifconfig) but also that you have the routing table configured to use it correctly.

I found that idea in this answer by Collin Anderson.

I use it in the internalip script in my scripts collection on GitHub, which you can install with:

wget https://rawgit.com/rsp/scripts/master/internalip
chmod a+x internalip

and use as:

internalip

or:

internalip TARGET

and you will get your IP address that would be used to connect with the TARGET IP address. The default target is 8.8.8.8 which is the Google's public DNS resolver and a good default for the Internet.

But if you run it with a different IP:

internalip 127.2.3.4

Then you will get:

127.0.0.1

because that is your IP address that would be used to connect with 127.2.3.4 on the loopback interface. It's useful when your target is on a LAN, VPN or other special network, in which case some other IP could be used for connections than the default IP for reaching the Internet.

External IP

To check your external IP address (the one that the servers on the Internet see when you connect to them - which may be different than the internal IP address described here) see this answer.

guntbert
  • 13,475
rsp
  • 2,480
19

I think it worth mentioning that running ifconfig along with -a option will display all interfaces wether or not the interface has an IP.

running ifconfig alone, will display only interfaces with IPs assigned.

Here is a nice trick you could use to display only IPs using Perl.

# ifconfig | perl -nle'/dr:(\S+)/ && print $1'
192.168.1.100
127.0.0.1

Your network card is recognized by the system, that why its showing up eth0 and eth1

here is a quick way of assigning IP to your interface, use valid IP/Subnet accordingly.

 ifconfig eth0 192.168.1.200/24 up 

then we need to add a default route

route add default gw 192.168.1.1

Best,

11

This command will show all the IP addresses for a single device:

dev=eth0
ip addr show $dev | awk '/inet/ {print $2}' | cut -d/ -f1

It will print one or two lines of output. The first one is the inet/IPv4 address and the other one is inet6/IPv6 if your system is configured to support this.

8

In case, the ifconfig command didn't display the IP address, there is a very simple and easy way to find out the IP address of the Ubuntu machine through the GUI. Follow the steps below:

Click the network icon in the notification area and click Connection Information. Network icon options

This brings up a window which has a some information, including the IP address. IP address information

Ibungo
  • 250
1

There's lots of good answers here to choose from already, but I thought I'd point out facter which is usually, but not necessarily used with puppet for collecting various facts about the system. The main advantage of facter is that it gives nice clean output which saves you all the manipulation with grep, sed, awk, cut, perl, etc. It isn't going to tell you which interface you're interested in, but if you know that, then the following gives you the IP without other cruft to clean up:

facter ipaddress_eth0 
mc0e
  • 368
1

ip command

Here's a variation on ip addr way.

ip has -o option which allows putting all information on single line - this is useful for parsing with tools like awk or perl. In combination with -4 option, we will only see IPv4 addresses. Thus, output would be something like this (note - replace wlan7 with the interface you want to check):

$ ip -4 -o addr show wlan7
3: wlan7    inet 192.168.0.78/24 brd 192.168.0.255 scope global dynamic wlan7\       valid_lft 85654sec preferred_lft 85654sec

As you can see, the IP address is 4th column/word in the output. The rest , is simple parsing exercise via the tool of your choice. Here, I'm using python:

ip -4 -o addr show wlan7 | python -c "import sys;print sys.stdin.readlines()[0].split()[3]" 

Same thing, only with here string <<< and command substitution $()

python -c "import sys;print sys.stdin.readlines()[0].split()[3]"  <<< $(ip -4 -o addr show wlan7)

Personally, I have this saved as a nice function in my ~/.bashrc so less typing is done.

wlan_ip() {
    ip -4 -o addr show wlan7 | python -c "import sys;print sys.stdin.readlines()[0].split()[3]" 
} 

Perl's and Ruby's version are a bit shorter:

$ ip -4 -o addr show  wlan7 | perl -lane 'print $F[3]'                         
192.168.0.78/24

$ ip  -4 -o addr show wlan7 | ruby -n -e 'print $_.split()[3]'                 
192.168.0.78/24

Wireshark

If you're familiar with using network analysis tools, you probably know that to get your ip address, all you need to do is clear the browser cache, start Wireshark capture on the specific interface, and look for http GET packets being transmitted. The destination column will show your ip address

0

There is lots of good info above. I keep it simple like so:

# what your computer thinks its ip address is
function iplocal() {
  ip route get 8.8.8.8 | awk '{print $NF; exit}'    # 8.8.8.8 is google dns
}
# what the outside world thinks your ip address is
function ipexternal() {
  curl --silent http://checkip.amazonaws.com        # or:  http://ipinfo.io/ip
}
function ipinfo() {
  echo "local    IP  =>  $(iplocal)"
  echo "external IP  =>  $(ipexternal)"
}

Sample usage:

> ipinfo
local    IP  =>  192.168.1.139
external IP  =>  76.88.85.95
0

Anyone still having issues with finding your ip addres can simply use ip addr

it will list all the network devices, that are connected to your computer, for example in the case of OP, there are three devices connected, but it seems that none of these devices are up, the eth0 and eth1 are ethernet devices (sockets) but as you can see these devices have not been assigned any inet address's.


eth0   link encap:Ethernet  HWaddr 00:06:4f:4a:66:f0
    BROADCAST MULTICAST  MTU:1500  Metric:1
    RX packets:0 errors:0 dropped:0 overruns:0 frame:0
    TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth1 link encap:Ethernet HWaddr 00:16:ec:05:c8:9c BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

lo Link encap:Local Loopback inet addr 127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:1800 errors:0 dropped:0 overruns:0 frame:0 Tx packets:1800 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:143896 (143.b KB) TX bytes:143896 (143.8 KB)

whenever you will connect to an ethernet or wifi, you will be assigned an ip address and following is an example of how it will look like

6: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1410 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:95:07:a5 brd ff:ff:ff:ff:ff:ff
    inet 172.31.133.25/20 brd 172.31.143.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fe95:7a5/64 scope link
       valid_lft forever preferred_lft forever

See in my example above, the device is an ethernet connection, and it has been assigned inet address


How to read the information provided by ip command

at first glance, if you are not familier with networking this might seem very absurd to you, but its very clear, in the second line you will see inet this is your ip address - in my case its 172.31.133.25 and its written in CIDR Notation, 20 is there as a subnet mask bit, which you dont have to worry about

So in above case your ip address is just 172.31.133.25

For some ip might not be provide a very good looking or informative response so you can install net-tools using

sudo apt install net-tools

this will install ifconfig and ifconfig will show you things a little clearly like following

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1410
        inet 172.31.133.25  netmask 255.255.240.0  broadcast 172.31.143.255
        inet6 fe80::215:5dff:fe95:7a5  prefixlen 64  scopeid 0x20<link>
        ether 00:15:5d:95:07:a5  txqueuelen 1000  (Ethernet)
        RX packets 238  bytes 223167 (223.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 145  bytes 12170 (12.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

You can clearly see inet is set here and subnet mask is written not in CIDR notation, but differently

In both the cases above ipv6 is also mentioned as inet6.