--- tags: Skills39.tw, Skills39, Debian, ip, ip a, 路由表, ip command, ip route, interfaces, ipv6, ip設定, 路由設定, 預設路由, ip address, 網路設定, Linux , linux GA: UA-158089455-3 image: none --- # Debian 10 buster ip 基本指令說明 ## 前言 此篇文章在說明Debian下ip這個指令較為常用的用途,這次來介紹debian內ip這個工具的一些基本指令,包含設定ip, 路由(route)及預設閘道(gateway),啟用/停用網卡等。 >《操作環境》 > OS : *Debian GNU/Linux 10 (buster)* > 套件 : iproute2-ss190107 (amd64) ## 查詢網路設定 1. **查詢整體設定** ``` # ip a ``` 或者是 ``` # ip addr show ``` ![EX1](https://i.imgur.com/hEzH35A.png) 2. **只查詢ipv4/ipv6的設定** ipv4如下 ``` # ip -4 a ``` ![Imgur](https://i.imgur.com/1RQSOIr.png) ipv6如下 ``` # ip -6 a ``` ![Imgur](https://i.imgur.com/Fy8hhLO.png) 3. **單獨查詢網卡資訊** 底下假設要查詢的網卡名稱為eth 1,那就打 ``` # ip a show eth1 ``` 或是 ``` # ip a show dev eth1 ``` ![Imgur](https://i.imgur.com/TsRfTNZ.png) ## 定義網路設定 1. **新增網卡(eth1)的ip位址** ``` # ip addr add ip位址/CIDR dev eth1 ``` ![Imgur](https://i.imgur.com/N2Ax1py.png) 2. **移除網卡(eth1)的ip位址** ``` # ip addr del ip位址/CIDR dev eth1 ``` ![Imgur](https://i.imgur.com/WHD6pEp.png) ***※以上的設定(新增和刪除)只是暫時的,重開機(reboot)後便會消失,若想讓他重開機後不要消失請編輯 /etc/network/[interfaces](http://www.52iot.net/tw/book/Linux/61.mhtml) 檔案*** 3. **啟動網卡(eth1)** ``` # ip link set dev eth1 up ``` ![Imgur](https://i.imgur.com/CkneVa3.png) 4. **停用網卡(eth1)** ``` # ip link set dev eth1 down ``` ![Imgur](https://i.imgur.com/tXAUVYG.png) ## 查詢/設定路由表(Routing table) 1. **顯示路由表** ``` # ip r ``` 或是 ``` # ip route show ``` ![Imgur](https://i.imgur.com/niKBKQM.png) 2. **新增路由** - 靜態路由 ``` # ip route add [要送往目的地的網段] via [目的地ip位址] dev [網卡名稱] ``` ![Imgur](https://i.imgur.com/zcmuGAd.png) - 預設(default)路由 ``` # ip route add default via [目的地ip位址] dev [網卡名稱] ``` ![Imgur](https://i.imgur.com/cGqcZZh.png) 3. **刪除路由** - 靜態路由 ``` # ip route del [送往目的地的網段] ``` ![Imgur](https://i.imgur.com/JPI0U3z.png) - 預設(default)路由 ``` # ip route del default dev [網卡名稱] ``` ![Imgur](https://i.imgur.com/b3PAf8O.png) ***※上述的兩個路由設定一樣只是暫時的,重開機(reboot)便會消失,而自行設定路由的優先度是高於預設(default)路由的,也就是說自己有做設定的話就會以你做的設定為優先*** ## 參考資料 >啟用/關閉網卡 https://linuxize.com/post/linux-ip-command/ >網路設定查詢 http://benjr.tw/94155 >linux interfaces配置文件詳解 http://www.52iot.net/tw/book/Linux/61.mhtml >ip設定 https://www.cyberciti.biz/tips/configuring-static-routes-in-debian-or-red-hat-linux-systems.html *** 此文章由 Willy Hsu 撰寫於2019/11/7,有意見還請指教! (` ͜ʖ´)