常用網絡命令(一)

一、.ifconfig命令

用來設定IP地址等。

1.命令格式

ifconfig [網絡設備] [參數]

2.常用選項

查看:ifconfig [Interface] [-a]

啓用或禁用:ifconfig IFACE up|down

啓用或禁用混雜模式:[-]promisc,如果選擇此參數,網卡將接收網絡中發給它所有的數據包

設定MTU: mtu N

設定IP:
                ifconfig IFACE IP/Netmask [up]
                ifcconfig IFACE IP netmask NETMASK
                #立即起效,但不能永久有效;

3、實例

1)、查看所有網卡狀態

[root@bash ~]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.6  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::20c:29ff:fee1:6e8c  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:e1:6e:8c  txqueuelen 1000  (Ethernet)
        RX packets 2861646  bytes 2752671819 (2.5 GiB)
        RX errors 0  dropped 224  overruns 0  frame 0
        TX packets 1032405  bytes 108810235 (103.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 1308  bytes 141244 (137.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1308  bytes 141244 (137.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2)、啓用和禁用網卡

[root@CentOS ~]# ifconfig eth1 down
[root@CentOS ~]# ifconfig|grep eth1
[root@CentOS ~]# ifconfig eth1 up
[root@CentOS ~]# ifconfig|grep eth1
eth1      Link encap:Ethernet  HWaddr 00:0C:29:30:84:C4

3)、設置臨時ip地址

[root@CentOS ~]# ifconfig eth1 192.168.1.11/24 up
[root@CentOS ~]# ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 00:0C:29:30:84:C4  
          inet addr:192.168.1.11  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:771 errors:0 dropped:0 overruns:0 frame:0
          TX packets:202 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:50906 (49.7 KiB)  TX bytes:24364 (23.7 KiB)

4)、設置MTU

[root@CentOS ~]# ifconfig eth1 mtu 2000
[root@CentOS ~]# ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 00:0C:29:30:84:C4  
          inet addr:192.168.1.11  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST SLAVE MULTICAST  MTU:2000  Metric:1
          RX packets:771 errors:0 dropped:0 overruns:0 frame:0
          TX packets:202 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:50906 (49.7 KiB)  TX bytes:24364 (23.7 KiB)

二、route命令

查看管理路由表,立即起效,但不會永久生效;

1.命令格式

route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]

2.常用選項

查看內核路由表:route [-n][-ee]
添加路由:route add
            -host HOST_IP gw NEXT_HOP [dev DEVICE]  #不能填寫掩碼
            -net NET_ADDR gw NEXT_HOP [dev DEVICE]  #只能制定網段
            -net 0.0.0.0 gw NEXT_HOP [dev DEVICE]
            default gw NEXT_HOP [dev DEVICE]
刪除路由:route del
            -host HOST_IP
            -net NET_ADDR

3.實例

1)、查看當前路由表

[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
0.0.0.0         192.168.120.240 0.0.0.0         UG    0      0        0 eth0

說明:第一行表示主機所在網絡的地址爲192.168.120.0,若數據傳送目標是在本局域網內通信,則可直接通過eth0轉發數據包;

第四行表示數據傳送目的是訪問Internet,則由接口eth0,將數據包發送到網關192.168.120.240

其中Flags爲路由標誌,標記當前網絡節點的狀態。

Metric 距離、跳數。暫無用。

Ref 不用管,恆爲0。

Flags標誌說明:

  • U Up表示此路由當前爲啓動狀態

  • H Host,表示此網關爲一主機

  • G Gateway,表示此網關爲一路由器

  • R Reinstate Route,使用動態路由重新初始化的路由

  • D Dynamically,此路由是動態性地寫入

  • M Modified,此路由是由路由守護程序或導向器動態修改

  • ! 表示此路由當前爲關閉狀態

備註:

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

2)、添加路由表

[root@CentOS ~]# route add default gw 192.168.1.9 #添加默認網關
[root@CentOS ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.1.9     0.0.0.0         UG    0      0        0 eth0

[root@CentOS ~]# route add -net 192.168.10.0 gw 192.168.10.22\
 netmask 255.255.255.0 dev eth1 #添加網絡路由
[root@CentOS ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.10.0    192.168.10.22   255.255.255.0   UG    0      0        0 eth1
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.1.9     0.0.0.0         UG    0      0        0 eth0

[root@CentOS ~]# route add -host 192.168.10.3 gw 192.168.10.22 dev eth1 
#添加主機路由,此處不應該寫掩碼,寫會報錯                    

[root@CentOS ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.10.3    192.168.10.22   255.255.255.255 UGH   0      0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.10.0    192.168.10.22   255.255.255.0   UG    0      0        0 eth1
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.1.9     0.0.0.0         UG    0      0        0 eth0

3)、刪除路由記錄

[root@CentOS ~]# route del -host 192.168.10.3 gw 192.168.10.22 dev eth1 #刪除主機路由
[root@CentOS ~]# route del -net 192.168.10.0 gw 192.168.10.22\
netmask 255.255.255.0 dev eth1 #刪除網絡路由
[root@CentOS ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.1.9     0.0.0.0         UG    0      0        0 eth0

小貼士:

在linux中添加永久路由的方法

1)、寫入/etc/rc.loacl

可以將上面提到的命令寫入/etc/rc.local文件中,這樣在系統啓動的時候會自動加入相關的路由設置 不過這種方法有一個缺點:如果某個系統服務,比如說是NFS服務,這個服務是在啓動network服務之後,在執行rc.local之前,如果你設置的有自動掛載的nfs,那麼,這裏鏈路的不通暢,會造成掛載的失敗。另外一個就是如果你重啓了網絡服務器,那麼路由就失效了,這個時候你不得不重新加載這個文件,但是如果你是遠程操作的呢?所以,這個方法不是非常的不推薦

2)、在/etc/sysconfig/network裏添加到文件末尾,格式如下(注:未驗證有效性):

GATEWAY=gw-ip 或者 GATEWAY=gw-dev 這種方式從外觀上來看只能用於添加默認網關了。。。對於多路網絡環境貌似不太適合的樣子

3)、直接寫入ifcfg文件

在配置ip地址的時候直接將GATEWAY的配置寫入ifcfg文件,形式:GATEWAY=gw-ip 適合添加默認路由

4)、寫入/etc/sysconfig/static-routes文件

默認在/etc/sysconifg目錄中是沒有這個文件的,需要我們手工創建,對這個文件的調用在下面: /etc/init.d/network:

159         # Add non interface-specific static-routes.
160         if [ -f /etc/sysconfig/static-routes ]; then
161            grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
162               /sbin/route add -$args
163            done
164         fi

/etc/sysconfig/static-routes:
any net 192.168.1.0/24 gw 192.168.1.24
any net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1

這種方式的話,和rc.local比較而言,比較實用,還比如nfs,這個路由的生效時間是在網絡服務network啓動的時候生效的,而其他的一些網絡相關服務都是在網絡服務啓動成功之後再啓動的,所以能夠保證網絡鏈路的通暢。 而且,如果我重啓了網絡服務,這個腳本是在網絡服務啓動腳本里面調用的,所以也會自動添加上相關的路由設置

三、tui或者gui配置網絡命令

TUI:system-config-network-tui
GUI: system-config-network-gui
#setup
#nmtui    NetworkManager的字符界面管理工具

四、ip命令

ip是iproute2軟件包裏面的一個強大的網絡配置工具,它能夠替代一些傳統的網絡管理工具,例如ifconfig、route等,使用權限爲超級用戶。幾乎所有的Linux發行版本都支持該命令。

ip [ OPTIONS ] OBJECT { COMMAND | help }

OBJECT:
            link: 鏈接接口,如eth0
            addr: 管理設備上的地址
            route: 管理路由

link:
            show [IFACE]
            set IFACE {up|down}
            set IFACE name NEWNAME

addr:
            添加地址:ip addr add IP/Prefixlen dev DEVICE
            刪除地址:ip addr del IP/prefixlen dev DEVICE
            查看:ip addr show [IFACE]
            清空:ip addr flush IFACE

route:
            查看路由:ip route {show|list}
            添加路由:ip route add DESTINATION via NEXT_HOP [dev DEIVCE]
            定義默認網關:ip route add default via NEXT_HOP [dev DEVICE]
            刪除路由:ip route del DESTINATION 
            搜索指定路由:ip route get DESTINATION

實例:

1)link

[root@young ~]# ip link #顯示所有網絡設備狀態,ip -s link會顯示更爲詳細信息
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:30:84:c4 brd ff:ff:ff:ff:ff:ff
3: eth1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
4: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN 
    link/ether 22:4f:02:f2:45:36 brd ff:ff:ff:ff:ff:ff
[root@young ~]# ip link show eth0  #顯示指定網絡設備狀態
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:30:84:c4 brd ff:ff:ff:ff:ff:ff

[root@young ~]# ip link set eth1 down #關閉指定設備
[root@young ~]# ip link show eth1
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
[root@young ~]# ip link set eth1 up  #打開指定設備

[root@young ~]# ip link set eth1 down
[root@young ~]# ip link set eth1 name eth9  #更改設備名稱,要先關閉設備
[root@young ~]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:30:84:c4 brd ff:ff:ff:ff:ff:ff
3: eth9: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
4: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN 
    link/ether 22:4f:02:f2:45:36 brd ff:ff:ff:ff:ff:ff

2)addr

[root@young ~]# ip addr add 192.168.10.11/24 dev eth1  #給設備添加ip地址
[root@young ~]# ip addr show eth1
3: eth1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.3/24 brd 192.168.10.255 scope global eth1
    inet 192.168.10.11/24 scope global secondary eth1

[root@young ~]# ip addr del 192.168.10.11/24 dev eth1  #刪除設備ip地址
[root@young ~]# ip addr show eth1
3: eth1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.3/24 brd 192.168.10.255 scope global eth1

[root@young ~]# ip addr show  #查看所有設備ip地址
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:30:84:c4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.9/24 brd 192.168.1.255 scope global eth0
    inet6 fe80::20c:29ff:fe30:84c4/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.3/24 brd 192.168.10.255 scope global eth1
4: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN 
    link/ether 22:4f:02:f2:45:36 brd ff:ff:ff:ff:ff:ff

[root@young ~]# ip addr show eth0 #查看制定設備ip地址
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:30:84:c4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.9/24 brd 192.168.1.255 scope global eth0
    inet6 fe80::20c:29ff:fe30:84c4/64 scope link 
       valid_lft forever preferred_lft forever

[root@young ~]# ip addr show eth1
3: eth1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.3/24 brd 192.168.10.255 scope global eth1
[root@young ~]# ip addr flush eth1   #清空指定設備的ip地址
[root@young ~]# ip addr show eth1 
3: eth1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:0c:29:30:84:ce brd ff:ff:ff:ff:ff:ff

3)route

[root@young ~]# ip route show  #顯示所有路由記錄
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.9 
169.254.0.0/16 dev eth0  scope link  metric 1002 

[root@young ~]# ip route add 192.168.10.0 via 192.168.10.6 dev eth1 #添加路由記錄
[root@young ~]# ip route list
192.168.10.0 via 192.168.10.6 dev eth1 
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.9 
192.168.10.0/23 dev eth1  proto kernel  scope link  src 192.168.10.6 
169.254.0.0/16 dev eth0  scope link  metric 1002 
[root@young ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.10.0    192.168.10.6    255.255.255.255 UGH   0      0        0 eth1
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.10.0    0.0.0.0         255.255.254.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

[root@young ~]# ip route add default via 192.168.1.1 dev eth0  #添加默認路由

[root@young ~]# ip route del 192.168.10.0/23 #刪除指定路由
[root@young ~]# ip route show
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.9 
169.254.0.0/16 dev eth0  scope link  metric 1002 
default via 192.168.1.1 dev eth0 

[root@young ~]# ip route get 10.42.0.47 #查看路由包來源
10.42.0.47 via 192.168.1.1 dev eth0  src 192.168.1.9 
cache  mtu 1500 hoplimit 64


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章