- How do I get the default gateway in Linux given the destination?
- 15 Answers 15
- Finding Your Router’s IP Address (Default Gateway) in Ubuntu and Other Linux
- Method 1: Get the router’s IP address in Linux using GUI
- Method 2: Get the router’s IP address in Linux command line
- Bonus tip
- How to Find Default Gateway IP in Linux
- Other methods to find gateway IP address in Linux
- Find gateway in Linux with route command
- Show gateway in Linux with netstat command
- How can I find the default gateway of a machine?
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:
Destination Gateway Genmask Flags MSS Window irtt Iface 10.9.9.17 0.0.0.0 255.255.255.255 UH 0 0 0 tun0 133.88.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 133.88.31.70 0.0.0.0 UG 0 0 0 eth0
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:
IP=$(/sbin/ip route | awk '/default/ < print $3 >') echo $IP
What if there is more than one default gateway? Your script yields me 192.168.1.1 192.168.0.1 , and this is true: I have two gateways to internet. But the main one is 192.168.1.1 . Should the first default value be selected as the main (prioritary) one?
@SopalajodeArrierez In my opinion, you should select the gateway for the particular interface you’re interested in, e.g. /sbin/ip route |grep ‘^default’ | awk ‘/eth1/
@KyleStrand There is a «metric» value for each route entry. The smaller one gets priority. Is there any way to use that value to find the default gateway?
@StarBrilliant There might be some way to do that with ‘awk’, but failing that I’d recommend just writing a small script that parses each line of output and searches for the smallest metric value.
@StarBrilliant Actually, if you know what column the «metric» will be in, sort will do most of the work. -k3,3 specifies (for instance) sorting by the third column, and -n indicates that the sort should be done numerically. You can then use head -1 to get only the first line, and get the relevant column (the gateway) using awk as in the answer.
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):
$ ip -4 route show default # use -6 instead of -4 for ipv6 selection. default via 172.28.206.254 dev wlp0s20f3 proto dhcp metric 600
To select the next hop for a particular interface:
$ ip -4 route list type unicast dev eth0 exact 0/0 # Exact specificity default via 172.29.19.1 dev eth0
In case of multiple default gateways, you can select which one gets chosen as the next hop to a particular destination address:
$ ip route get $(dig +short google.com | tail -1) 173.194.34.134 via 172.28.206.254 dev wlan0 src 172.28.206.66 cache
You can then extract the value using sed / awk / grep , etc. Here is one example using bash ‘s read builtin:
I like this because ip already outputs the correct line. A slightly improved version based on this is: ip -4 route list 0/0 | cut -d ‘ ‘ -f 3
@tosh — technically, they are the same 0.0.0.0/0.0.0.0 is the default route — as for the example ip route show to default or perhaps ip route show default would be more explicit, I will update.
Why do these linux tools rarely provide a way to output just the information you want instead of having to awk sed grep cut? Why is there not just a simple -o/—output via flag to route so you do not have to worry about the output format changing.
route -n|grep "UG"|grep -v "UGH"|cut -f 10 -d " "
route -n | awk ‘$4 == «UG»
This simple perl script will do it for you.
#!/usr/bin/perl $ns = `netstat -nr`; $ns =~ m/0.0.0.0\s+(4+.3+.5+.5+)/g; print $1
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:
I know this is years old. the problem with your regex is it will match anything that has 0.0.0.0 followed by an ip address. this means if your gateway is 0.0.0.0 then it will return the genmask. Specifically, the «G» flag indicates a gateway. something like this would be more accurate: echo $(route -n | perl -ne ‘print m/0.0.0.0\s+(9+.6+.6+.1+)\s+0.0.0.0\s+\wG/g’) there is no difference between netstat -nr and route -n
#!/bin/sh GATEWAY_DEFAULT=$(ip route list | sed -n -e "s/^default.*[[:space:]]\([[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\.[[:digit:]]\+\).*/\1/p") echo $
For a list of all default gateways, use mezgani’s answer, duplicated (and slightly simplified) here:
/sbin/ip route | awk '/^default/ < print $3 >'
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:
/sbin/ip route |grep '^default' | awk '/eth1/ '
You can make a script that takes a single network-interface name as an argument and prints the associated gateway:
#!/bin/bash if [[ $# -ne 1 ]]; then echo "ERROR: must specify network interface name!" >&2 exit 1 fi # The third argument of the 'default' line associated with the specified # network interface is the Gateway. # By default, awk does not set the exit-code to a nonzero status if a # specified search string is not found, so we must do so manually. /sbin/ip route | grep '^default' | awk "/$1/ END"
As noted in the comments, this has the advantage of setting a sensible exit-code, which may be useful in a broader programmatic context.
Finding Your Router’s IP Address (Default Gateway) in Ubuntu and Other Linux
Looking for a way to connect to your router but don’t know its address? Here’s how to get the IP address of your router in Ubuntu and other Linux systems.
You probably already know how to get your system’s IP address in Linux. But how do you know the IP address of your router? 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: 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. 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
[email protected]:~$ ip route default via 192.168.1.1 dev wlp0s20f3 proto dhcp metric 600 169.254.0.0/16 dev wlp0s20f3 scope link metric 1000 192.168.1.0/24 dev wlp0s20f3 proto kernel scope link src 192.168.1.34 metric 600
The first line, which starts with ‘default via’, gives you the gateway IP. This is your router’s IP address. 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
In case you didn’t know, you have to use the Ctrl+C to stop a running command in Linux. I hope you find this tip useful when you need it.
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:
default via 192.168.0.1 dev wlp58s0 proto dhcp metric 600 169.254.0.0/16 dev wlp58s0 scope link metric 1000 192.168.0.0/24 dev wlp58s0 proto kernel scope link src 192.168.0.106 metric 600
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:
default via 192.168.0.1 dev wlp1s0 proto dhcp metric 600
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:
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.0.1 0.0.0.0 UG 600 0 0 wlp58s0 169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 wlp58s0 192.168.0.0 0.0.0.0 255.255.255.0 U 600 0 0 wlp58s0
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:
Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 wlp58s0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 wlp58s0 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 wlp58s0
You can identify the gateway with the G flag.
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 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.
https://www.privateinternetaccess.com/pages/whats-my-ip/
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.0.2.2 0.0.0.0 UG 0 0 0 eth0 10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 10.2.0.0 192.168.255.9 255.255.255.0 UG 0 0 0 tun0 192.168.33.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 192.168.255.1 192.168.255.9 255.255.255.255 UGH 0 0 0 tun0 192.168.255.5 0.0.0.0 255.255.255.255 UH 0 0 0 tun4 192.168.255.5 0.0.0.0 255.255.255.255 UH 0 0 0 tun6 192.168.255.5 0.0.0.0 255.255.255.255 UH 0 0 0 tun5 192.168.255.5 0.0.0.0 255.255.255.255 UH 0 0 0 tun2 192.168.255.9 0.0.0.0 255.255.255.255 UH 0 0 0 tun0 192.168.255.13 0.0.0.0 255.255.255.255 UH 0 0 0 tun3 192.168.255.13 0.0.0.0 255.255.255.255 UH 0 0 0 tun1
$ netstat -r Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface default 10.0.2.2 0.0.0.0 UG 0 0 0 eth0 10.0.2.0 * 255.255.255.0 U 0 0 0 eth0 10.2.0.0 192.168.255.5 255.255.255.0 UG 0 0 0 tun6 192.168.33.0 * 255.255.255.0 U 0 0 0 eth1 192.168.255.1 192.168.255.5 255.255.255.255 UGH 0 0 0 tun6 192.168.255.5 * 255.255.255.255 UH 0 0 0 tun6 192.168.255.5 * 255.255.255.255 UH 0 0 0 tun3 192.168.255.5 * 255.255.255.255 UH 0 0 0 tun2 192.168.255.5 * 255.255.255.255 UH 0 0 0 tun4 192.168.255.9 * 255.255.255.255 UH 0 0 0 tun5 192.168.255.13 * 255.255.255.255 UH 0 0 0 tun0 192.168.255.13 * 255.255.255.255 UH 0 0 0 tun1