5 ways to use the ip Command when Networking on Linux

Configurare noua (How To)

Situatie

Solutie

See your Network Interfaces

A common use for the ip command is checking what network devices are connected to your Linux system. You can use this information to connect across the network or to make changes them, and more as we’ll see later. For example, type the command ip link show to list all your network interfaces along with other potentially useful information about them.

In my screenshot, you can see I have three network interfaces. The first, lo, is an internal loopback device, a “virtual” network interface used only by my Linux system’s internal processes. The second, wlo1, is the name of the wireless network card inside my laptop. The third is an Ethernet interface connected to my laptop via USB-C port.

Disable and Enable Network Interfaces

Let’s say you’ve identified a network interface, and now you want to stop it from operating. For whatever reason, you don’t want any data passing through it. You can use the ip command to disable it so Linux never uses it.The way you do this is to use the “link set” command, telling ip to set the device to “down.” In this example, I’m using ip to disable my wireless interface called wlo1. Your interface may be named the same or something different, so confirm you’re naming the right interface before using the command.

sudo ip link set wlo1 down

From now on, I won’t be able to use my wlo1 interface, and if I use the ip link show command again, I’ll see it’s status is “down.” Let’s say, though, I changed my mind and I want to use wlo1 again. Easy.

ip link set wlo1 up

As before, there’s no confirmation message that anything happened, which indicates I can consider wlo1 back in operation.

See your Local IP Address

If you want to find out what your IP address relative to your local network is, the ip command is one of the simplest means of doing so. This can be helpful in identifying mapped devices on your network, accessing servers you’re self-hosting, and more.Type ip address or ip addr or simply ip a into your terminal, and you’ll see all of your device’s IP addresses organized by network device. You most likely only care about the non-loopback addresses, though. Note that they’ll be shown with “/24” or something similar affixed to indicate its CIDR class.

If you have multiple physical network interfaces like I do, you’ll see you have multiple local IP addresses. When connecting to my device from another, I can choose which connection method (aka network interface) I prefer by specifying the correct IP address.

IPV4 Only

The ip address command on its own outputs a lot of info, including both your IPV4 address and IPV6. If you only care about IPV4, you can use the -4 flag to filter for just IPV4 addresses.

As you can see, the output is a lot easier to scan for the IP address.

IPV6 Only

Similarly, if you’re only interested in the IPV6 version of your IP address, you can use the -6 flag.

ip -6 a

Add a Local IP Address

You might sometimes need to create an IP address for your network interface, such as to fix conflicts with other devices, or to make it easy for software looking for a specific address to find your device. You can do that with the ip command.In my case, I want to give my Ethernet connection the address 192.168.1.130. I confirmed first, of course, that I didn’t have other devices on my local network with this address; I don’t want to cause any IP address collisions. Then, I’m ready to set the new address with this command:

sudo ip addr add 192.168.1.130/24 dev enx00e04c68143d

Now, when I check my Ethernet device stats with ip link show, I can see there are two different addresses.

Delete an IP Address

Let’s say after adding my new IP address I want to delete the old one. Of course, I should first make sure nothing on my local network relies on that address for important connections and instead is using my new, preferred IP address. Then I simply pass this command:

sudo ip addr del 192.168.1.129/24 dev enx00e04c68143d

From now on, my device will no longer be reachable by the address 192.168.1.129.

Tip solutie

Permanent

Voteaza

(16 din 28 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?