12 个 ip 命令范例
ifconfig ip
一年又一年，我们一直在使用 `ifconfig` 命令来执行网络相关的任务，比如检查和配置网卡信息。但是 `ifconfig` 已经不再被维护，并且在最近版本的 Linux 中被废除了！ `ifconfig` 命令已经被 `ip` 命令所替代了。


`ip` 命令跟 `ifconfig` 命令有些类似，但要强力的多，它有许多新功能。`ip` 命令完成很多 `ifconfig` 命令无法完成的任务。


![](/data/attachment/album/201801/13/110118j9qcyqmsy8cmk9a3.jpg)


本教程将会讨论 `ip` 命令的 12 中最常用法，让我们开始吧。


### 案例 1：检查网卡信息


检查网卡的诸如 IP 地址，子网等网络信息，使用 `ip addr show` 命令：



```
[linuxtechi@localhost]$ ip addr show

或

[linuxtechi@localhost]$ ip a s

```

这会显示系统中所有可用网卡的相关网络信息，不过如果你想查看某块网卡的信息，则命令为：



```
[linuxtechi@localhost]$ ip addr show enp0s3

```

这里 `enp0s3` 是网卡的名字。


![IP-addr-show-commant-output](/data/attachment/album/201801/13/110151fyxsy3s3sulssphy.jpg)


### 案例 2：启用/禁用网卡


使用 `ip` 命令来启用一个被禁用的网卡：



```
[linuxtechi@localhost]$ sudo ip link set enp0s3 up

```

而要禁用网卡则使用 `down` 触发器：



```
[linuxtechi@localhost]$ sudo ip link set enp0s3 down

```

### 案例 3：为网卡分配 IP 地址以及其他网络信息


要为网卡分配 IP 地址，我们使用下面命令：



```
[linuxtechi@localhost]$ sudo ip addr add 192.168.0.50/255.255.255.0 dev enp0s3

```

也可以使用 `ip` 命令来设置广播地址。默认是没有设置广播地址的，设置广播地址的命令为：



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

```

我们也可以使用下面命令来根据 IP 地址设置标准的广播地址：



```
[linuxtechi@localhost]$  sudo ip addr add 192.168.0.10/24 brd + dev enp0s3

```

如上面例子所示，我们可以使用 `brd` 代替 `broadcast` 来设置广播地址。


### 案例 4：删除网卡中配置的 IP 地址


若想从网卡中删掉某个 IP，使用如下 `ip` 命令：



```
[linuxtechi@localhost]$ sudo ip addr del 192.168.0.10/24 dev enp0s3

```

### 案例 5：为网卡添加别名（假设网卡名为 enp0s3）


添加别名，即为网卡添加不止一个 IP，执行下面命令：



```
[linuxtechi@localhost]$  sudo ip addr add 192.168.0.20/24 dev enp0s3 label enp0s3:1

```

![ip-command-add-alias-linux](/data/attachment/album/201801/13/110157e55drlru5crd4dfa.jpg)


### 案例 6：检查路由/默认网关的信息


查看路由信息会给我们显示数据包到达目的地的路由路径。要查看网络路由信息，执行下面命令：



```
[linuxtechi@localhost]$  ip route show

```

![ip-route-command-output](/data/attachment/album/201801/13/110201uiwbkf5b4irxbex8.jpg)


在上面输出结果中，我们能够看到所有网卡上数据包的路由信息。我们也可以获取特定 IP 的路由信息，方法是：



```
[linuxtechi@localhost]$ sudo ip route get 192.168.0.1

```

### 案例 7：添加静态路由


我们也可以使用 IP 来修改数据包的默认路由。方法是使用 `ip route` 命令：



```
[linuxtechi@localhost]$ sudo ip route add default via 192.168.0.150/24

```

这样所有的网络数据包通过 `192.168.0.150` 来转发，而不是以前的默认路由了。若要修改某个网卡的默认路由，执行：



```
[linuxtechi@localhost]$ sudo ip route add 172.16.32.32 via 192.168.0.150/24 dev enp0s3

```

### 案例 8：删除默认路由


要删除之前设置的默认路由，打开终端然后运行：



```
[linuxtechi@localhost]$  sudo ip route del 192.168.0.150/24

```

**注意：** 用上面方法修改的默认路由只是临时有效的，在系统重启后所有的改动都会丢失。要永久修改路由，需要修改或创建 `route-enp0s3` 文件。将下面这行加入其中：



```
[linuxtechi@localhost]$  sudo vi /etc/sysconfig/network-scripts/route-enp0s3

172.16.32.32 via 192.168.0.150/24 dev enp0s3

```

保存并退出该文件。


若你使用的是基于 Ubuntu 或 debian 的操作系统，则该要修改的文件为 `/etc/network/interfaces`，然后添加 `ip route add 172.16.32.32 via 192.168.0.150/24 dev enp0s3` 这行到文件末尾。


### 案例 9：检查所有的 ARP 记录


ARP，是<ruby> 地址解析协议 <rt>  Address Resolution Protocol </rt></ruby>的缩写，用于将 IP 地址转换为物理地址（也就是 MAC 地址）。所有的 IP 和其对应的 MAC 明细都存储在一张表中，这张表叫做 ARP 缓存。


要查看 ARP 缓存中的记录，即连接到局域网中设备的 MAC 地址，则使用如下 ip 命令：



```
[linuxtechi@localhost]$  ip neigh

```

![ip-neigh-command-linux](/data/attachment/album/201801/13/110207p2mgoul020ou3z4q.jpg)


### 案例 10：修改 ARP 记录


删除 ARP 记录的命令为：



```
[linuxtechi@localhost]$ sudo ip neigh del 192.168.0.106 dev enp0s3

```

若想往 ARP 缓存中添加新记录，则命令为：



```
[linuxtechi@localhost]$ sudo ip neigh add 192.168.0.150 lladdr 33:1g:75:37:r3:84 dev enp0s3 nud perm

```

这里 `nud` 的意思是 “neghbour state”（网络邻居状态），它的值可以是：


* `perm` - 永久有效并且只能被管理员删除
* `noarp` - 记录有效，但在生命周期过期后就允许被删除了
* `stale` - 记录有效，但可能已经过期
* `reachable` - 记录有效，但超时后就失效了


### 案例 11：查看网络统计信息


通过 `ip` 命令还能查看网络的统计信息，比如所有网卡上传输的字节数和报文数，错误或丢弃的报文数等。使用 `ip -s link` 命令来查看：



```
[linuxtechi@localhost]$ ip -s link

```

![ip-s-command-linux](/data/attachment/album/201801/13/110214ncq5qr4b48cvyf2h.jpg)


### 案例 12：获取帮助


若你想查看某个上面例子中没有的选项，那么你可以查看帮助。事实上对任何命令你都可以寻求帮助。要列出 `ip` 命令的所有可选项，执行：



```
[linuxtechi@localhost]$ ip help

```

记住，`ip` 命令是一个对 Linux 系统管理来说特别重要的命令，学习并掌握它能够让配置网络变得容易。本教程就此结束了，若有任何建议欢迎在下面留言框中留言。




---


via: <https://www.linuxtechi.com/ip-command-examples-for-linux-users/>


作者：[Pradeep Kumar](https://www.linuxtechi.com/author/pradeep/) 译者：[lujun9972](https://github.com/lujun9972) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


 


OK
In this blog post, we will cover 16 IP command examples in Linux.

Linux, with its open-source nature, provides users with a vast array of tools to empower their networking endeavors. Among these tools, the IP command stands out as a versatile and powerful utility for managing various aspects of network configuration. [IP command](https://access.redhat.com/sites/default/files/attachments/rh_ip_command_cheatsheet_1214_jcs_print.pdf) can be your key to mastering network configurations on your Linux system.

#### Understanding the ip command

The ip command is a part of the iproute2 package, which is pre-installed on most Linux distributions. It it supersedes the deprecated ifconfig command and serves as a one-stop solution for network configuration, addressing, routing, and more. Whether you’re setting up a new network interface, troubleshooting connectivity issues, or fine-tuning network parameters, the “ip” command is your go-to tool.

Without any further delay, let jump into the real time IP command examples which will help you to master the Linux networking.

## 1) Display Network Interfaces

The first step in Linux networking is to identify your network interfaces. Open the terminal and run following command:

$ ip link show

Output above shows that we have one network interface with name “enp0s3”.

## 2) Display Current Network Information


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

$ ip addr show or $ 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 a particular interface, command is

$ ip addr show enp0s3

where enp0s3 is the name of the interface.

## 3) Configure Network Interfaces

To configure an interface, use the ‘ip addr’ command. For instance, to set a static IP address:

$ sudo ip addr add 192.168.1.50/255.255.255.0 dev enp0s3

Additionally, 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

$ sudo ip addr add broadcast 192.168.1.255 dev enp0s3

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

$ sudo ip addr add 192.168.1.50/24 brd + dev enp0s3

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

## 4) Adding Default Route


To add a default route, run following ip command with route option.

Syntax: $ sudo ip route add default via <Gateway_IP_Address>

Example,

$ sudo ip route add default via 192.168.1.1

## 5) Bring an Interface Up/Down


Use ‘ip link set‘ command to bring an interface up or down:

$ sudo ip link set enp0s3 up

Above command will enable the interface enp0s3.

$ sudo ip link set enp0s3 down

Command above will bring down or disable the interface enp0s3

## 6) Remove IP Address From an Interface

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

$ sudo ip addr del <IP-Address> dev <Interface-Name>

$ sudo ip addr del 192.168.1.50/24 dev enp0s3

## 7) Adding an Alias for an Interface


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

$ sudo ip addr add 192.168.1.120/24 dev enp0s3 label enp0s3:1

## 8) Display Routing Table

View your routing table with ‘ip route show’. Route table and default gateway information shows us the route a packet will take to reach the destination.

$ ip route show

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,

$ sudo ip route get 192.168.1.1 192.168.1.1 dev enp0s3 src 192.168.1.4 uid 1000 $

## 9) Adding a Static Route


To add a static route, specify the destination network and gateway, example is shown below:

$ sudo ip route add 172.16.32.32 via 192.168.1.150/24 dev enp0s3

## 10) Remove a Static Route


Remove a route using ‘ip route del’:

$ sudo ip route del 192.168.1.150/24

## 11) List 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.

Use ‘ip neigh‘ to list the entries in ARP cache i.e. MAC addresses of the devices connected in LAN.

$ ip neigh

## 12) Modifying ARP entries


To delete an ARP entry, the command used is

$ sudo ip neigh del 192.168.1.177 dev enp0s3

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

$ sudo ip neigh add 192.168.1.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.

## 13) 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 link

## 14) Create a VLAN Interface

Create a VLAN interface using ‘ip link add’:

$ ip link add link enp0s3 name enp0s3.110 type vlan id 110

## 15) Set MTU Size of an Interface

To modify the MTU (Maximum Transmission Unit) size of an interface, run

$ ip link set enp0s3 mtu 1700

## 16) How to get help

If you want to find an 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

$ ip --help

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.

Also Read : [How to Automate tasks in Linux using Crontab](https://www.linuxtechi.com/schedule-automate-tasks-linux-cron-jobs/)

drbThe commentary for Example 7 gives the gateway as 192.168.1.150

Shouldn’t that be 192.168.0.150?

Pradeep KumarIt was a typo, i have corrected it now., thank you very much for pointing this error.

David JohasonThank 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.

LesYour 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.