Linux ip command set gateway

12 IP Command Examples for Linux Users

For years & years we have been using ‘ifconfig‘ command to perform network related tasks like checking network interfaces or configuring them. But ‘ifconfig’ is no longer being maintained & has been deprecated on the recent versions of Linux. ‘ifconfig’ command has been replaced with ‘ip‘ command.

Linux ip command somewhat similar to ‘ifconfig’ command but it’s much more powerful with much more functionalities attached to it. ‘ip’ command is able to perform several tasks which were not possible to perform with ‘ifconfig’ command.

In this tutorial, we are going to discuss 12 ‘ip’ command examples in linux, so let’s get going,

Example 1) View Current network information for interfaces ( LAN Cards )

To display IP address and Subnet etc for the interfaces, use ‘ ip addr show ‘ command

[linux[email protected]]$ ip addr show or [[email protected]]$ ip a s

This will show network information related to all interfaces available on our system, but if we want to view same information for single interface, command is

where enp0s3 is the name of the interface.

IP-addr-show-commant-output

Example 2) Enabling & disabling network interface (or LAN card)

We can enable or disable network interface (enp0s3) with ip command, example is shown below:

[[email protected]]$ sudo ip link set enp0s3 up

Above command enable the interface enp0s3 and to disable the network interface we will use ‘down’ trigger,

[[email protected]]$ sudo ip link set enp0s3 down

Example 3) Assign IP address & broadcast address to an interface

To assign IP address to interface with ip command , we will use below:

[[email protected]]$ sudo ip addr add 192.168.0.50/255.255.255.0 dev enp0s3

We can also set broadcast address to interface with ‘ip’ command. By default no broadcast address is set, so to set a broadcast address command is

[linuxtechi@localhost]$ sudo ip addr add broadcast 192.168.0.255 dev enp0s3

We can also set standard broadcast address along with IP address by using the following command,

[linuxtec[email protected]]$ sudo ip addr add 192.168.0.10/24 brd + dev enp0s3

As shown in the above example, we can also use ‘brd’ in place on ‘broadcast’ to set broadcast ip address.

Example 4) Remove IP address from the interface

If we want to flush or remove the assigned IP from the interface, then run beneath ip command

[li[email protected]]$ sudo ip addr del 192.168.0.10/24 dev enp0s3

Example 5) Adding an Alias for an interface (enp0s3)

To add an alias i.e. assign more than one IP to an interface, execute below command

[[email protected]]$ sudo ip addr add 192.168.0.20/24 dev enp0s3 label enp0s3:1

ip-command-add-alias-linux

Example 6) View route table and default gateway information

Checking route table and default gateway information shows us the route a packet will take to reach the destination. To check the network routing information, execute the following ip command,

Читайте также:  Linux загрузка сетевого интерфейса

ip-route-command-output

In the above output we will see the routing information for packets for all the network interfaces. We can also get the routing information to a particular ip using,

[[email protected]]$ sudo ip route get 192.168.0.1

Example 7) Adding a static route with ip command

If we want to change the default route taken by packets, we can do so with IP command by adding a static route.

To assign a new default gateway, use following ‘ip route’ command

[linux[email protected]]$ sudo ip route add default via 192.168.0.150/24

So now all network packets will travel via 192.168.0.150 as opposed to old default route. For changing the default route for a single interface & to make change route further, execute

[[email protected]]$ sudo ip route add 172.16.32.32 via 192.168.0.150/24 dev enp0s3

Example 8) Delete static route

To delete or remove the previously added default route, run ‘ip route del’ command

[[email protected]]$ sudo ip route del 192.168.0.150/24

Note:- Changes made to default route using the above mentioned commands are only temporary & all changes will be lost after a system has been restarted. To make a persistence route change, we need to modify / create route-enp0s3 file . Add the following line to it, demonstration is shown below

[[email protected]]$ sudo vi /etc/sysconfig/network-scripts/route-enp0s3 172.16.32.32 via 192.168.0.150/24 dev enp0s3

If you are using Ubuntu or Debian based OS, then the location of the file is ‘/etc/network/interfaces‘ and add the line “ ip route add 172.16.32.32 via 192.168.0.150/24 dev enp0s3 ” to the bottom of the file.

Example 9) View all ARP entries (ip neigh)

ARP, short for ‘Address Resolution Protocol‘ , is used to convert an IP address to physical address (also known as MAC address) & all the IP and their corresponding MAC details are stored in a table known as ARP cache.

To view entries in ARP cache i.e. MAC addresses of the devices connected in LAN, the IP command used is

ip-neigh-command-linux

To display entries of arp table run ,

Example 10) Modifying ARP entries

To delete an ARP entry, the command used is

[l[email protected]]$ sudo ip neigh del 192.168.0.106 dev enp0s3

or if we want to add a new entry to ARP cache, the command is

[[email protected]]$ sudo ip neigh add 192.168.0.150 lladdr 33:1g:75:37:r3:84 dev enp0s3 nud perm

where nud means neighbour state, it can be

  • perm – permanent & can only be removed by administrator,
  • noarp – entry is valid but can be removed after lifetime expires,
  • stale – entry is valid but suspicious,
  • reachable – entry is valid until timeout expires.

Example 11) View network statistics

With ‘ip’ command we can also view the network statistics like bytes and packets transferred, errors or dropped packets etc for all the network interfaces. To view network statistics, use ‘ip -s link‘ command

ip-s-command-linux

Example 12) How to get help

If you want to find a option which is not listed in above examples, then you can look for help. In Fact you can use help for all the commands. To list all available options that can be used with ‘ip’ command, use

Remember that ‘ip’ command is very important command for Linux admins and it should be learned and mastered to configure network with ease. That’s it for now, please do provide your suggestions & leave your queries in the comment box below.

Читайте также:  Phaser 3020 linux drivers

4 thoughts on “12 IP Command Examples for Linux Users”

The commentary for Example 7 gives the gateway as 192.168.1.150
Shouldn’t that be 192.168.0.150? Reply

Thank you. Changing over from ifconfig. ip much more powerful. i like it. ip neigh is good too. there much for to learn on ip. i think it even loops and was made for scripting. Reply

Your example of adding alias IP addresses is only a temporary fix. It does not write anything to the /etc/sysconfig/network-scripts directory. But your method DOES work better than anything I have tried to add to a network-script, the only problem is your method won’t survive a reboot. My method of adding it to the network-scripts creates duplicate routes.. Would love to see how you can get your method to save and survive a reboot with the same results. Reply

Leave a Comment Cancel reply

Recent Posts

WP-Subscribe-MailChimp

  • How to Install PHP 8 on RHEL 9 | Rocky Linux 9 | AlmaLinux 9
  • How to Install PHP 8 on RHEL 8 / Rocky Linux 8 / CentOS 8
  • How to Install and Use Wireshark in Ubuntu 22.04
  • Top 10 Things to Do After Installing Debian 12 (Bookworm)
  • How to Install Debian 12 (Bookworm) Step-by-Step
  • How to Upgrade Debian 11 to Debian 12 (Bookworm) via CLI
  • How to Setup Dynamic NFS Provisioning in Kubernetes Cluster
  • How to Install Nagios on Rocky Linux 9 / Alma Linux 9
  • How to Install Ansible AWX on Kubernetes Cluster
  • How to Install Docker on Fedora 38/37 Step-by-Step

Pages

Источник

How to Set the Default Gateway on Ubuntu

All the devices on your network rely on the default gateway for communication. Data packets pass through the router to and from your network before being routed to the particular device that owns the packet.

Each operating system comes with a default gateway. However, you can temporarily or permanently change the default gateway to add another route for your network devices. You can use the IP command on Ubuntu to modify your default gateway.

Checking the Default Gateway

Changing the default gateway is common when you have different sub-networks or when you must point a specific machine to a particular gateway. Before changing the default gateway, let’s list the available routes.

Use the list option with the IP command or its shorthand r to stand for the route.

The default gateway has the default keyword in it. If you configured multiple routes on your network, you can use the grep command to filter the router and get the default gateway.

Use the following command:

The current default gateway is 192.168.88.1 on enp0s3 interface. Let’s proceed to set a new default gateway.

How to Set a New Default Gateway

The ip command uses the route option to set the new default gateway. You must specify the type of route that you want to add. In our case, it’s “default”.

For instance, let’s set the default gateway as 192.168.88.10.

Suppose we want to set the default gateway for a particular network interface. In that case, specify the network interface after the gateway. In our case, the interface is enp0s3.

Note that we must add sudo to use the administrator privileges since we are editing the routing table for Ubuntu which is an administrative task.

We can use the list or route options to verify the newly added default gateway.

Note how the currently added default gateway is the one that we specified earlier. In the previous output, we now have two default gateways. The keynote is that the changes we made are temporary until you add them to the network manager configuration files.

Читайте также:  Исправить ошибки линукс минт

In the previous case, we can delete the added gateway such that we remain with only one.

Use the delete keyword to remove the added gateway.

If we check the available default gateway after running the delete command, we confirm that we only have one default gateway remaining which is 192.168.88.1.

Suppose we want to make the permanent network changes to implement the new gateway. We must edit the configuration file. Open the network manager file using a file editor of your choice. In this case, let’s use gedit with the following command:

Add the new gateway using the following presented format. Make sure that the spacing is set to two whitespaces with the correct indention. Once edited, save the file and exit the editor.

Before applying the changes using the netplan command, run a dry test.

If you are sure with the new network configuration, press the enter key. Otherwise, the changes will revert to the previous settings after the specified seconds.

Conclusion

Ubuntu comes with a default gateway, but that doesn’t mean that you can’t set a new gateway. This guide covered everything about adding a new gateway using the ip command and how to save the changes permanently to the configuration file.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.

Источник

How to set the Default gateway

You can use route like in route add default gw 192.168.0.254 for example.

And if route is not present, but ip is, you can use it like this: ip route add default via 192.168.0.254 dev eth0 , assuming that 192.168.0.254 is the ip of your gateway

ifconfig is deprecated on Linux and furthermore, it’s the wrong tool for the job. To set the default gateway on Linux use the ip command as follows:

ip route add default via dev # e.g. ip route add default via 192.168.0.101 dev eth0 

For remove gateway in Linux Command : route delete default gw 192.168.1.1 eth1

For add gateway in Linux Command : route add default gw 192.168.1.250 eth1

example: route add default gw 192.168.1.2 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 for example:

ip route add 192.168.1.0/24 dev eth0 
ip route add 192.168.1.0/24 via 192.168.1.254 

You must log in to answer this question.

Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Оцените статью
Adblock
detector