歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

每天一個Linux命令(46)ifconfig命令

[b]在windows系統中,ipconfig命令行工具被用來獲取網絡接口配置信息並對此進行修改。Linux系統擁有一個類似的工具,也就是ifconfig(interfaces config)[/b][b] (1)用法:[/b]

[b] 用法: ifconfig [網絡設備] [參數][/b][b] (2)功能:[/b]

[b] 功能: ifconfig命令用來查看和配置網絡設備,當網絡環境發生改變時可通過此命令對網絡進行相應的配置。[/b] [b]注意: 用ifconfig命令配置的網卡信息,在網卡重啟後機器重啟後,配置就不存在。要想將上述的配置信息永遠的存在電腦裡,那就要修改網卡的配置文件了。[/b]

[b] (3)選項參數:[/b] 1) up        啟動指定網絡設備/網卡。

2) down       關閉指定網絡設備/網卡。該參數可以有效地阻止通過指定接口的IP信息流,如果想永久地關閉一個接口,我們還需要從核心路由表中將該接口的路由信息全部刪除。 3) -a          無論是否激活,顯示所有配置的網絡接口。

4) add         給指定網卡配置IPv6地址  5) del         刪除指定網卡的IPv6地址

6) arp|-arp       打開或關閉支持ARP協議 7) mtu<字節數>    設置網卡的最大傳輸單元

8) netmask<子網掩碼> 設置網卡的子網掩碼[b] (4)實例:[/b]

1)[sunjimeng@localhost ~]$ ifconfig        

[sunjimeng@localhost ~]$ ifconfig         //未聯網時的參數
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:4d:a3:cc  txqueuelen 1000  (Ethernet)
        RX packets 1914  bytes 174936 (170.8 KiB)
        RX errors 0  dropped 36  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        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 3246  bytes 281472 (274.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3246  bytes 281472 (274.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[sunjimeng@localhost ~]$ ifconfig                   //連接到有線網時的參數
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500                //mtu表示最大傳輸單元
        inet 192.168.117.128  netmask 255.255.255.0  broadcast 192.168.117.255
        inet6 fe80::20c:29ff:fe4d:a3cc  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4d:a3:cc  txqueuelen 1000  (Ethernet)
        RX packets 1969  bytes 181974 (177.7 KiB)
        RX errors 0  dropped 36  overruns 0  frame 0
        TX packets 48  bytes 6324 (6.1 KiB)
        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 3590  bytes 310452 (303.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3590  bytes 310452 (303.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[b] 說明:[/b]

1)etn(Number)指的是網卡,可以看到目前這個網卡的物理地址(MAC地址)是 00:0c:29:4d:a3:cc。

2)inet後表示ip地址,此網卡的ip地址是192.168.117.128,廣播地址是192.168.117.255,掩碼地址為255.255.255.0。

3)lo是表示主機的回壞地址,這個一般是用來測試一個網絡程序,但又不想讓局域網或外網的用戶能夠查看,只能在此台主機上運行和查看所用的網絡接口。

比如把 http服務器指定到回環地址,在浏覽器輸入127.0.0.1就能看到你所架WEB網站了。但只是您能看得到,局域網的其它主機或用戶無從知道。

2)[root@localhost sunjimeng]# ifconfig eno16777736 down    打開和關閉指定網卡

[sunjimeng@localhost ~]$ ifconfig eno16777736 down      //必須是root權限才可以
SIOCSIFFLAGS: 不允許的操作
[sunjimeng@localhost ~]$ su root
密碼:
[root@localhost sunjimeng]# ifconfig eno16777736 down
[root@localhost sunjimeng]# ifconfig
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 3598  bytes 311224 (303.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3598  bytes 311224 (303.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost sunjimeng]# ifconfig eno16777736 up
[root@localhost sunjimeng]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:4d:a3:cc  txqueuelen 1000  (Ethernet)
        RX packets 2013  bytes 185705 (181.3 KiB)
        RX errors 0  dropped 36  overruns 0  frame 0
        TX packets 88  bytes 9726 (9.4 KiB)
        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 3602  bytes 311520 (304.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3602  bytes 311520 (304.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

注意:ssh登陸linux服務器,關閉了網卡就不能開啟了,除非你有多網卡。 3)[root@localhost sunjimeng]# ifconfig -a      無論是否激活,顯示所有配置的網絡接口,不帶參數的只顯示已激活的網絡接口

[root@localhost sunjimeng]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:4d:a3:cc  txqueuelen 1000  (Ethernet)
        RX packets 2016  bytes 185981 (181.6 KiB)
        RX errors 0  dropped 36  overruns 0  frame 0
        TX packets 88  bytes 9726 (9.4 KiB)
        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 3602  bytes 311520 (304.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3602  bytes 311520 (304.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
4)啟用或關閉支持ARP協議

[root@localhost sunjimeng]# ifconfig eno16777736 arp
[root@localhost sunjimeng]# ifconfig eno16777736 -arp
5)[root@localhost sunjimeng]# ifconfig eno16777736 mtu 2000    設置網絡設備傳輸的最大單元

[root@localhost sunjimeng]# ifconfig
eno16777736: flags=4291<UP,BROADCAST,RUNNING,NOARP,MULTICAST>  mtu 1500
        inet 192.168.117.128  netmask 255.255.255.0  broadcast 192.168.117.255
        inet6 fe80::20c:29ff:fe4d:a3cc  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4d:a3:cc  txqueuelen 1000  (Ethernet)
        RX packets 75518  bytes 108412465 (103.3 MiB)
        RX errors 0  dropped 36  overruns 0  frame 0
        TX packets 20662  bytes 1263791 (1.2 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 4381  bytes 364888 (356.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4381  bytes 364888 (356.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost sunjimeng]# ifconfig eno16777736 mtu 2000
[root@localhost sunjimeng]# ifconfig
eno16777736: flags=4291<UP,BROADCAST,RUNNING,NOARP,MULTICAST>  mtu 2000
        ether 00:0c:29:4d:a3:cc  txqueuelen 1000  (Ethernet)
        RX packets 99762  bytes 145117330 (138.3 MiB)
        RX errors 0  dropped 36  overruns 0  frame 0
        TX packets 20663  bytes 1263851 (1.2 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 5243  bytes 424328 (414.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5243  bytes 424328 (414.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
6)[root@localhost sunjimeng]# ifconfig eno16777736 hw ether 00:0c:29:4d:a3:dd    設置網卡的mac地址

[root@localhost sunjimeng]# ifconfig
eno16777736: flags=4291<UP,BROADCAST,RUNNING,NOARP,MULTICAST>  mtu 2000
        inet 0.0.0.64  netmask 0.0.0.0  broadcast 255.255.255.255
        ether 00:0c:29:4d:a3:cc  txqueuelen 1000  (Ethernet)
        RX packets 109425  bytes 159195954 (151.8 MiB)
        RX errors 0  dropped 36  overruns 0  frame 0
        TX packets 20668  bytes 1264151 (1.2 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 33371  bytes 2821752 (2.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 33371  bytes 2821752 (2.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost sunjimeng]# ifconfig eno16777736 hw ether 00:0c:29:4d:a3:dd
[root@localhost sunjimeng]# ifconfig
eno16777736: flags=4291<UP,BROADCAST,RUNNING,NOARP,MULTICAST>  mtu 2000
        inet 0.0.0.64  netmask 0.0.0.0  broadcast 255.255.255.255
        ether 00:0c:29:4d:a3:dd  txqueuelen 1000  (Ethernet)
        RX packets 109434  bytes 159196494 (151.8 MiB)
        RX errors 0  dropped 36  overruns 0  frame 0
        TX packets 20668  bytes 1264151 (1.2 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 34011  bytes 2876296 (2.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 34011  bytes 2876296 (2.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Copyright © Linux教程網 All Rights Reserved