Skip to Content

3 ways to find gateway ip in Linux

This article is part of the following series.

 

Finding your gateway IP address in Linux can be a little tricky if you don’t know where to look. In this blog post, I will show you three different ways to find your gateway IP address.

I have been using Linux for many years. I use different Linux commands every day to solve a wide range of problems. 

If you are not sure how to start, no worries. I will take you through the process.

To check the gateway in Linux, you can use ip route, route, or netstat command. Open the terminal and type in any of the above commands.  The gateway info will be listed in the command output.

I will also discuss some of the benefits and drawbacks of each method. Let’s get started!

Understanding gateway in Linux

Before I jump to the topic, it is necessary for your to know gateway.

A network gateway is a device that connects two networks together. In the context of Linux, a gateway is often used to connect a local network to the Internet.

A gateway can be a hardware device, such as a router, or it can be a software program, such as a proxy server.

By using a gateway, you can allow computers on your local network to access resources on the Internet.

Methods to get network gateway in Linux

There are a few ways that you can check the gateway in Linux. 

  • use the ip route command. This command displays all of the information about your network interfaces, including the gateway.
  • use the route command. This command allows you to view and modify the routing table on your system. To get the gateway, simply type “route -n” into the terminal. 
  • use the netstat command. This command shows all active network connections and their status. To get the network gateway, simply type “netstat -rn” into the terminal. 

 

Let’s take a closer look at each of these methods!

Related: 4 useful Linux Commands to Fix Network issue

Procedure to find network gateway in Linux

The procedure to check the network gateway in Linux is as follows:

  • Open the terminal application.
  • Type ip route command
  • Press Enter to run the command.
  • This command will display many information about your network interfaces, including the gateway.

 

Find gateway ip with ip route command in Linux

The easiest way to find the gateway ip in Linux is using ip route command.  Open the terminal and type “ip route”  and press Enter. The gateway ip is typically listed under the “default” section.

$ ip route
default via 172.31.32.1 dev eth0 proto dhcp metric 100
172.31.32.0/20 dev eth0 proto kernel scope link src 172.31.37.167 metric 100

When you run the ip route command without any arguments, it displays the current routing table. This includes information about the network destinations, the gateway, and the network interface associated with each route.

Furthermore, you can also use the ip route command to add or delete routing table entries, or to modify the properties of existing entries.

For example, you can use this command to set up a new static route, or to modify the metric of an existing route to give it higher or lower priority.

If you get any network connectivity issue, this command is your first choice.

The ip route command is a sub command of ip command. Let me briefly introduce ip command here.

The ip command is a more general-purpose command that is used for various networking tasks, such as configuring network interfaces, setting up virtual tunnels, and managing network namespaces. It can be used to perform a wide range of network-related operations.

For example, ip link can be used to view and manage network interfaces, ip addr can be used to view and configure IP addresses on those interfaces, and ip tunnel can be used to set up virtual network tunnels.

To learn more about ip command, I highly recommend the following article. 

3 useful Linux ip commands you need to know about networking

Related: 10 Linux Interview Questions about networking You Need to Know

Check gateway ip with route command in Linux

The route command is another way to view and modify the routing table on your system. To get the gateway, simply type “route -n” into the terminal. This will display all of the routes that are currently configured on your system. U means the route is ‘up’ and the G indicates that it is gateway.

$ route -n
Kernel IP routing table
Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
0.0.0.0     172.31.32.1   0.0.0.0     UG  100  0    0 eth0
172.31.32.0   0.0.0.0     255.255.240.0  U   100  0    0 eth0

If you want to add a new route, you can use the “-add” option. For example, if you wanted to add a route for the 192.168.0.0/24 network, you would type “route add -net 192.168.0.0/24” into the terminal.

Related: Step by Step Guide to troubleshoot a network issue in Linux

Get gateway ip with netstat command in Linux

The netstat command is another way to view all of the active network connections on your system. To get the network gateway, simply type “netstat -rn” into the terminal. The gateway will be displayed under the “Gateway” column.

$ netstat -rn
Kernel IP routing table
Destination   Gateway     Genmask     Flags  MSS Window irtt Iface
0.0.0.0     172.31.32.1   0.0.0.0     UG    0 0     0 eth0
172.31.32.0   0.0.0.0     255.255.240.0  U     0 0     0 eth0

Netstat will display all of the active network connections and their status.

If you want to see more information about a specific connection, you can use the “-a” option. For example, if you wanted to see more information about the connection to 192.168.0.0/24, you would type “netstat -a | grep 192.168.0.0/24” into the terminal.

To learn more about netstat command, I recommend this guide.

6 ways to use netstat command in Linux

How to change the default gateway in Linux?

The command to change the default gateway in Linux is “route”. For example, to add a new default gateway with the IP address 192.168.0.254, you would use the following command: “route add 192.168.0.254”.

What is the difference between a gateway and a router?

A gateway is a device that connects two networks. A router is a type of gateway that is used to connect two or more computer networks.

What is the difference between a default gateway and a static route?

A default gateway is the network router that your computer uses to connect to the Internet. A static route is a route that has been manually configured by a user.

How to add a static route in Linux?

The command to add a static route in Linux is “route”. For example, to add a new route for the network 192.168.0.0/24 with the gateway 192.168.0.254, you would use the following command: “route add -net 192.168.0.0 netmask 255.255.255.0 dev eth0

How to delete a static route in Linux?

The command to delete a static route in Linux is “route”. For example, to delete the route for the network 192.168.0.0/24 with the gateway 192.168.0.254, you would use the following command: “route del -net 192.168.0.0 netmask 255.255.255.0 dev eth0

How to check Ip address in Linux

The ip command is a great way to view all of the information about your network interfaces. If you want to view more information about your network interfaces, you can use the “ip addr” command. This command displays all of the information about your network interfaces, including the IP address, netmask.

$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 655
36 qdisc noqueue state UNKNOWN group default

The “ip link show” command is great way to view MAC address information in Linux about your network interfaces. To view the MAC address info, type “ip link show <interface>” into the terminal.

$ ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether 0a:4f:94:5e:11:02 brd ff:ff:ff:ff:ff:ff

Hopefully, this blog post has helped you learn a few different ways to check your gateway in Linux. If you have any questions or comments, feel free to leave them below! Thanks for reading!

Daniel Li

Saturday 4th of November 2023

use the ip route command. This command displays all of the information about your network interfaces, including the gateway.

use the route command. This command allows you to view and modify the routing table on your system. To get the gateway, simply type “route -n” into the terminal.

use the netstat command. This command shows all active network connections and their status. To get the network gateway, simply type “netstat -rn” into the terminal.