歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux配置網卡IP地址命令及常用網絡配置命令

linux配置網卡IP地址命令及常用網絡配置命令

日期:2017/3/3 16:19:17   编辑:關於Linux

ifconfig eth0 192.168.1.102 netmask 255.255.255.0 //添加IP地址

route add default gw 192.168.1.1 //添加網關

啟動生效:

vim /etc/sysconfig/network-scripts/ifcfg-eth0 //(某些版本中可能沒有vim這個編輯器比如centos,大家可能用vi替換)

IPADDR=192.168.1.102 //添加IP地址

NETMASK=255.255.255.0 //掩碼

GATEWAY=192.168.1.1 //添加網關

最後結果如 下:

DEVICE=eth0

BOOTPROTO=static

IPADDR=192.168.1.102

NETMASK=255.255.255.0

GETEWAY=192.1 68.1.1

ONBOOT=yes

TYPE=Ethernet

注:BOOTPROTO只有在static(靜態)模式下才可以使用設置的IP信 息

修改dns

vim /etc/resolv.conf

修改後可即時生效,啟動同樣有效

形式

加入nameserver= ***.***.***.***

最多可有三個,作為上一個失敗後的候選

修改host name

即時生效:

hostname fc2

啟動生效:

vim /etc/sysconfig/network

形式如下:

NETWORKING=yes

HOSTNAME=rh- linux.fc.org

##########################################################

首先,先了解傳統的網 絡配置命令:

1. 使用ifconfig命令配置並查看網絡接口情況

示例1: 配置eth0的IP,同時激活設 備:

ifconfig eth0 192.168.4.1 netmask 255.255.255.0 up

示例2: 配置eth0別名設備 eth0:1 的IP, 並添加路由

ifconfig eth0:1 192.168.4.2

route add –host 192.168.4.2 dev eth0:1

示例3:激活 (禁用)設備

ifconfig eth0:1 up(down)

示例4:查看所有(指定)網絡接口配置

ifconfig (eth0)

—————————

2. 使用route 命令配置路由表

示例1:添加到主機路由

route add –host 192.168.4.2 dev eth0:1

route add –host 192.168.4.1 gw 192.168.4.250

示例2:添加到網絡的路由

route add –net IP netmask MASK eth0

route add –net IP netmask MASK gw IP

route add – net IP/24 eth1

示例3:添加默認網關

route add default gw IP

示例4:刪除路由

route del – host 192.168.4.1 dev eth0:1

示例5:查看路由信息

route 或  route -n  (-n 表示不解析名字,列出 速度會比route 快)

—————————

3.arp 管理命令

示例1:查看arp緩存

arp

示例2: 添加

arp –s IP MAC

示例3: 刪除

arp –d IP

—————————

4. ip是iproute2軟件包裡面的 一個強大的網絡配置工具,它能夠替代一些傳統的網絡管理工具。

例如:ifconfig、route等,

上面的示 例完全可以用下面的ip命令實現,而且ip命令可以實現更多的功能.下面介紹一些示例:

4.0 ip命令的語法

ip [OPTIONS] OBJECT [COMMAND [ARGUMENTS]]

4.1 ip link set–改變設備的屬性. 縮寫:set、s

示例1:up/down 起動/關閉設備。

ip link set dev eth0 up

這個等於傳統的

ifconfig eth0 up

ifconfig eth0 down

www.bianceng.cn

示例2:改變設備傳輸隊列的長度。

參數:txqueuelen NUMBER或者txqlen NUMBER

ip link set dev eth0 txqueuelen 100

示例3:改變網絡設備MTU(最大傳輸單元)的值。

ip link set dev eth0 mtu 1500

示例4: 修改網絡設備的MAC地址。

參數: address LLADDRESS

ip link set dev eth0 address 00:01:4f:00:15:f1

4.2 ip link show–顯示設備屬性.  縮寫:show、list、 lst、sh、ls、l

-s選項出現兩次或者更多次,ip會輸出更為詳細的錯誤信息統計。

示例:

ip -s -s link ls eth0

這個命令等於傳統的

ifconfig eth0

—————————

5.1 ip address add–添 加一個新的協議地址. 縮寫:add、a

示例1:為每個地址設置一個字符串作為標簽。為了和Linux-2.0的網 絡別名兼容,這個字符串必須以設備名開頭,接著一個冒號,

ip addr add local 192.168.4.1/28 brd + label eth0:1 dev eth0

等價於

ifconfig eth0:5 192.168.4.1/28

ip addr add local 192.168.4.1/28 dev eth0

示例2: 在以太網接口eth0上增加一個地址192.168.20.0,掩碼長度為24位 (155.155.155.0),標准廣播地址,標簽為 eth0:Alias:

ip addr add 192.168.4.2/24 brd + dev eth1 label eth1:1

這個命令等於傳統的: 

ifconfig eth1:1 192.168.4.2/24

5.2 ip address delete– 刪除一個協議地址. 縮寫:delete、del、d

ip addr del 192.168.4.1/24 brd + dev eth0 label eth0:Alias1

5.3 ip address show–顯示協議地址. 縮寫:show、list、lst、sh、ls、l

ip addr ls eth0

5.4.ip address flush–清除協議地址. 縮寫:flush、f

示例1 : 刪除屬於私網10.0.0.0/8的所有 地址:

ip -s -s a f to 10/8

示例2 : 取消所有以太網卡的IP地址

ip -4 addr flush label “eth0 ″

—————————

6. ip neighbour–neighbour/arp表管理命令

縮寫 neighbour、neighbor、 neigh、n

命令 add、change、replace、delete、fulsh、show(或者list)

6.1 ip neighbour add — 添 加一個新的鄰接條目

ip neighbour change–修改一個現有的條目

ip neighbour replace–替換一個已 有的條目

縮寫:add、a;change、chg;replace、repl

示例1: 在設備eth0上,為地址10.0.0.3添加一 個permanent arp條目:

ip neigh add 10.0.0.3 lladdr 0:0:0:0:0:1 dev eth0 nud perm

示例2:把狀 態改為reachable

ip neigh chg 10.0.0.3 dev eth0 nud reachable

6.2.ip neighbour delete–刪除一 個鄰接條目

示例1:刪除設備eth0上的一個arp條目10.0.0.3

ip neigh del 10.0.0.3 dev eth0

6.3.ip neighbour show–顯示網絡鄰居的信息. 縮寫:show、list、sh、ls

示例1: ip -s n ls 193.233.7.254

193.233.7.254. dev eth0 lladdr 00:00:0c:76:3f:85 ref 5 used 12/13/20 nud reachable

6.4.ip neighbour flush–清除鄰接條目. 縮寫:flush、f

示例1: (-s 可以顯示詳細信息)

ip -s -s n f 193.233.7.254

—————————

7. 路由表管理

7.1.縮寫 route、ro、 r

7.5.路由表

從Linux-2.2開始,內核把路由歸納到許多路由表中,這些表都進行了編號,編號數字的范 圍是1到255。另外,

為了方便,還可以在/etc/iproute2/rt_tables中為路由表命名。

默認情況下,所 有的路由都會被插入到表main(編號254)中。在進行路由查詢時,內核只使用路由表main。

7.6.ip route add — 添加新路由

ip route change — 修改路由

ip route replace — 替換已有的路由

縮寫:add 、a;change、chg;replace、repl

示例1: 設置到網絡10.0.0/24的路由經過網關193.233.7.65

ip route add 10.0.0/24 via 193.233.7.65

示例2: 修改到網絡10.0.0/24的直接路由,使其經過設備 dummy

ip route chg 10.0.0/24 dev dummy

示例3: 實現鏈路負載平衡.加入缺省多路徑路由,讓ppp0和 ppp1分擔負載(注意:scope值並非必需,它只不過是告訴內核,這個路由要經過網關而不是直連 的。實際上 ,如果你知道遠程端點的地址,使用via參數來設置就更好了)。

ip route add default scope global nexthop dev ppp0 nexthop dev ppp1

ip route replace default scope global nexthop dev ppp0 nexthop dev ppp1

示例4: 設置NAT路由。在轉發來自192.203.80.144的數據包之前,先進行網絡地址轉換 ,把這個地址轉換為193.233.7.83

ip route add nat 192.203.80.142 via 193.233.7.83

示例5: 實現 數據包級負載平衡,允許把數據包隨機從多個路由發出。weight 可以設置權重.

ip route replace default equalize nexthop via 211.139.218.145 dev eth0 weight 1 nexthop via 211.139.218.145 dev eth1 weight 1

7.7.ip route delete– 刪除路由

縮寫:delete、del、d

示例1:刪除上一節命令加入的多 路徑路由

ip route del default scope global nexthop dev ppp0 nexthop dev ppp1

7.8.ip route show — 列出路由

縮寫:show、list、sh、ls、l

示例1: 計算使用gated/bgp協議的路由個數

ip route ls proto gated/bgp |wc

1413  9891  79010

示例2: 計算路由緩存裡面的條數,由於被緩存 路由的屬性可能大於一行,以此需要使用-o選項

ip -o route ls cloned |wc

159  2543  18707

示例3: 列出路由表TABLEID裡面的路由。缺省設置是table main。TABLEID或者是一個真正的路由表ID或者 是/etc/iproute2/rt_tables文件定義的字符串,

或者是以下的特殊值:

all — 列出所有表的路由;

cache — 列出路由緩存的內容。

ip ro ls 193.233.7.82 tab cache

示例4: 列出某個路由表的內容

ip route ls table fddi153

示例5: 列出默認路由表的內容

ip route ls

這個命令等於傳統的: route

7.9.ip route flush — 擦除路由表

示例1: 刪除路由表main中的所有網關路由(示例:在路由監 控程序掛掉之後):

ip -4 ro flush scope global type unicast

示例2:清除所有被克隆出來的IPv6路 由:

ip -6 -s -s ro flush cache

示例3: 在gated程序掛掉之後,清除所有的BGP路由:

ip -s ro f proto gated/bgp

示例4: 清除所有ipv4路由cache

ip route flush cache

*** IPv4 routing cache is flushed.

7.10 ip route get — 獲得單個路由 .縮寫:get

Copyright © Linux教程網 All Rights Reserved