Using ip commands

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
4,408
Reaction score
4,601
Credits
41,638
While the ip commands aren't nearly as full featured as nmcli (network manager ) there is still a lot you can do.

IP stands for internet protocol. This has to do with network commands. The "ip" command does not directly support wi-fi connections. This is for ethernet connections.

How to view your current IP address and settings.
Code:
ip addr show

How to bring an interface up.
Code:
ip link set dev eth0 up

Note: your interface name won't always be "eth0". Just replace eth0 with your interface name. You can see your interface
name with the "ip addr" command above.

How to shut an interface down.
Code:
ip link set dev eno1 down

Again, your interface name might not be eno1.

How to add a static IP address to your interface.
Code:
ip addr add 192.168.1.100/24 dev eth0

As mentioned before, your interface may not be eth0. You will need the netmask, most of the time it will be /24 which is the same as 255.255.255.0. Note, it is possible to assign more than one IP address to an interface. (This is not recommended, unless you know
what you are doing, it can break things).

How to delete an IP address from an interface.
Code:
ip addr del 192.168.1.100/24 dev eth0

Again, you will need to include the netmask.

How to view your default route (default gateway)
Code:
ip route show

How to add a default route (default gateway)
Code:
ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0

How to delete a route
Code:
ip route del 192.168.2.0/24

The ip command doesn't have a direct dns command, but you can do something like...
Code:
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf
echo "nameserver 8.8.4.4" | sudo tee -a /etc/resolv.conf

That will add two DNS servers.

There is also not a direct dhcp client setting for the ip command. But you can do this with another command.
Code:
dhclient eth0

To release a dhcp lease, and add a new one.
Code:
dhclient -r eth0
dhclient eth0

That's the basic commands. I will do more advanced commands in another post.
 
Last edited:


Most of this won't apply to most home user/hobbyists. This is mostly for servers in data center, but some of it could apply
if you're a true nerd/geek and you're running this kind of thing at your house. You will need a managed switch/router with some
special configurations for some of these.

How to setup interface bonding/teaming with "ip". You will need two interfaces to do this.
Code:
sudo modprobe bonding

sudo ip link add bond0 type bond

sudo ip link set eth0 master bond0
sudo ip link set eth1 master bond0

Again, as before your interfaces might not be named eth0 and eth1.
Code:
sudo ip link set bond0 up

How to add a VPN with a VLAN.
Code:
sudo ip link set dev tun0 up

Code:
sudo ip addr add 127.0.0.2/8 dev lo

How to create the VLAN interfaces.
Code:
sudo ip link add link eth0 name eth0.10 type vlan id 10
sudo ip link set eth0.10 up

How to create a GRE tunnel.
Code:
sudo ip tunnel add gre1 mode gre remote 192.168.1.1 local 192.168.1.2 ttl 255
sudo ip link set gre1 up

So as you see, while you can't do everything you can do in networkManager (nmcli), you can still do quite a bit.
 
The ip command doesn't have a direct dns command, but you can do something like...
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf echo "nameserver 8.8.4.4" | sudo tee -a /etc/resolv.conf
That will add two DNS servers.
This will not work if NetworkManager has IP already set in UI or trough nmcli because it will overwrite the file when restarted.
 
This will not work if NetworkManager has IP already set in UI or trough nmcli because it will overwrite the file when restarted.

True, which is another reason, I like to just use NetworkManager. But you do have a few options.

If you're using NetworkManager, you can edit /etc/NetworkManager/NetworkManager.conf
Code:
[main]
dns=none
rc-manager=unmanaged

Then restart NetworkManager
Code:
sudo systemctl restart NetworkManager

Another option, if you're using NetworkManager... (this pretty much bypasses "ip").
Code:
sudo nmcli connection modify <connection_name> ipv4.ignore-auto-dns yes
sudo nmcli connection modify <connection_name> ipv4.dns "8.8.8.8 8.8.4.4"

But NetworkManager isn't the only culprit here. If you're using systemd-resolved, you can edit /etc/systemd/resolved.conf
Code:
[Resolve]
DNS=8.8.8.8 8.8.4.4

Then restart
Code:
sudo systemctl restart systemd-resolved

The other option for systemd-resolved is to create a drop-in config file.
Code:
sudo mkdir -p /etc/systemd/resolved.conf.d
sudo vim /etc/systemd/resolved.conf.d/dns_servers.conf

In your dns_server.conf, all you need are the following lines.
Code:
[Resolve]
DNS=8.8.8.8 8.8.4.4

If you don't want to do any of that, there is yet another option... (I have done this, but I don't recommend it).
You can make the /etc/resolv.conf immutable (unchangeable).
Code:
sudo chattr +i /etc/resolv.conf

But that sometimes can break other things. So, if it does, you can remove it...
Code:
sudo chattr -i /etc/resolv.conf

But honestly, given all the hassles above... I just use NetworkManager.
 


Follow Linux.org

Members online


Top