A gateway is a node or a router that acts as an access point to passes network data from local networks to remote networks. There are many ways to find out your gateway in Linux. Here are some of them from Terminal.
You can find default gateway using ip, route and netstat commands in Linux systems.
Using route command
Open up your terminal and type the following commands:
|
mohan@mohan:~$ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth1 192.168.1.0 0.0.0.0 255.255.255.0 U 1 0 0 eth1 |
The above output shows my default gateway is 192.168.1.1. UG stands for the network link is Up and G stands for Gateway.
Using ip route
Use the following command:
|
mohan@mohan:~$ ip route show default via 192.168.1.1 dev eth1 proto static 192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.100 metric 1 |
Using netstat
Use the following command:
|
mohan@mohan:~$ netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 192.168.1.1 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 |
And finally you can view it using the eth config files. If your network interface is eth0, then the command should be:
In RHEL based systems:
|
cat /etc/sysconfig/network-scripts/ifcfg-eth0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
mohan@mohan:~$ cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0" BOOTPROTO=none NM_CONTROLLED="yes" ONBOOT=yes TYPE="Ethernet" UUID="bcb0a409-d7d4-4f2f-882f-ec46e07e670d" HWADDR=08:00:27:A6:0C:AC IPADDR=192.168.1.200 PREFIX=24 GATEWAY=192.168.1.1 DNS1=8.8.8.8 DNS2=192.168.1.200 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System eth0" |
And for Debian based systems, use the following command:
|
cat /etc/network/interfaces |
|
mohan@mohan:~$ cat /etc/network/interfaces # interfaces(5) file used by ifup(8) and ifdown(8) auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 192.168.1.100 netmask 255.255.255.0 network 192.168.1.0 broadcast 192.168.1.255 gateway 192.168.1.1 |
You can add a new default route and remove the old one using either the ip
or route
command. The above commands will replace the gateway with 192.0.2.1. Both command pairs do the same thing.
|
ip route add default via 192.0.2.1 ip route del default via 10.0.0.1 route add default gw 192.0.2.1 route del default gw 10.0.0.1 |
Change 192.0.2.1 to your desired default gateway. The default gateway needs to be on one of networks you have a direct connection to. You can change your IP address in a similar manner.
ip
is a newer tool which will do most everything you need to do to view and manage IP addresses and routing on IPv4 and IPv6 networks.
ifconfig
is an an older tool for configuring IP addresses on an IPv4 network.
To make the change permanent, update your network configuration files in /etc
. The file(s) vary depending on the distribution you are using.
At least one of these commands should be available on any Unix derived O/S. Different versions may work slightly differently. Check the man
page for details on your O/S.