How to show (just) the IP address of my router?
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 networkip 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}'
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}'
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