首頁 系統教程 Linux iproute 安裝套件中 12個 ip 常用指令

iproute 安裝套件中 12個 ip 常用指令

Apr 03, 2024 pm 01:52 PM
linux linux教程 紅帽 linux系統 linux指令 linux認證 紅帽linux linux視頻 子網

Year after year, we have been using the ifconfig command to perform network-related tasks, such as checking and configuring network card information. But ifconfig is no longer maintained and has been deprecated in recent versions of Linux! The ifconfig command has been replaced by the ip command.

The

ip command is somewhat similar to the ifconfig command, but it is much more powerful and has many new functions. The ip command completes many tasks that the ifconfig command cannot.

iproute 安装包中 12个  ip 常用命令

This tutorial will discuss the 12 most common uses of the ip command, let’s get started.

Case 1: Check network card information

Check the network information such as IP address, subnet and other network card, use ip addr show command:

[linuxtechi@localhost]$ ip addr show

或

[linuxtechi@localhost]$ ip a s
登入後複製

This will display the relevant network information of all available network cards in the system, but if you want to view the information of a certain network card, the command is:

[linuxtechi@localhost]$ ip addr show enp0s3
登入後複製

Here enp0s3 is the name of the network card.

iproute 安装包中 12个  ip 常用命令

IP-addr-show-commant-output

Case 2: Enable/disable network card

Use the ip command to enable a disabled network card:

[linuxtechi@localhost]$ sudo ip link set enp0s3 up
登入後複製

To disable the network card, use the down trigger:

[linuxtechi@localhost]$ sudo ip link set enp0s3 down
登入後複製
Case 3: Assign IP address and other network information to the network card

To assign an IP address to the network card, we use the following command:

[linuxtechi@localhost]$ sudo ip addr add 192.168.0.50/255.255.255.0 dev enp0s3
登入後複製

You can also use the ip command to set the broadcast address. By default, the broadcast address is not set. The command to set the broadcast address is:

[linuxtechi@localhost]$ sudo  ip addr add broadcast 192.168.0.255 dev enp0s3
登入後複製

We can also use the following command to set the standard broadcast address based on the IP address:

[linuxtechi@localhost]$  sudo ip addr add 192.168.0.10/24 brd + dev enp0s3
登入後複製

As shown in the above example, we can use brd instead of broadcast to set the broadcast address.

Case 4: Delete the IP address configured in the network card

If you want to delete an IP from the network card, use the following ip command:

[linuxtechi@localhost]$ sudo ip addr del 192.168.0.10/24 dev enp0s3
登入後複製
Case 5: Add an alias for the network card (assuming the network card is named enp0s3)

添加别名,即为网卡添加不止一个 IP,执行下面命令:
iproute 安装包中 12个  ip 常用命令

[linuxtechi@localhost]$  sudo ip addr add 192.168.0.20/24 dev enp0s3 label enp0s3:1
登入後複製

ip-command-add-alias-linux

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

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

[linuxtechi@localhost]$  ip route show
登入後複製

iproute 安装包中 12个  ip 常用命令

ip-route-command-output

在上面输出结果中,我们能够看到所有网卡上数据包的路由信息。我们也可以获取特定 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,是地址解析协议Address Resolution Protocol的缩写,用于将 IP 地址转换为物理地址(也就是 MAC 地址)。所有的 IP 和其对应的 MAC 明细都存储在一张表中,这张表叫做 ARP 缓存。

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

[linuxtechi@localhost]$  ip neigh
登入後複製

iproute 安装包中 12个  ip 常用命令

ip-neigh-command-linux

案例 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
登入後複製

iproute 安装包中 12个  ip 常用命令

ip-s-command-linux

案例 12:获取帮助

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

[linuxtechi@localhost]$ ip help
登入後複製

以上是iproute 安裝套件中 12個 ip 常用指令的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

vscode需要什麼電腦配置 vscode需要什麼電腦配置 Apr 15, 2025 pm 09:48 PM

VS Code 系統要求:操作系統:Windows 10 及以上、macOS 10.12 及以上、Linux 發行版處理器:最低 1.6 GHz,推薦 2.0 GHz 及以上內存:最低 512 MB,推薦 4 GB 及以上存儲空間:最低 250 MB,推薦 1 GB 及以上其他要求:穩定網絡連接,Xorg/Wayland(Linux)

Linux體系結構:揭示5個基本組件 Linux體系結構:揭示5個基本組件 Apr 20, 2025 am 12:04 AM

Linux系統的五個基本組件是:1.內核,2.系統庫,3.系統實用程序,4.圖形用戶界面,5.應用程序。內核管理硬件資源,系統庫提供預編譯函數,系統實用程序用於系統管理,GUI提供可視化交互,應用程序利用這些組件實現功能。

vscode終端使用教程 vscode終端使用教程 Apr 15, 2025 pm 10:09 PM

vscode 內置終端是一個開發工具,允許在編輯器內運行命令和腳本,以簡化開發流程。如何使用 vscode 終端:通過快捷鍵 (Ctrl/Cmd ) 打開終端。輸入命令或運行腳本。使用熱鍵 (如 Ctrl L 清除終端)。更改工作目錄 (如 cd 命令)。高級功能包括調試模式、代碼片段自動補全和交互式命令歷史。

git怎麼查看倉庫地址 git怎麼查看倉庫地址 Apr 17, 2025 pm 01:54 PM

要查看 Git 倉庫地址,請執行以下步驟:1. 打開命令行並導航到倉庫目錄;2. 運行 "git remote -v" 命令;3. 查看輸出中的倉庫名稱及其相應的地址。

notepad怎麼運行java代碼 notepad怎麼運行java代碼 Apr 16, 2025 pm 07:39 PM

雖然 Notepad 無法直接運行 Java 代碼,但可以通過借助其他工具實現:使用命令行編譯器 (javac) 編譯代碼,生成字節碼文件 (filename.class)。使用 Java 解釋器 (java) 解釋字節碼,執行代碼並輸出結果。

vscode在哪寫代碼 vscode在哪寫代碼 Apr 15, 2025 pm 09:54 PM

在 Visual Studio Code(VSCode)中編寫代碼簡單易行,只需安裝 VSCode、創建項目、選擇語言、創建文件、編寫代碼、保存並運行即可。 VSCode 的優點包括跨平台、免費開源、強大功能、擴展豐富,以及輕量快速。

Linux的主要目的是什麼? Linux的主要目的是什麼? Apr 16, 2025 am 12:19 AM

Linux的主要用途包括:1.服務器操作系統,2.嵌入式系統,3.桌面操作系統,4.開發和測試環境。 Linux在這些領域表現出色,提供了穩定性、安全性和高效的開發工具。

vscode終端命令不能用 vscode終端命令不能用 Apr 15, 2025 pm 10:03 PM

VS Code 終端命令無法使用的原因及解決辦法:未安裝必要的工具(Windows:WSL;macOS:Xcode 命令行工具)路徑配置錯誤(添加可執行文件到 PATH 環境變量中)權限問題(以管理員身份運行 VS Code)防火牆或代理限制(檢查設置,解除限制)終端設置不正確(啟用使用外部終端)VS Code 安裝損壞(重新安裝或更新)終端配置不兼容(嘗試不同的終端類型或命令)特定環境變量缺失(設置必要的環境變量)

See all articles