Posted: . At: 8:24 AM. This was 3 years ago. Post ID: 14903
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



How to get the gateway IP address of your machine on Linux.


Getting the gateway IP address of your machine is a very important trick sometimes. This is very easy on Linux.

Use the ip route command on Linux to print information about the IP routing. This includes the gateway IP address.

┌──[jason@192.168.1.2][~/Videos]
└──╼  ╼ $ ip route | grep default | awk '{print $3}'
192.168.1.1

This is very simple and works just fine.

This is how to use the ip route command to print the IP address of your computer.

┌──[jason@192.168.1.2][~/Videos]
└──╼  ╼ $ ip route | sed -n -e '3{p;q}' | awk '{print $9}'
192.168.1.2

This command will print only the 3rd line of output on STDOUT.

sed -n -e '3{p;q}'

Then, use this command to only print the 9th column of output on that line.

awk '{print $9}'

Use this command to get the external Internet-facing IP address that you currently are issued from your Internet Service Provider.

┌──(john㉿DESKTOP-PF01IEE)-[/mnt/c/Users/Intel i5]
└─$ dig +short myip.opendns.com @resolver1.opendns.com
203.125.10.200

This is the proper way to accomplish this easily. There are many ways to get this kind of information. You may also use the IP(1) command to get routing information.

┌──(john㉿DESKTOP-PF01IEE)-[/mnt/c/Users/Intel i5]
└─$ ip -h -4 route
default via 172.26.32.1 dev eth0
172.26.32.0/20 dev eth0 proto kernel scope link src 172.26.43.110

This shows the network route I am currently using with Kali Linux.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.