How Do I Find Out My Linux Gateway / Router IP Address?

See all GNU/Linux related FAQ
How do I find out my gateway IP for a computer or a network device that allows or controls access to another computer or network under Linux / UNIX-like operating systems? How can I find out my Linux Gateway / Router IP address using the CLI?

A gateway (default route) is a network point that acts as an entrance to another network. On the Internet, a node or stopping point can be either a gateway node or a host (end-point) node. Both the computers of Internet users and the computers that serve pages to users are host nodes. The computers that control traffic within your company’s network or at your local Internet service provider (ISP) are gateway nodes. In the network for an enterprise, a computer server acting as a gateway node is often also acting as a proxy server and a firewall server. A gateway is often associated with both a router, which knows where to direct a given packet of data that arrives at the gateway, and a switch, which furnishes the actual path in and out of the gateway for a given packet.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux terminal
Category Network Utilities
OS compatibility AIX AlmaLinux Alpine Amazon Linux Arch CentOS Debian Fedora FreeBSD HP-UX Linux macOS Mint Mint NetBSD OpenBSD openSUSE Pop!_OS RHEL Rocky Slackware Stream SUSE Ubuntu Unix WSL
Est. reading time 4 minutes
Advertisement

Command to find out my Linux Gateway / Router IP address using ip command

The latest version of all Linux distribution based upon systemd. These Linux distros user can use the ip command to find out router IP address.
$ ip r
$ ip route

Command to find out my Linux Gateway Router IP address
From the above output it is clear that 10.105.28.1 is the default gateway IP address. If you have multiple gateways configured, the following command will tell you how the destination is reached for the IP address 1.1.1.1 and 192.168.2.20:
{vivek@wks01 ~} ip route get 1.1.1.1
Outputs:
1.1.1.1 dev wg1 table 52267 src 192.168.13.4 uid 1000
    cache

{vivek@wks01 ~} ip route get 192.168.2.20
Outputs:
192.168.2.20 dev enp5s0 src 192.168.2.25 uid 1000
    cache

To send packets to the IP address 1.1.1.1, use the interface named ‘wg1‘ and prefer the source IP address ‘192.168.13.4‘. This route is located in table 52267 and was learned automatically (cached) and The User ID (UID) 1000 associated with this route. : The interface ‘wg1‘ is a virtual interface used for WireGuard VPN tunnels.

How to find default Gateway IP in Linux

Some computers might have multiple default gateways. For example, OpenVPN or WireGuard client software might setup gateway for VPN traffic. Another case is static gateway IP address set up for different networks on your system. Here is how to filter out ip command outputs with grep command:
$ ip r | grep default
How to Find Default Gateway IP in Linux
In this above example 10.8.0.1 and 192.168.2.254 are the default gateway IP address. The gateway with lowest Metric is the first to be searched and used as the default gateway. In this case, 10.8.0.1 has the 50 as metric cost and 192.168.2.254 has the 100 as metric cost. Linux will always use the one with the lowest cost first. So all traffic will be routed using 10.8.0.1 gateway IP address.

Find out Gateway / router IP address under Linux / UNIX using route command

You need to use the router command command under Unix-like systems such as FreeBSD or macOS and others. This command can manipulate the kernel’s IP routing tables. It can also be used to print gateway/router IP address. Type the following command to see default gateway. For example:
$ route -n
Sample outputs:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 ra0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 ra0

192.168.1.1 is gateway IP address for our computer. The flag U indicates that route is up and G indicates that it is gateway. You can print gateway name, enter:
$ route
Sample output:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     *               255.255.255.0   U     0      0        0 ra0
default         wrt             0.0.0.0         UG    0      0        0 ra0

Second column prints Gateway hostname / IP address. In our example wrt is gateway.

netstat command example to find out gateway/router IP on Unix/FreeBSD/OpenBSD/macOS

The route command may not work on *BSD and macOS to see GATEWAY IP address. Hence, ppen a terminal and type the following netstat command:
$ netstat -r -n
Sample outputs:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.1.254   0.0.0.0         UG        0 0          0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth1

The route and netstat command demo

Animated gif 01: Use route and netstat command to find out default gateway/router IP address on Linux

Animated gif 01: Use route and netstat command to find out default gateway/router IP address on Linux

Conclusion

I hope you learned about finding the default gateway IP in the Linux command-line option. Let us know if you need any more help in the comment section below. For more information please documentations by typing the help command or man command:
$ man ip
$ man netstat
$ man route
$ ip route help

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

37 comments… add one
  • Cristian Sep 27, 2012 @ 10:33

    THX man. learning linux now and i like it. it is so complex, so many things to learn :D

  • Zhei Kent Oct 3, 2012 @ 6:20

    thanks :)

  • dinesh Rao Jul 22, 2013 @ 13:19

    Thanks………..

  • Walle Apr 2, 2014 @ 5:12

    Hi. I ve seen a couple of very helpful replies here so I was hoping to get an answer to my problem here.

    I am located in Japan and got my self an internet contract. It looks like a DSL router. Though I am not sure what I actually ordered. The usual way to establish connection is only by a CD on Windows. I am pretty sure I can somehow make that darn thing work with Ubuntu as well, I just dont know how.

    If editing a DSL connection to the Network Manager it still doesnt work. But I am not very skilled with that. There I tiped in my User data and thats it. I dont know wether of how to fill out the PPPoE or do it automatically. I also wouldnt know the addresse to fill in here.

    Then I also tried some sudo pppoe config command but that wouldnt help me either. Afterwards I would always have to get to the etc/network/interfaces file to set everything back so that it wouldnt take 10 times longer to start the laptop cause of the wrong network configurations.

    I thought to myself it could be done by accessing the router with an Windows Laptop, but I dont know my neighbours too well to borrow one. However do you think that might be a solution as well ?

    Thanks for your comments in advance

    Walle

    • jim Aug 6, 2014 @ 10:27

      if you plug the router in and turn it on, plug a network cable between your linux computer, it should connect just by using DHCP… The CDs ISPs give out have bloatware like anti-virus, online backup storage (never enough!), dumb wireless managers etc. A network is a netowrk, and there is no need for all the software on the CD…

  • Amit Saha Sep 14, 2014 @ 13:07

    Nice description…Thanks..

  • David Dykstra May 23, 2015 @ 18:43

    If you have curl installed you can also use:

    curl ident.me

  • Jj Jun 2, 2015 @ 14:53

    Hello i dont have this ip i have just 0.0.0.0

    0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 venet0

    what is the good ip ?

  • coderkid Dec 5, 2016 @ 12:02

    i have been told to use ‘ip route’. does this work the same way? sorry if this is obvious i am new to linux
    any help is appreciated.
    thanks in advance

  • Sergio Araujo Mar 2, 2017 @ 18:46

    route -n | awk ‘/UG/ {print $2}’

  • R. H. Feb 8, 2021 @ 9:30

    unbrauchbar und am Ziel vorbei!
    Es soll angezeigt werden, wie IPV4-Adressen mit einem Linuxbefehl angezeigt werden

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.