How to show (just) the IP address of my router?
Solution 1
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}'
Solution 2
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}'
Solution 3
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
Solution 4
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.
Solution 5
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}'
Related videos on Youtube
kaio
Updated on September 18, 2022Comments
-
kaio over 1 year
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
). -
Avinash Raj about 9 yearsyou don't need to use
tr
, grep alone will do the jobroute -n | grep -Po "0\.0\.0\.0 \s*\K\S+(?=\s*0\.0\.0\.0)"
-
Avinash Raj about 9 yearssimple
nm-tool | grep -oP '(?i)gateway:\s*\K\S+'
-
Avinash Raj about 9 yearsyou don't need to o for
uniq
justtracepath -m 1 8.8.8.8 | awk '/1:/ {print $2;exit}'
would be 5in. -
Panther about 9 yearsso many choices. On my system it prints twice w/o uniq. YOur solution works also.
-
Avinash Raj about 9 yearsWhat if the address is
192.168.0.1
? -
Eliah Kagan about 9 yearsPerhaps this is due to some (additional) peculiarity of my configuration, but on a machine with adapters
eth0
andwlan0
, both actively connected but with a gateway configured only onwlan0
,nm-tool | grep -i gateway | xargs echo | cut -d' ' -f2
outputs merely0.0.0.0
.nm-tool | grep -oP '(?i)gateway:\s*\K\S+'
outputs two lines:0.0.0.0
followed by192.168.1.1
. (Theip route ...
androute -n ...
ways produce just192.168.1.1
, which I would consider the most desirable result.) -
Eliah Kagan about 9 years
-
heemayl about 9 yearsThis won't work if the network is not being managed by
network-manager
e.g. byifupdown
orifconfig
..in those casesnm-tool
won't generate desired output..in a nutshell this answer solely relies on the network being managed bynetwork-manager
.. -
Sergiy Kolodyazhnyy about 9 years@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
-
Sergiy Kolodyazhnyy about 9 yearsBy the way, @bodhi.zazen, there's variation on your answer , use traceroute . Same idea different command, right ?
-
Panther about 9 years@serg sure, there is probably a long list of commands and filtering options, sed, awk, perl, grep ...
-
Eliah Kagan about 9 yearsThanks for the added explanations in the edit! One thing though: doesn't
arp -n
list all cached IP addresses for machines on the same layer 2 network as the host? I think the only time the gateway is the only IP address listed is when it's the only other machine the host has communicated with recently on a LAN. For example, when I run it, it lists the IP of my gateway (viawlan0
, through which I connect to the Internet) and also the IP of a machine I recentlyssh
ed into (througheth0
, which is connected to a LAN containing no routers). -
Sergiy Kolodyazhnyy about 9 yearsThat's true. Should I add a "disclamer" to it or just remove that one ?
-
Fabby about 9 years:D :D :D Another one-liner: "Upvoted"! ;-)