How to Add or Change the Default Gateway in Linux

Table of contents:

How to Add or Change the Default Gateway in Linux
How to Add or Change the Default Gateway in Linux
Anonim

The "default gateway" of a network connection is the IP address of the router. Normally this parameter is automatically detected by the operating system when configuring the connection, but in some cases it may be necessary to intervene manually. The latter scenario often occurs when there are multiple routers within the LAN.

Steps

Part 1 of 2: Using the Terminal Window

Add or Change the Default Gateway in Linux Step 1
Add or Change the Default Gateway in Linux Step 1

Step 1. Open a "Terminal" window

You can use the Linux Tool Sidebar or you can use the key combination Ctrl + Alt + T.

Add or Change the Default Gateway in Linux Step 2
Add or Change the Default Gateway in Linux Step 2

Step 2. View the IP address of the current network default gateway

You can find this information by entering the route command and pressing the Enter button. The address that appears next to the entry "default" represents the IP address of the default gateway and on the right side of the table is also the name of the network interface to which it has been assigned.

Add or Change the Default Gateway in Linux Step 3
Add or Change the Default Gateway in Linux Step 3

Step 3. Delete the default gateway currently in use

If multiple network gateway addresses are configured you will run into connection problems generated by IP address conflicts. If you need to change the default gateway address, the first step is to delete the current one.

Run the command sudo route delete default gw ip_address network_card. For example, to delete the default gateway 10.0.2.2 from the eth0 network interface, you will need to run the following command sudo route delete default gw 10.0.2.2 eth0

Add or Change the Default Gateway in Linux Step 4
Add or Change the Default Gateway in Linux Step 4

Step 4. Run the command

sudo route add default gw IP_address network_card. For example, to set the eth0 interface network gateway with the IP address 192.168.1.254 you will need to run the following command sudo route add default gw 192.168.1.254 eth0. You will be asked to enter your user account password in order to complete this step.

Part 2 of 2: Edit the Configuration File

Add or Change the Default Gateway in Linux Step 5
Add or Change the Default Gateway in Linux Step 5

Step 1. Open the configuration file using a text editor

Type the command sudo nano / etc / network / interfaces inside the "Terminal" window to perform this step. By modifying the network connection configuration file, the default settings contained within will be used automatically each time the system is restarted.

Add or Change the Default Gateway in Linux Step 6
Add or Change the Default Gateway in Linux Step 6

Step 2. Review the correct data section

You need to locate the section for the network interface whose gateway you want to change. In the case of an Ethernet network card, the interface name is usually eth0.

Add or Change the Default Gateway in Linux Step 7
Add or Change the Default Gateway in Linux Step 7

Step 3. Add the following line

gateway IP_address to the section of the file in question.

For example, if your router's IP address is 192.168.1.254 you will need to enter the following line of gateway text 192.168.1.254 in the file.

Add or Change the Default Gateway in Linux Step 8
Add or Change the Default Gateway in Linux Step 8

Step 4. Save the new changes and close the editor

Press the key combination Ctrl + X then press the Y key on your keyboard to save the file and close the editor.

Recommended: