How to add or change the default gateway in Linux

Author: Carl Weaver
Date Of Creation: 26 February 2021
Update Date: 1 July 2024
Anonim
Change the default gateway in linux
Video: Change the default gateway in linux

Content

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

  1. 1 Start a terminal. Click on it in the sidebar or click Ctrl+Alt+T.
  2. 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.
  3. 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 addressAdapter... For example, to remove the default gateway 10.0.2.2 adapter eth0, enter sudo route delete default gw 10.0.2.2 eth0.
  4. 4 Enter sudo route add default gw IP addressAdapter. For example, to change the default gateway of an adapter eth0 at 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

  1. 1 Open the config file in an editor. Enter sudo nano / etc / network / interfacesto open the file in nano editor. The changes made will be stored until the next reboot of the system.
  2. 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.
  3. 3 In the section add the line gateway IP address. For example, enter gateway 192.168.1.254to assign 192.168.1.254 to the default gateway.
  4. 4 Save your changes. Click on Ctrl+Xand then press Yto save your changes and exit the text editor.
  5. 5 Reboot the network. To do this, enter sudo /tc/init.d/networking restart.