How can I find the default gateway of a machine?
I’m trying to find the default Gateway of a machine, but I see multiple interfaces.
Also, when I find my IP address from the below website, it gives me a different IP address. I thought the default Gateway and external IP would be the same? Correct me if I’m wrong.
3 Answers 3
destination 0.0.0.0 and genmask 0.0.0.0 matches any other packet which is not defined in routing tables (last way out), so your default gateway is: 10.0.2.2 .
You can also use:
I thought the default Gateway and external ip will be same.
Not true, for example you can forward your traffic to your router with an internal IP address but your the router will replace the source address with its own public IP (NAT).
1st Method:
ip route show or ip route or ip r
You will get some rows , the row where it says ‘default’ is the row that gives you information on default router ip.
To narrow down the result and finding/displaying only ‘default’ row you can filter it like this.
Finding Your Router's IP Address (Default Gateway) in Ubuntu and Other Linux
I am not talking about the public-facing IP which you can get by connecting to websites like Show My IP or simply searching for ‘what is my ip’ in DuckDuckGo.
I am talking about the default gateway IP which your Linux desktop uses to connect to it.
Why do you need it? Well, if you need to change the SSID, password, or other configuration of your wi-fi/network, you have to connect to it. And the simplest way is to type the router’s IP address in a web browser and then use the router’s username and password.
While I cannot help you with the username and password of your router, I can surely tell you how to get its IP.
As always, I’ll show both GUI and command-line methods.
Method 1: Get the router’s IP address in Linux using GUI
It’s quite simple actually. I am using GNOME desktop with Ubuntu here. If you use some other desktop environments, screenshots may look different.
Open System Settings:
Go to Settings
Now go to Wi-Fi or Network (if you are using a wired, Ethernet connection). Here, click on the little settings symbol beside your currently used network.
It will open a new window with several details about your connection such as the IP address, DNS, and Mac address. You can also see the saved wifi password under the security tab.
You’ll also see an entry named ‘Default Route’. This is what you are looking for. The IP address of your router.
Default Route is the IP address of your router
Your system and all other devices on your network connect to the router using this IP address. This is the setup most households have.
Now that I have shown the GUI method, let’s go to the terminal route.
Method 2: Get the router’s IP address in Linux command line
Open a terminal and use the following command:
It will show you a few entries.
The first line, which starts with ‘default via’, gives you the gateway IP. This is your router’s IP address.
Getting default gateway in Linux command line
As you can see, 192.168.1.1 is the IP address of my router. Usually, the router’s IP address is the first number of the subnet. However, this is not a hard and fast rule. I have seen routers with x.y.z.30 addresses as well.
Bonus tip
As shared by Samir in the comments, you can also use the ping command to get the gateway IP:
Ping gateway
How to Find Default Gateway IP in Linux
This quick Linux tip shows various methods to find gateway IP address of your router in Linux command line.
In an earlier article, I told you about finding IP address in Linux command line. In this quick tip, I’ll show you how to find the default gateway IP in Linux command line.
A gateway is works as the entrance or a door between two networks. A router is an example of the gateway. All your traffic goes to the router and then to the rest of the internet.
Sometimes, you’ll need to know the IP address of your router. The gateway IP is your router’s IP address in the normal setup.
I am going to use the IP command to show the gateway IP in Linux.
Open a terminal and use the following command:
You should see an output like this:
Focus on the line that starts with default. This will give the default gateway IP.
Alternatively and conveniently, you can use the above command in combination with the grep command:
This will just give the default gateway IP in the output:
And as you can see, 192.168.0.1 is the default gateway IP in my case.
Other methods to find gateway IP address in Linux
The IP command in Linux provides most of your basic networking needs. But as you have already noticed by now, there are multiple ways to do a certain things in Linux.
To know the gateway IP, you can use other networking command line tools as well. Let me show them to you.
Find gateway in Linux with route command
You can use the -n option with the route command to display the routing table with the IP addresses.
The sample output should be like this:
Notice the U and G flags? U means the route is ‘up’ and the G indicates that it is gateway.
Show gateway in Linux with netstat command
To display the gateway information, you can use the netstat command and display the routing table that consists the gateway as well.
Output should be identical to what you saw with the route command:
You can identify the gateway with the G flag.
Conclusion
I hope this quick Linux tip helped you in finding the default gateway IP in Linux command line. Add this website to your feed reader for such regular Linux tips and tutorials.
How do I get the default gateway in Linux given the destination?
I’m trying to get the default gateway, using the destination 0.0.0.0 .
I used the command netstat -rn | grep 0.0.0.0 and it returned this list:
My goal here is to ping the default gateway using the destination 0.0.0.0 , which is 133.88.31.70 , but this one returns a list because of using grep .
How do I get the default gateway only? I need it for my Bash script to determine whether the network connection is up or not.
15 Answers 15
You can get the default gateway using ip command like this:
The ip route command from the iproute2 package can select routes without needing to use awk / grep , etc to do the selection.
To select the default route (from possibly many):
To select the next hop for a particular interface:
In case of multiple default gateways, you can select which one gets chosen as the next hop to a particular destination address:
You can then extract the value using sed / awk / grep , etc. Here is one example using bash ‘s read builtin:
works on any linux:
This simple perl script will do it for you.
Basically, we run netstat, save it to $ns. Then find the line that starts off with 0.0.0.0. Then the parentheses in the regex saves everything inside it into $1. After that, simply print it out.
If it was called null-gw.pl, just run it on the command like:
or if you need it inside a bash expression:
This is how I do it:
For a list of all default gateways, use mezgani’s answer, duplicated (and slightly simplified) here:
If you have multiple network interfaces configured simultaneously, this will print multiple gateways. If you want to select a single known network interface by name (e.g. eth1 ), simply search for that in addition to filtering for the ^default lines:
You can make a script that takes a single network-interface name as an argument and prints the associated gateway:
As noted in the comments, this has the advantage of setting a sensible exit-code, which may be useful in a broader programmatic context.