Linux set the default gateway

Linux setup default gateway with route command

[donotprint]

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements None
Est. reading time 5m

[/donotprint]The route command manipulates the kernel’s IP routing tables. Its primary use is to set up static routes to specific hosts or networks via an interface after it has been configured with the ifconfig program. When the add or del options are used, route modifies the routing tables. Without these options, route displays the current contents of the routing tables.

Display default route

Following three-command display the current routing table:
# route
Output:

OR
$ /sbin/route
Output:

You can use -n option, to display numerical addresses instead of trying to determine symbolic host names (via dns or /etc/hosts file). This is useful if you are trying to determine why the route to your nameserver has vanished.
$ /sbin/route -n Output:

Please note that a destination entry 0.0.0.0 (or default) is the default gateway. In above example 191.255.255.1 is a default gateway.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Add / setup a new route

The syntax is as follows:

  • IP-ADDRESS: Specify router IP address
  • INTERFACE-NAME: Specify interface name such as eth0

For example if your router IP address is 192.168.1.254 type the following command as the root user:
# route add default gw 192.168.1.254 eth0
OR use hostname such as dsl-router:
# route add default gw dsl-router eth0
Or use the ip command (newer syntax) to route all traffic via 192.168.1.254 gateway connected via eth0 network interface:
# ip route add 192.168.1.0/24 dev eth0
OR
# ip route add 192.168.1.0/24 via 192.168.1.254

Setting route using GUI tools/command under Linux

If you find above command hard to use, consider using GUI tools. If your are using Red Hat/CentOS/Fedora core Linux type following command: # redhat-config-network OR If you are using other Linux distribution use command: # network-admin

See also

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to set the default gateway on Ubuntu

A default gateway is used for traffic not destined for the local network and for which no particular route is preferred inside the routing table. Similarly, traffic of static routes must not or need not pass via a default gateway. Traditionally, a default gateway serves as the dedicated network router.

What will we cover?

This guide will learn how to set up a default gateway on Ubuntu 20.04 (Focal Fossa) OS using the ‘ip route’ and ‘route’ command. Let us get started with this HowTo.

What are the Prerequisites?

Before you start with this tutorial, look at the following requirements:

  1. Ubuntu 20.04 installed on your system with internet connectivity.
  2. Root user account or user account with ‘sudo’ privileges.
  3. Basic knowledge of computer networks and Ubuntu administration.

Precaution: Please note that this is a very advanced technical guide. Only attempt to follow this guide if you know what you are doing. If you are a novice and end up messing with network configuration, you may lose your connection to the internet.

Experimental Setup

For this guide, we will use IP 192.168.56.4 (enp0s8) as the default gateway address, the IP address of one of the interfaces on our Ubuntu machine (VM1). The other interface has a different IP address which is 192.168.57.5 (enp0s3). Now comes the VM2, which has only one interface with IP address 192.168.56.101 (enp0s3). It is clear that VM2 enp0s3 cannot access VM1 enp0s3 as both are on different network addresses.

Our task is to make both VMs communicate on interfaces with different network addresses. We will set the IP of VM1(192.168.56.4) as a gateway for another machine (VM2), and we will then check by pinging VM1 from VM2 to see if it can reach it the gateway.

VM1 192.168.56.4 (enp0s8) 192.168.56.101 (enp0s3)
VM2 192.168.57.5
(enp0s3)

Finding the Default Gateway IP

Let us first check the default gateway that is already set on our machine, for this run the command:

This will display the default gateway on the terminal.

Alternatively, we can use the ‘route’ command for the same purpose:

If you get an error like ‘Command ‘route’ not found’, then you can install the ‘route’ command with:

Check the UG flag to look for the Gateway. Routing table flags and their meanings are given below:

U : UP— indicates that the route is up and valid
H : Host— target is to a host
G : Gateway—means that the route is to a gateway
R : Reject— It is set by ARP when an entry is expired
D : Dynamic— Route added by a routing redirect
M : Modified— Route modified by a routing redirect

The output of the above two commands is almost identical. Another way is to use the ‘netstat -rn’ command to check the gateway IP.

Setting Default Gateway Using the ‘ip’ Command

First, we will use the ‘ip’ command to set the default gateway on VM2. For this, follow the steps mentioned below:

Step 1. Open a terminal (Ctrl+Alt+F2) and run the command:

Step 2. Now ping the VM1 from VM2 to check if our gateway is accessible:

To delete the above default route, use the command below:

To confirm the above changes, use the “route -n” command.

Setting Default Gateway Using the ‘route’ Command

Now we will use the ‘route’ command to set the default gateway on VM2. For this, follow the steps mentioned below:

Step 1. Open a terminal (Ctrl+Alt+F2) and run the command:

Step 2. Now ping the VM1 from VM2 to check if our gateway is accessible:

To delete the above default route, use the command below:

To confirm the above changes, use the “route -n” command.

Conclusion

In this guide, we have learned about setting the default gateway on Ubuntu 20.04. Try to make different scenarios of devices and connect them using a common gateway.

About the author

Ali Imran Nagori

Ali imran is a technical writer and Linux enthusiast who loves to write about Linux system administration and related technologies. You can connect with him on LinkedIn
.

Источник

Debian / Ubuntu Linux Setting a Default Gateway

Q. How do I setup a default gateway on Debian or Ubuntu Linux? My router IP is 192.168.1.254 and I’d like to setup this as a default gateway for Debian Linux.

A. You can use command line tool such as
a] ip command – show / manipulate routing, devices, policy routing and tunnels

b] route command – show / manipulate the IP routing table

c] Save routing information to a configuration file so that after reboot you get same default gateway.

ip command to set a default router to 192.168.1.254

Login as the root and type:
# ip route add default via 192.168.1.254
OR
$ sudo ip route add default via 192.168.1.254

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

route command to set a default router to 192.168.1.254

Login as the root and type:
# route add default gw 192.168.1.254
OR
$ sudo route add default gw 192.168.1.254

Save routing information to a configuration file /etc/network/interfaces

Open /etc/network/interfaces file
# vi /etc/network/interfaces
OR
$ sudo vi /etc/network/interfaces
Find eth0 or desired network interface and add following option
gateway 192.168.1.254
Save and close the file. Restart networking:
# /etc/init.d/networking restart
OR
$ sudo /etc/init.d/networking restart

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

I also want to set my defualt DNS server and proxy settings, just like in windows. How do I do that ??

In Ubuntu, you can either add the ‘nameserver’ lines in /etc/resolv.conf like “shutty” shows, or use the recommended method in Ubuntu Precise (12.04 LTS) by adding “dns-nameservers ” and “dns-search ” in /etc/network/interfaces, like so:

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.3.1
netmask 255.255.255.0
# dns-* options are implemented by the resolvconf package, if installed

dns-nameservers 8.8.8.8
dns-search lab.internal

for dns, edit file /etc/resolv.conf and add lines
nameserver 1.1.1.1
nameserver 2.2.2.2
(where 1.1.1.1 and 2.2.2.2 are your nameservers)

for proxy, use your browser configuration

How do I add a second gateway on higher metric incase the first goes down?

You need to install routed and configure /etc/gateway. Another recommended option is install zebra / Quagga server and configure it. Both allows you to define and configure various routing parameters including metric.

How can I get (in a variable) the current default gateway? Is there a sort of “default gateway enviroment variable” that I can read?
Thanks

Run following to display default gateway:
ip route show | grep default | awk ‘< print $3>‘
Use following to store gateway IP in a variable called GW:
GW=$(ip route show | grep default | awk ‘< print $3>‘)
echo $GW

vivek, many many many thanks. It works!

For two interfaces that both do dhcp the metrics on the default gateway can be added like this.

auto eth1
iface eth1 inet dhcp
metric 10

auto eth2
iface eth2 inet dhcp
metric 20

Is there any way,so that we can give proxy addresss frm command line apart from browser.because if we give this from command line,it will be applicable to all system.

I have two interfaces, eth0 & eth1. eth0 fixed IP 192.168.1.20 hardwired to test machine with fixed IP 192.168.1.6, (not easily altered).
I want eth1 to connect to internet via a DHCP Server Router. Without eth1 enabled there is no problem but as soon as I enable eth1 as DHCP client I lose any access to 192.168.1.6
Have spent many hours altering the route(s), etc., with no success.
Do I have to use ipchains or can it be achieved with route?

I am having some routing problems with Ubuntu 9.04:
Whenever I start up my system, the Internet won’t work unless I do:
«sudo route add default gw 192.168.1.254»
I tried to alter /etc/network/interfaces, but with no luck. After restart, it doesn’t work. executing route prints:

catalin@lolinux:

$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 * 255.255.255.0 U 0 0 0 wlan0

After I manually add the gateway, everything is fine, and route prints another gateway:

catalin@lolinux:

$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.0 * 255.255.255.0 U 0 0 0 wlan0
default dsldevice.lan 0.0.0.0 UG 0 0 0 wlan0

Is there any difference from what I altered and the kernel routing table? If there is, how do I modify that? Or would a startup script be a better solution?

I have the “gateway 192.168.1.1” in my network file but I’m still required to manually set the route after reboot. This behavior began only after I “bonded” my two NIC’s. The routing table before the manual route command shows
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 bond0
192.168.1.0 0.0.0.0 255.255.255.0 U 1 0 0 eth1
192.168.1.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eth0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth1
0.0.0.0 192.168.1.1 0.0.0.0 UG 100 0 0 bond0
and after adds this one line just below the 169.254… line
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 bond0

Am I missing something or am I experiencing a true bug?

I’m using Kubuntu 9.04 Jaunty.

I am having the same problem..I have to manually set my default gateway after each boot up even though all my network settings include it. How do I get it to stick?

I eventually removed Network Manager. Set everything up manually and it works fine.

I just switched to static ip’s and set everything through wicd..works perfectly. Also I used the network admin tool and I believe that screwed up lots of things.

How can I find Gateway server !? to add the numbers to the address code!?

How can i use 2 gateway , I have a complex network using 2 ISP and Linux as a cache and firewall and a router.

i WANT lINUX router to be able to use both ISP in same time and could nat global IP.

I am using my nanostation 5 as a router… but I can’t receive ping from the ISP gateway… even though I have my ip in the same range. I found this site but the GW isn’t adding… Here are my tables

Destination Gateway Genmask Flags Metric Ref Use Iface
10.64.64.64 * 255.255.255.255 UH 0 0 0 ppp0
10.29.136.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default * 0.0.0.0 U 0 0 0 ppp0

My ip range is 10.29.136.0… I need to add 10.29.136.1 as GW

youtr ip 169.254 its not corect. Reset your router

Hehe
funny thing here! very old Debian distro, I’m trying to add new default gateway via.. tun interface (VPN), but it’s refusing to add anything 😀

ip route
10.10.0.2 dev tun0 proto kernel scope link src 10.10.0.1
10.1.0.0/24 via 10.10.0.2 dev tun0
10.0.0.0/16 dev eth1 proto kernel scope link src 10.0.0.254
192.168.0.0/16 dev eth0 proto kernel scope link src 192.168.33.100
default via 192.168.0.1 dev eth0

ping 10.1.0.1
PING 10.1.0.1 (10.1.0.1) 56(84) bytes of data.
64 bytes from 10.1.0.1: icmp_seq=1 ttl=64 time=0.726 ms
64 bytes from 10.1.0.1: icmp_seq=2 ttl=64 time=0.544 ms
64 bytes from 10.1.0.1: icmp_seq=3 ttl=64 time=1.60 ms
64 bytes from 10.1.0.1: icmp_seq=4 ttl=64 time=1.58 ms

sudo ip route add default via 10.1.0.1
RTNETLINK answers: No such process

sudo route add default gw 10.1.0.1
SIOCADDRT: Nie ma takiego procesu

solved, I’ve used 10.10.0.2 as gateway which is IP alias of VPN

add the following to the bottom of your /etc/network/interfaces file

# Static route
up route add -net xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx dev ethX

I use the following commands to add a new gateway and remove an existing one :

sudo route add default gw 192.168.12.1 eth1
sudo route delete default gw 10.0.2.2 eth0

Источник

Читайте также:  Nforce chipset drivers windows 10
Оцените статью