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 (default gateway) is the IP address of your router. It is usually automatically detected by the operating system during installation, but you may need to change it. This is especially true if there are multiple network adapters or routers on the network.

Steps

Part 1 of 2: Using the Terminal

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

Step 1. Start the terminal

Click on it in the sidebar or press 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. Find the address of the active default gateway

To do this, enter route and press ↵ Enter. The default line will display the default gateway address, and the interface assigned to it can be found on the right side of the table.

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

Step 3. Remove the active default gateway

If multiple default gateways are installed, they may conflict. Remove the active default gateway if you intend to change it.

Enter sudo route delete default gw IP address of the Adapter. For example, to remove the default gateway 10.0.2.2 of the eth0 adapter, enter 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. Enter sudo route add default gw IP address of the Adapter

For example, to change the default gateway of the eth0 adapter to 192.168.1.254, enter sudo route add default gw 192.168.1.254 eth0. You will be prompted for a user password to execute the command.

Part 2 of 2: Edit the config 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 in an editor

Enter sudo nano / etc / network / interfaces to open the file in the nano editor. The changes made will be stored until the next reboot of the system.

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

Step 2. Go to the required section

Find the section of the adapter whose default gateway you want to change. The wired adapter is called 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 line gateway IP address to the section

For example, enter gateway 192.168.1.254 to assign the default gateway 192.168.1.254.

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

Step 4. Save your changes

Press Ctrl + X and then press Y to save your changes and exit the text editor.

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

Step 5. Reboot the network

To do this, enter sudo /tc/init.d/networking restart.

Popular by topic