Categories
Garden-Projects

How to add or change the default gateway in linux

A default gateway is what linux will use to interact with other computers connected on the network. This is typically the router and will send all packets to that IP address. The actions below all use the route command, to find out more possiblities view the route man page.

View the Current Routing Table

To view the current routing table on a linux server you can do the following:

You can see that the default route is the first line sending all packets to the 192.168.1.1 Gateway.

Remove A Default Gateway

To remove a default route you will need to use the following command, substituting 192.168.1.1 with the IP address of the default route you previously saw

Once you have removed it confirm by doing another route -n

Add A Default Gateway

To add a new default route you will need to use the following:

Once you have added the new route you will see it appear in the routing table:

Permanently Configure The Default Gateway

To make the new default gateway persist through a reboot, you will need to edit the network interface file:

Substitute the name of the file with the interface you are setting the route for.

Update or append the following line the file switching out 192.168.1.1 with the IP address of the default router:

Once you have saved the file, you can restart networking to ensure it is working properly

Hi everyone, great forum, I can see this question has been discusse alot and I have tried all the options provided but I am still denied access to the Internet on my relatively new Acer Aspire 5742 laptop. I am no pc tech and find it all very difficult and would love to be able to correct the problem myself as I cant afford to take it to a repairer. We have had no problem with access on our PC and the laptop worked fine originally and has now all of a sudden it says the laptop is connected to the internet but no access. I have tried as outlined in the forum – disabling ‘Bonjour’, troubleshooting, disabling and renabling the network adapters (which are Broadcom 802.11n Network Adapter and Broadcom Netlink (TM) Gigabit Ethernet and both working properly), automatic startup for dhcp & wired auto config.

The wireless network connection – unidentified network

The local area connection – network cable unplugged .

Does anyone have any suggestions as Im sure it is something simple but Im just too dumb with computers :))

  • Subscribe
  • Subscribe to RSS feed

Report abuse

Try the steps listed below:

Step 1 : Manually assign the default gateway and the IP address and check if it works fine. Before assigning the IP address and the Default gateway manually, make
a note of the IP address on the computer. To do this open Command prompt and typeipconfig and press enter. This will display the IP address, make a note of it.

To assign the IP address and default gateway manually, follow the steps below:
a. In system tray, right click network icon. Choose Open Network and Sharing Center.
b. Right click on Local Area Connection and click on Properties. c. Double click Internet Protocol Version 4 (TCP/IPv4) and assign the IP address manually.
Example: Old Ip address is 192.168.0.1
Change the IP address to 192.168.0.50
d. Let the Subnet mask and default gateway be the same.
Subnet mask = 255.255.255.0
Default gateway, usually would be 192.168.0.1 or 192.168.1.1
e. Click on Apply and Ok and restart the computer and verify if it helps to fix the issue.

Step 2: If disabling and re-enabling Network adapter did not fix the issue, then I would suggest you to completely uninstall and reinstall network adapters.

Follow these steps:
a. Click on Start
b. In the start search box type ‘devmgmt.msc’ and press enter
c. In the Device Manager, expand Network Adapters
d. Right click on the installed network adapter and uninstall it.
e. Restart the computer and reinstall the network adapter.

While uninstalling the device drivers make sure you place a check mark against “Delete the driver software for this device”.

Step 3: Temporarily disable the Firewall on your machine and check if it makes any difference.

Step 4: Reset WINSOCK, IPv4 TCP/IP, and IPv6 TCP/IP stack.
a. Click on Start and in the search box, type cmd.
b. Right click on the Command Prompt, select “Run as Administrator” to open a Command Prompt.
c. At the command prompt window, type the following commands:
netsh winsock reset catalog that resets WINSOCK entries to installation defaults.
netsh int ipv4 reset reset.log that resets IPv4 TCP/IP stack to installation defaults.
netsh int ipv6 reset reset.log resets IPv6 TCP/IP stack to installation defaults.

d. Restart the computer.

Also, refer these articles:

How to reset Internet Protocol (TCP/IP)
http://support.microsoft.com/kb/299357
Why can’t I connect to the Internet?

Regards,
Afzal Taher – Microsoft Support.
Visit our Microsoft Answers Feedback Forum and let us know what you think.

Systems connected to the network will generally access to the internet. In order to access to the internet they need some network configuration like gateway or default gateway. In this tutorial we will examine how to add or change default gateway in Ubuntu, Debian, CentOS, Fedora, Mint, Kali operating systems.

Routing table is used to route IP network communication. Hosts generally uses default route to send packages which will redirect them accordingly to transmit destination. We will start by listing current routing table. We will use ip route show command like below.

List Routing Table

Our default gateway line is

  • default means this line is default gateway
  • via 192.168.122.1 specifies next hop which is default gateway IP address
  • dev ens3 is the interface we want use to access default gateway

Removing default gateway is easy if we list routing table because routing table line is used with del command like below. But keep in mind if you are connecting system remotely from different network which means if you are using default route you connection will be lost.

  • ip route del is our key line which deletes specified default gateway
  • default via 192.168.122.1 dev ens3 is the same as routing table

As stated previously default gateway is used to send packages in order to transmit to the destination. We can add new default gateway with the ip route add command like below.

  • ip route add will add provided default gateway
  • default means target network is all which is default
  • via 192.168.1.1 is our default gateway network address
  • dev ens3 is network interface for default gateway

List routing table again and ping some of remote networks will give the status of default gateway

How to add or change the default gateway in linux

How To Add or Change Default Route or Default Gateway in Ubuntu, Linux? Infografic

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:

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

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:

How to add or change the default gateway in linux

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:

How to add or change the default gateway in linux

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:

How to add or change the default gateway in linux

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

How to add or change the default gateway in linux

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
.

I’ve covered how devices communicate using IP when the devices are on the same subnet. But what happens if they are on different networks? This is where the default gateway comes into play. As described previously, the source device and destination device are either on the same subnet or they’re on different subnets. In this example, let’s change our source IP address, while keeping the destination address the same. So now our source and destination address and subnet masks are:

192.168.99.15 255.255.255.0

192.168.40.39 255.255.255.0

Because we are still using /24 subnet masks, we know these two devices are in different subnets since the third octet for each is different. And because subnets are used to break up broadcasts, using the broadcast mechanism with an ARP table will not work in this situation. This is why we need a default gateway.

The default gateway is used as the destination of all traffic that is not on the same subnet. The gateway is a layer 3 device such as a router or multi-layer switch that is used to route traffic on a hop-by-hop basis. But for the purposes of this discussion, the only thing the end device needs to know is whether the data is on the same subnet. If it’s not, the source device delivers traffic to the end device through the default gateway.

The default gateway always resides in the same subnet as the end device IP. The gateway can really be any unique address within the subnet itself, but most network administrators designate the first number of the subnet as the gateway. Therefore, 192.168.99.1 would be the default gateway of our source device given the fact that we have a 255.255.255.0 subnet mask.

The default gateway is the IP address of the router. Generally speaking, the operating system will automatically detect the gateway during the router installation, but you may need to change it manually. Especially when there are multiple network adapters or routers in the network, it is often necessary to manually add or change the gateway. How to modify it? Let’s take a look at the detailed tutorial.

1、 Use terminal

1. Open the terminal. You can open the terminal program from the sidebar, or press Ctrl + Alt + T to open it.

How to add or change the default gateway in linux

2. Browse the current default gateway information. You can type route and press enter to view the current default gateway settings. The address next to “default” displays the current default gateway address, and the interface to which it is assigned is displayed on the right side of the desktop.

How to add or change the default gateway in linux

3. Delete the current default gateway. If you have multiple default gateways, you will encounter connection conflicts. If you want to change it, delete the existing default gateway. Enter sudo route delete default gw IP address adapter. For example, if you want to delete the default gateway 10.0.2.2 on the eth0 adapter, enter sudo route delete default gw 10.0.2.2 eth0.

How to add or change the default gateway in linux

4. Enter. Sudo route add default gw IP address adapter. For example, if you want to change the default gateway of the eth0 adapter to 192.168.1.254, you can type sudo route add default gw 192.168.1.254 eth0. You need to enter the user password to complete the command.

How to add or change the default gateway in linux

2、 Edit profile

1. Open the configuration file in the editor. Enter sudo nano / etc / network / interfaces in the nano editor to open the file. Editing the configuration file allows the system to keep your changes every time you restart the system.

How to add or change the default gateway in linux

2. Find the right part. Find the adapter section where you need to change the default gateway settings. For wired network connections, it is usually eth0.

How to add or change the default gateway in linux

3. Add the. Gateway IP address to this section. For example, enter gateway 192.168.1.254 to change the default gateway to 192.168.1.254.

How to add or change the default gateway in linux

4. Save your changes and exit. Press Ctrl + X, then y to save the changes and exit.

How to add or change the default gateway in linux

5. Restart the network. Type sudo / etc / init.d/networking restart to restart the network.

How to add or change the default gateway in linux

The above is the Linux system to change the default gateway tutorial, I hope you like, please continue to pay attention to developeppaer.

Currently I’m running a FreeBSD 9.1 and the default gateway is already configured in the rc.conf .

But now I want to change the default gateway without rebooting the system, is this possible?

How to add or change the default gateway in linux

7 Answers 7

Where 1.2.3.4 is the new gateway. You can even concatenate them onto the same line with a ;

Edit: This is FreeBSD, not Linux. The command is different. Please do not edit this Answer if you haven’t read the Question carefully enough to determine the operating system being used.

You can add a new default route and remove the old one using either the ip or route command. The commands below will replace the gateway with 192.0.2.1. Both command pairs do the same thing. FreeBSD and other OSs should have one or both programs, possibly with slightly different formats. (FreeBSD has the route command and excludes the gw keyword used in other implementations.) The commands man ip and/or man route should provide you with documentation on your specific implementation.

There are multiple implementations of these commands, so the above may not match your implementation. Your implementation should have a man page with examples for common use cases such as adding and removing default gateways. Try man route and man ip to see how your implementation works.

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.

Your default gateway is the IP address of your router, automatically detected by your OS. But sometimes you may need to set it manually. You can easily do this using route command that allows you to display or change the IP routing table. It is mainly created to setup static IP addresses for your system, hosts, or networks. In this article, we will look at how to set default gateway in Linux.

How to Set Default Gateway in Linux

Here is the syntax to use route command

If you simply run route command without any options, it will display the current routing table.

In case of above output, the row with Destination as default is for default gateway. In this case, the default Gateway value is dsl-router.

If you use -n option, you will see the output as IP addresses, without displaying their host names using DNS lookup.

In case of above output, the Destination with IP address 0.0.0.0 is the row for default gateway, with IP address 192.255.255.1.

Add Gateway

Here is the command to add a new gateway.

In the above command,

IP-ADDRESS is the IP address of router

INTERFACE-NAME is the name of network interface such as eth0.

If your router IP address is 192.34.45.56 then run the following command to set it as default gateway.

You may also set default gateway using interface name.

You may also use the following command to set default gateway, just update the IP address below with that of your router.

If you find that your default gateways it not changing even after the above commands, then try deleting your existing default gateway first using the following command, and then run the above commands to set it afresh. Specify the IP address of your present default gateway below in place of 192.255.255.0.

That’s it. You may also use GUI tools like network-admin to set default gateway in Linux.

IP route command is an extension of IP command, we have already discussed IP commands in brief in our previous tutorial. IP route command is used to add, delete or modify the Linux system’s routing table.

We basically use the IP route command to create static routes in Linux to specific hosts or to change the default gateway for the Linux system. In this tutorial, we will discuss how we can use IP route command to create a static route & to change the default gateway for the Linux system.

Let’s discuss some examples for the same.

IP ROUTE Command

1- Check the current routing table

To check the current routing table of the system, we can use the following commands,

# route

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

10.10.1.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3

# route -n

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

10.10.1.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3

0.0.0.0 10.10.1.10 0.0.0.0 UG 0 0 0 enp0s3

We can also use the following command to check the routes,

# ip route show

The assigned system IP for the system is 10.10.1.100 & we can see that the destination is within that range only in the first command output. The second command also shows the gateway for the IP range i.e. 10.10.1.10.

The routing files are located in the folder “/etc/sysconfig/network-scripts/”,

# cat /etc/sysconfig/network-scripts/route-enp0s3

10.10.1.0/24 via 10.10.1.10 dev enp0s3

2- Create a new route i.e. create a new static route

To create a new static route to a new network range, the command will be

# ip route add 10.10.3.0/24 dev en0sp3

And we want this network to route from a particular gateway, the command is

# ip route add 10.10.3.0/24 via 10.10.1.10 dev en0sp3

3- Delete a route

To delete a route command is,

# ip route delete 10.10.3.0//24 dev en0sp3

4- Delete the existing default gateway

To delete the existing default gateway for the system, the commands are

# ip route delete default

Same can be accomplished with route command,

# route delete default

5- Add a new default gateway

To setup a new system as the default gateway, we need to use the following command,

# route add default gw 10.10.1.20

or we can also use the following command,

# ip route add default via 10.10.1.20

6- To reject network packets to a particular host or to a network

We can also use the IP route command to reject network traffic to a particular host or even to complete the network range.

To reject network packets to a single host, the command is,

# route add -host 10.10.2.20 reject

To reject the network packets to an entire network, we can use the following command,

# route add -net 10.10.2.0 netmask 255.0.0.0 reject

We have only discussed some of the important examples of use for managing the routes on our system. Please feel free to send in any questions or queries using the comment box below.

If you think we have helped you or just want to support us, please consider these:-