Linux ifconfig 配置網絡接口


Linux ifconfig 可以用來配置網絡接口的IP地址、掩碼、網關、物理地址等;值得一說的是用Linux ifconfig 為網卡指定IP地址,這只是用來調試網絡用的,並不會更改系統關於網卡的配置文件。

如果您想把網絡接口的IP地址固定下來,目前有三個方法:一是通過各個發行和版本專用的工具來修改IP地址;二是直接修改網絡接口的配置文件;三是修改特定的文件,加入Linux ifconfig 指令來指定網卡的IP地址,比如在redhat或Fedora中,把Linux ifconfig 的語名寫入/etc/rc.d/rc.local文件中;

Linux ifconfig 配置網絡端口的方法: Linux ifconfig 工具配置網絡接口的方法是通過指令的參數來達到目的的,我們只說最常用的參數; Linux ifconfig  網絡端口  IP地址    hw <HW>  MAC地址  netmask  掩碼地址    broadcast  廣播地址   [up/down]

實例一:

比如我們用Linux ifconfig 來調試 eth0網卡的地址

  1. [root@localhost ~]# Linux ifconfig  eth0 down   
  2. [root@localhost ~]# Linux ifconfig  eth0  192.168.1.99 broadcast 192.168.1.255  netmask 255.255.255.0  
  3. [root@localhost ~]# Linux ifconfig eth0 up   
  4. [root@localhost ~]# Linux ifconfig eth0   
  5. eth0      Link encap:Ethernet  HWaddr 00:11:00:00:11:11  
  6. inet addr:192.168.1.99  Bcast:192.168.1.255  Mask:255.255.255.0  
  7. UP BROADCAST MULTICAST  MTU:1500  Metric:1  
  8. RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
  9. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  

10. collisions:0 txqueuelen:1000  

11. RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)  

12. Interrupt:11 Base address:0x3400  

注解: 上面的例子我們解說一下;

第一行:Linux ifconfig eth0 down 表示如果eth0是激活的,就把它DOWN掉。此命令等同於 ifdown eth0;

第二行:用Linux ifconfig 來配置 eth0的IP地址、廣播地址和網絡掩碼;

第三行:用Linux ifconfig eth0 up 來激活eth0 ; 此命令等同於 ifup eth0

第四行:用 Linux ifconfig eth0 來查看 eth0的狀態;

當然您也可以用直接在指令IP地址、網絡掩碼、廣播地址的同時,激活網卡;要加up參數;比如下面的例子; [root@localhost ~]# Linux ifconfig  eth0  192.168.1.99 broadcast 192.168.1.255  netmask 255.255.255.0 up

實例二:在這個例子中,我們要學會設置網絡IP地址的同時,學會設置網卡的物理地址(MAC地址);

比如我們設置網卡eth1的IP地址、網絡掩碼、廣播地址,物理地址並且激活它; [root@localhost ~]# Linux 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 ~]# Linux ifconfig eth1    hw ether  00:11:00:00:11:22[root@localhost ~]# Linux ifconfig eth1 192.168.1.252   netmask 255.255.255.0 broadcast 192.168.1.255   up

其中 hw 后面所接的是網絡接口類型, ether表示乙太網, 同時也支持 ax25 、ARCnet、netrom等,詳情請查看 man Linux ifconfig ;

3.3 如何用Linux ifconfig 來配置虛擬網絡接口;

有時我們為了滿足不同的需要還需要配置虛擬網絡接口,比如我們用不同的IP地址來架運行多個HTTPD服務器,就要用到虛擬地址;這樣就省卻了同一個IP地址,如果開設兩個的HTTPD服務器時,要指定端口號。

虛擬網絡接口指的是為一個網絡接口指定多個IP地址,虛擬接口是這樣的 eth0:0 、 eth0:1、eth0:2 ... .. eth1N。當然您為eth1 指定多個IP地址,也就是 eth1:0、eth1:1、eth1:2 ... ...以此類推;

其實用Linux ifconfig 為一個網卡配置多個IP地址,就用前面我們所說的Linux ifconfig的用法,這個比較簡單;看下面的例子; [root@localhost ~]# Linux ifconfig eth1:0 192.168.1.251 hw ether  00:11:00:00:11:33   netmask 255.255.255.0 broadcast 192.168.1.255   up或[root@localhost ~]# Linux ifconfig eth1    hw ether  00:11:00:00:11:33[root@localhost ~]# Linux ifconfig eth1 192.168.1.251   netmask 255.255.255.0 broadcast 192.168.1.255   up

注意:指定時,要為每個虛擬網卡指定不同的物理地址;

在 Redhat/Fedora 或與Redhat/Fedora類似的系統,您可以把配置網絡IP地址、廣播地址、掩碼地址、物理地址以及激活網絡接口同時放在一個句子中,寫入/etc/rc.d/rc.local中。比如下面的例子;

Linux ifconfig eth1:0 192.168.1.250 hw ether  00:11:00:00:11:44   netmask 255.255.255.0 broadcast 192.168.1.255   up

Linux ifconfig eth1:1 192.168.1.249 hw ether  00:11:00:00:11:55   netmask 255.255.255.0 broadcast 192.168.1.255   up

解說:上面是為eth1的網絡接口,設置了兩個虛擬接口;每個接口都有自己的物理地址、IP地址... ...

3.4 如何用Linux ifconfig 來激活和終止網絡接口的連接;

激活和終止網絡接口的用 Linux ifconfig 命令,后面接網絡接口,然后加上 down或up參數,就可以禁止或激活相應的網絡接口了。當然也可以用專用工具ifup和ifdown 工具;

  1. [root@localhost ~]# Linux ifconfig eth0 down    
  2. [root@localhost ~]# Linux ifconfig eth0 up  
  3. [root@localhost ~]# ifup eth0   
  4. [root@localhost ~]# ifdown eth0  

對於激活其它類型的網絡接口也是如此,比如 ppp0,wlan0等;不過只是對指定IP的網卡有效。 注意:對DHCP自動分配的IP,還得由各個發行版自帶的網絡工具來激活;當然得安裝dhcp客戶端;這個您我們應該明白;比如Redhat/Fedora [root@localhost ~]#  /etc/init.d/network start Slackware 發行版; [root@localhost ~]# /etc/rc.d/rc.inet1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM