41

What command can I run to get just the IP address of my router?

With this command I should have only IP of my router and not, for example, the whole routing table (as when I run route -n).

6 Answers 6

43

One-liners:

  • nm-tool | grep -i gateway | xargs echo | cut -d' ' -f2
  • nm-tool | grep -i gateway | awk '{print $2}
  • netstat -nr | awk '$1 == "0.0.0.0"{print$2}'
  • arp -n | awk '{print $1}' Note: works only if your machine is the only one on the network
  • ip route show | grep -i 'default via'| awk '{print $3 }'

output: 192.168.0.1 for me

NOTE:

For 15.04 and later, there is no nm-tool , so use nmcli dev show <IFACE>. For example,

$ nmcli dev show wlan7 | grep GATEWAY                                                                                    
IP4.GATEWAY:                            192.168.0.1
IP6.GATEWAY:                            

Edits and additional info

As you can see from examining the command, we take output of nm-tool (which,note, takes information from NetworkManager), find line containing word gateway, print than information with echo (separated by spaces), and then cut off only second item of that whole output. Note, that if you have two or more connections there, you may need top remove xargs echo | cut -d' ' -f2 part, and replace it with awk '{print $2}'; in other words the whole line would look like this nm-tool | grep -i gateway | awk '{print $2}'. Alternatively you could have used nm-tool | grep -oP '(?i)gateway:\s*\K\S+' as proposed by Avinash Raj in the comments. As you can see there is nothing special done here , and the whole ordeal here is merely an exercise in using output editing tools such as cut, awk, and grep.

Another method of getting the gateway information is through nmcli dev list (yes, still relying on network manager) command. nmcli is the command - line version of network manager. You could run nmcli dev list | grep -i routers or you could run nmcli dev list | grep -i 'gw =' . Again, you could exercise in in cutting of all the other info except the desired ip address, if you wanted to.

Since in the original question the only specification was to print the default gateway only, I am relying on output of nm-tool here. NetworkManager comes with Ubuntu by default, this is the standard way for managing Ubuntu's network connections. If you use something other , like wicd or connect through wpa_cli, nm-tool won't provide you an answer. In such case you may find other people's answers bellow more useful.

A more distro-neutral, and config-neutral option would be to use netstat -n , which uses kernel routing table, similar to route -n. It's output is bellow, nothing surprising.

  Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 wlan0
192.168.0.0     0.0.0.0         255.255.255.0   U     9      0        0 wlan0

And here's the way to cut out your desired info : netstat -nr | awk '$1 == "0.0.0.0"{print$2}'

Another one, neutral as well: arp -n | awk '{print $1}'

8
  • simple nm-tool | grep -oP '(?i)gateway:\s*\K\S+' Apr 5, 2015 at 4:30
  • @AvinashRaj Thanks ! I haven't studied grep and awk much in depth so operating on the knowledge i already had. This will be good to know Apr 5, 2015 at 4:36
  • 1
    Perhaps this is due to some (additional) peculiarity of my configuration, but on a machine with adapters eth0 and wlan0, both actively connected but with a gateway configured only on wlan0, nm-tool | grep -i gateway | xargs echo | cut -d' ' -f2 outputs merely 0.0.0.0. nm-tool | grep -oP '(?i)gateway:\s*\K\S+' outputs two lines: 0.0.0.0 followed by 192.168.1.1. (The ip route ... and route -n ... ways produce just 192.168.1.1, which I would consider the most desirable result.) Apr 5, 2015 at 17:21
  • This won't work if the network is not being managed by network-manager e.g. by ifupdown or ifconfig..in those cases nm-tool won't generate desired output..in a nutshell this answer solely relies on the network being managed by network-manager..
    – heemayl
    Apr 5, 2015 at 18:04
  • @EliahKagan Ah, I see. In case of my command, it finds lines with 'gateway' in it, then echoes it as one line separated by spaces, and cuts off the second item in it. Because you have two such lines there, my command only prints one of them. Avinash's command is better in this case. As for 0.0.0.0, here's something related. As for heemayl's comment, yes, that's true, and there were no specifications in the OP's question other than just to show the output Apr 5, 2015 at 18:09
15

You can find it many ways

ip route show default

A better question, what or how do you want to shape the output ?

ip route show | awk '/default/ {print $3}'

tracepath -m 1 8.8.8.8 | awk '/1:/ {print $2}' | uniq

From the comments -(thank you Avinash Raj 0

tracepath -m 1 8.8.8.8 | awk '/1:/ {print $2;exit}'
5
  • you don't need to o for uniq just tracepath -m 1 8.8.8.8 | awk '/1:/ {print $2;exit}' would be 5in. Apr 5, 2015 at 14:47
  • so many choices. On my system it prints twice w/o uniq. YOur solution works also.
    – Panther
    Apr 5, 2015 at 14:49
  • Note tracepath supports -m on some versions of Ubuntu but not on others. Apr 5, 2015 at 17:24
  • By the way, @bodhi.zazen, there's variation on your answer , use traceroute . Same idea different command, right ? Apr 5, 2015 at 18:29
  • @serg sure, there is probably a long list of commands and filtering options, sed, awk, perl, grep ...
    – Panther
    Apr 5, 2015 at 19:01
7

As you usually use route -n, you can try this sed solution coupled with route -n:

route -n | sed -nr 's/(0\.0\.0\.0) +([^ ]+) +\1.*/\2/p' 

Here is a test:

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth0
192.168.0.0     0.0.0.0         255.255.240.0   U     0      0        0 eth0

$ route -n | sed -nr 's/(0\.0\.0\.0) +([^ ]+) +\1.*/\2/p' 
192.168.1.1

Another way would be to use grep:

$ route -n | tr -s ' ' | grep -Po "(?<=0\.0\.0\.0 )[^ ]+(?= 0\.0\.0\.0)"
192.168.1.1

As @AvinashRaj has pointed out this can be done by only using grep (no need to squeeze the spaces using tr):

route -n | grep -Po "0\.0\.0\.0\s*\K\S+(?=\s*0\.0\.0\.0)"
192.168.1.1
1
  • you don't need to use tr, grep alone will do the job route -n | grep -Po "0\.0\.0\.0 \s*\K\S+(?=\s*0\.0\.0\.0)" Apr 5, 2015 at 4:21
1

I use this one quite often:

route -n | awk '{ print $2 }' | grep -Eo '[1-9]{0,3}\.[1-9]{0,3}\.[0-9]{0,3}\.[1-9]{0,3}'
1
  • What if the address is 192.168.0.1 ? Apr 5, 2015 at 14:50
1

If you have access to a web browser on the system you can go to http://whatsmyrouterip.com/ and it should be able to find your ip without any command.

Otherwise simply opening the terminal and doing a ip route | grep default should be enough. It might give more than one depending on your network interfaces tho.

0

I use this quite often and easy to remember:

route | grep default | awk '{print $2}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .