ifconfig 命令介绍
ifconfig 是一個用來查看、配置、啟用或禁用網絡接口的工具,這個工具極為常用的。可以用這個工具來臨時性的配置網卡的IP地址、掩碼、廣播地址、網關等。也可以把它寫入一個文件中(比如/etc/rc.d/rc.local),這樣系統引導后,會讀取這個文件,為網卡設置IP地址
1 ifconfig 查看網絡接口狀態;
ifconfig 如果不接任何參數,就會輸出當前網絡接口的情況;
[root@localhost ~]# ifconfigeth0????? Link encap:Ethernet? HWaddr 00:C0:9F:94:78:0E????????? inet addr:192.168.1.88? Bcast:192.168.1.255? Mask:255.255.255.0????????? inet6 addr: fe80::2c0:9fff:fe94:780e/64 Scope:Link????????? UP BROADCAST RUNNING MULTICAST? MTU:1500? Metric:1????????? RX packets:850 errors:0 dropped:0 overruns:0 frame:0????????? TX packets:628 errors:0 dropped:0 overruns:0 carrier:0????????? collisions:0 txqueuelen:1000????????? RX bytes:369135 (360.4 KiB)? TX bytes:75945 (74.1 KiB)????????? Interrupt:10 Base address:0x3000lo??????? Link encap:Local Loopback????????? inet addr:127.0.0.1? Mask:255.0.0.0????????? inet6 addr: ::1/128 Scope:Host????????? UP LOOPBACK RUNNING? MTU:16436? Metric:1????????? RX packets:57 errors:0 dropped:0 overruns:0 frame:0????????? TX packets:57 errors:0 dropped:0 overruns:0 carrier:0????????? collisions:0 txqueuelen:0????????? RX bytes:8121 (7.9 KiB)? TX bytes:8121 (7.9 KiB)
解說:
eth0 表示第一塊網卡, 其中 HWaddr 表示網卡的物理地址,可以看到目前這個網卡的物理地址(MAC地址)是 00:C0:9F:94:78:0E ; inet addr 用來表示網卡的IP地址,此網卡的 IP地址是 192.168.1.88,廣播地址, Bcast:192.168.1.255,掩碼地址Mask:255.255.255.0
lo 是表示主機的回壞地址,這個一般是用來測試一個網絡程序,但又不想讓局域網或外網的用戶能夠查看,只能在此臺主機上運行和查看所用的網絡接口。比如把 HTTPD服務器的指定到回壞地址,在瀏覽器輸入 127.0.0.1 就能看到你所架WEB網站了。但只是您能看得到,局域網的其它主機或用戶無從知道;
如果想知道主機所有網絡接口的情況,請用下面的命令;
[root@localhost ~]# ifconfig -a
如果想查看某個端口,比如查看eth0 的狀態,就可以用下面的方法;
[root@localhost ~]# ifconfig eth0
2 ifconfig 配置網絡接口;
ifconfig 可以用來配置網絡接口的IP地址、掩碼、網關、物理地址等;值得一說的是用ifconfig 為網卡指定IP地址,這只是用來調試網絡用的,并不會更改系統關于網卡的配置文件。如果您想把網絡接口的IP地址固定下來,目前有三個方法:一是通過各個發行和版本專用的工具來修改IP地址;二是直接修改網絡接口的配置文件;三是修改特定的文件,加入ifconfig 指令來指定網卡的IP地址,比如在redhat或Fedora中,把ifconfig 的語名寫入/etc/rc.d/rc.local文件中;
ifconfig 配置網絡端口的方法:
ifconfig 工具配置網絡接口的方法是通過指令的參數來達到目的的,我們只說最常用的參數;
ifconfig 網絡端口 IP地址 hw <HW> MAC地址 netmask 掩碼地址 broadcast 廣播地址 [up/down]
* 實例一:
比如我們用ifconfig 來調試 eth0網卡的地址
[root@localhost ~]# ifconfig eth0 down
[root@localhost ~]# ifconfig eth0 192.168.1.99 broadcast 192.168.1.255 netmask 255.255.255.0
[root@localhost ~]# ifconfig eth0 up
[root@localhost ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:11:00:00:11:11
?????????? inet addr:192.168.1.99 Bcast:192.168.1.255 Mask:255.255.255.0
?????????? UP BROADCAST MULTICAST MTU:1500 Metric:1
?????????? RX packets:0 errors:0 dropped:0 overruns:0 frame:0
?????????? TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
?????????? collisions:0 txqueuelen:1000
?????????? RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
?????????? Interrupt:11 Base address:0x3400
注解: 上面的例子我們解說一下;
第一行:ifconfig eth0 down 表示如果eth0是激活的,就把它DOWN掉。此命令等同于 ifdown eth0;
第二行:用ifconfig 來配置 eth0的IP地址、廣播地址和網絡掩碼;
第三行:用ifconfig eth0 up 來激活eth0 ; 此命令等同于 ifup eth0
第四行:用 ifconfig eth0 來查看 eth0的狀態;
當然您也可以用直接在指令IP地址、網絡掩碼、廣播地址的同時,激活網卡;要加up參數;比如下面的例子;
[root@localhost ~]# ifconfig eth0 192.168.1.99 broadcast 192.168.1.255 netmask 255.255.255.0 up
* 實例二:在這個例子中,我們要學會設置網絡IP地址的同時,學會設置網卡的物理地址(MAC地址);
比如我們設置網卡eth1的IP地址、網絡掩碼、廣播地址,物理地址并且激活它;
[root@localhost ~]# ifconfig eth1 192.168.1.252 hw ether 00:11:00:00:11:11 netmask 255.255.255.0 broadcast 192.168.1.255 up
或
[root@localhost ~]# ifconfig eth1 hw ether 00:11:00:00:11:22
[root@localhost ~]# ifconfig eth1 192.168.1.252 netmask 255.255.255.0 broadcast 192.168.1.255 up
其中 hw 后面所接的是網絡接口類型, ether表示乙太網, 同時也支持 ax25 、ARCnet、netrom等,詳情請查看 man ifconfig ;
為網卡配置多IP
為一塊網卡配置多IP可以通過命令行設置也可以通過配置文件設置,以下我分別通過這兩種方式實現:
1.用命令行設置
ifconfig命令格式:ifconfig 設備名 IP netmask 掩碼 broadcast 廣播地址 動作
[root@localhost root]# ifconfig
eth0????? Link encap:Ethernet HWaddr 00:0E:1F:01:6C:50
????????? inet addr:192.168.16.108 Bcast:192.168.16.255 Mask:255.255.255.0
????????? UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
????????? RX packets:2 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:100
????????? RX bytes:224 (224.0 b) TX bytes:387 (387.0 b)
????????? Interrupt:11 Base address:0x2000
lo??????? Link encap:Local Loopback
????????? inet addr:127.0.0.1 Mask:255.0.0.0
????????? UP LOOPBACK RUNNING MTU:16436 Metric:1
????????? RX packets:773 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:773 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:0
????????? RX bytes:52352 (51.1 Kb) TX bytes:52352 (51.1 Kb)
可以看到,當前eth0的IP為192.168.16.108,此時請注意一下MAC(00:0E:1F:01:6C:50)
現在我們為eth0綁定另一個IP,設備為eth0:0,可以看到,現在又多了一個IP,注意,網卡實際上只有一塊(可以看MAC,都為00:0E:1F:01:6C:50),卻綁定了兩個IP,可以依此配置多個IP。
[root@localhost network-scripts]# ifconfig eth0:0 192.168.1.100 netmask 255.255.255.0
[root@localhost root]# ifconfig
eth0????? Link encap:Ethernet HWaddr 00:0E:1F:01:6C:50
????????? inet addr:192.168.16.108 Bcast:192.168.16.255 Mask:255.255.255.0
????????? UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
????????? RX packets:29 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:100
????????? RX bytes:2879 (2.8 Kb) TX bytes:387 (387.0 b)
????????? Interrupt:11 Base address:0x2000
eth0:0??? Link encap:Ethernet HWaddr 00:0E:1F:01:6C:50
????????? inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
????????? UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
????????? RX packets:29 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:100
????????? RX bytes:2879 (2.8 Kb) TX bytes:387 (387.0 b)
????????? Interrupt:11 Base address:0x2000
lo??????? Link encap:Local Loopback
????????? inet addr:127.0.0.1 Mask:255.0.0.0
????????? UP LOOPBACK RUNNING MTU:16436 Metric:1
????????? RX packets:7329 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:7329 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:0
????????? RX bytes:500674 (488.9 Kb) TX bytes:500674 (488.9 Kb)
2.通過配置文件設置多IP
網卡IP配置的文件在/etc/sysconfig/network-scripts/下,文件分別為ehtx或ethx:x
[root@localhost root]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-eth0????? ifdown-ippp ifdown-ppp ifup-aliases ifup-isdn?? ifup-ppp???? ifup-wireless
ifcfg-lo??????? ifdown-ipv6 ifdown-sit ifup-ippp???? ifup-plip?? ifup-routes init.ipv6-global
ifdown????????? ifdown-isdn ifdown-sl?? ifup-ipv6???? ifup-plusb ifup-sit???? network-functions
ifdown-aliases ifdown-post ifup??????? ifup-ipx????? ifup-post?? ifup-sl????? network-functions-ipv6
#這是eth0的對應的內容
[root@localhost network-scripts]#vi ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
USERCTL=no
PEERDNS=yes
TYPE=Ethernet
IPADDR=192.168.16.108
NETMASK=255.255.255.0
HWADDR=00:0e:1f:01:6c:50
GATEWAY=192.168.16.1
NETWORK=192.168.16.0
BROADCAST=192.168.16.255
配置另一IP,因我們剛才已配置了eth0:0,所以此時要用eth0:1了
[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth0:1
修改配置文件,內容如下
[root@localhost network-scripts]# vi ifcfg-eth0:1
DEVICE=eth0:1
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.2.100
NETMASK=255.255.255.0
NETWORK=192.168.2.0
啟動eth0:1,查看,此時是不是就已經有三個IP了?
[root@localhost network-scripts]# ifup eth0:1
[root@localhost network-scripts]# ifconfig
eth0????? Link encap:Ethernet HWaddr 00:0E:1F:01:6C:50
????????? inet addr:192.168.16.108 Bcast:192.168.16.255 Mask:255.255.255.0
????????? UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
????????? RX packets:52 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:100
????????? RX bytes:6037 (5.8 Kb) TX bytes:627 (627.0 b)
????????? Interrupt:11 Base address:0x2000
eth0:0??? Link encap:Ethernet HWaddr 00:0E:1F:01:6C:50
????????? inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
????????? UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
????????? RX packets:52 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:100
????????? RX bytes:6037 (5.8 Kb) TX bytes:627 (627.0 b)
????????? Interrupt:11 Base address:0x2000
eth0:1??? Link encap:Ethernet HWaddr 00:0E:1F:01:6C:50
????????? inet addr:192.168.2.100 Bcast:192.168.2.255 Mask:255.255.255.0
????????? UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
????????? RX packets:52 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:100
????????? RX bytes:6037 (5.8 Kb) TX bytes:627 (627.0 b)
????????? Interrupt:11 Base address:0x2000
lo??????? Link encap:Local Loopback
????????? inet addr:127.0.0.1 Mask:255.0.0.0
????????? UP LOOPBACK RUNNING MTU:16436 Metric:1
????????? RX packets:14131 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:14131 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:0
????????? RX bytes:965895 (943.2 Kb) TX bytes:965895 (943.2 Kb)
(*^_^*)配置成功!
轉載于:https://www.cnblogs.com/dengjiali/articles/2473184.html
總結
以上是生活随笔為你收集整理的ifconfig 命令介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LETTers比赛第七场 Guess t
- 下一篇: Hibernate关联关系映射-----