Linux ifconfig 配置网络接口
Linux ifconfig 可以用來配置網(wǎng)絡(luò)接口的IP地址、掩碼、網(wǎng)關(guān)、物理地址等;值得一說的是用Linux ifconfig 為網(wǎng)卡指定IP地址,這只是用來調(diào)試網(wǎng)絡(luò)用的,并不會更改系統(tǒng)關(guān)于網(wǎng)卡的配置文件。
如果您想把網(wǎng)絡(luò)接口的IP地址固定下來,目前有三個方法:一是通過各個發(fā)行和版本專用的工具來修改IP地址;二是直接修改網(wǎng)絡(luò)接口的配置文件;三是修改特定的文件,加入Linux ifconfig 指令來指定網(wǎng)卡的IP地址,比如在redhat或Fedora中,把Linux ifconfig 的語名寫入/etc/rc.d/rc.local文件中;
Linux ifconfig 配置網(wǎng)絡(luò)端口的方法: Linux ifconfig 工具配置網(wǎng)絡(luò)接口的方法是通過指令的參數(shù)來達(dá)到目的的,我們只說最常用的參數(shù); Linux ifconfig? 網(wǎng)絡(luò)端口? IP地址??? hw <HW>? MAC地址? netmask? 掩碼地址??? broadcast? 廣播地址?? [up/down]
實(shí)例一:
比如我們用Linux ifconfig 來調(diào)試 eth0網(wǎng)卡的地址
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地址、廣播地址和網(wǎng)絡(luò)掩碼;
第三行:用Linux ifconfig eth0 up 來激活eth0 ; 此命令等同于 ifup eth0
第四行:用 Linux ifconfig eth0 來查看 eth0的狀態(tài);
當(dāng)然您也可以用直接在指令I(lǐng)P地址、網(wǎng)絡(luò)掩碼、廣播地址的同時,激活網(wǎng)卡;要加up參數(shù);比如下面的例子; [root@localhost ~]# Linux ifconfig? eth0? 192.168.1.99 broadcast 192.168.1.255? netmask 255.255.255.0 up
實(shí)例二:在這個例子中,我們要學(xué)會設(shè)置網(wǎng)絡(luò)IP地址的同時,學(xué)會設(shè)置網(wǎng)卡的物理地址(MAC地址);
比如我們設(shè)置網(wǎng)卡eth1的IP地址、網(wǎng)絡(luò)掩碼、廣播地址,物理地址并且激活它; [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 后面所接的是網(wǎng)絡(luò)接口類型, ether表示乙太網(wǎng), 同時也支持 ax25 、ARCnet、netrom等,詳情請查看 man Linux ifconfig ;
3.3 如何用Linux ifconfig 來配置虛擬網(wǎng)絡(luò)接口;
有時我們?yōu)榱藵M足不同的需要還需要配置虛擬網(wǎng)絡(luò)接口,比如我們用不同的IP地址來架運(yùn)行多個HTTPD服務(wù)器,就要用到虛擬地址;這樣就省卻了同一個IP地址,如果開設(shè)兩個的HTTPD服務(wù)器時,要指定端口號。
虛擬網(wǎng)絡(luò)接口指的是為一個網(wǎng)絡(luò)接口指定多個IP地址,虛擬接口是這樣的 eth0:0 、 eth0:1、eth0:2 ... .. eth1N。當(dāng)然您為eth1 指定多個IP地址,也就是 eth1:0、eth1:1、eth1:2 ... ...以此類推;
其實(shí)用Linux ifconfig 為一個網(wǎng)卡配置多個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
注意:指定時,要為每個虛擬網(wǎng)卡指定不同的物理地址;
在 Redhat/Fedora 或與Redhat/Fedora類似的系統(tǒng),您可以把配置網(wǎng)絡(luò)IP地址、廣播地址、掩碼地址、物理地址以及激活網(wǎng)絡(luò)接口同時放在一個句子中,寫入/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的網(wǎng)絡(luò)接口,設(shè)置了兩個虛擬接口;每個接口都有自己的物理地址、IP地址... ...
3.4 如何用Linux ifconfig 來激活和終止網(wǎng)絡(luò)接口的連接;
激活和終止網(wǎng)絡(luò)接口的用 Linux ifconfig 命令,后面接網(wǎng)絡(luò)接口,然后加上 down或up參數(shù),就可以禁止或激活相應(yīng)的網(wǎng)絡(luò)接口了。當(dāng)然也可以用專用工具ifup和ifdown 工具;
對于激活其它類型的網(wǎng)絡(luò)接口也是如此,比如 ppp0,wlan0等;不過只是對指定IP的網(wǎng)卡有效。 注意:對DHCP自動分配的IP,還得由各個發(fā)行版自帶的網(wǎng)絡(luò)工具來激活;當(dāng)然得安裝dhcp客戶端;這個您我們應(yīng)該明白;比如Redhat/Fedora [root@localhost ~]#? /etc/init.d/network start Slackware 發(fā)行版; [root@localhost ~]# /etc/rc.d/rc.inet1
轉(zhuǎn)載于:https://www.cnblogs.com/fanweisheng/p/11109277.html
總結(jié)
以上是生活随笔為你收集整理的Linux ifconfig 配置网络接口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 盒模型应用
- 下一篇: Sqoop数据迁移工具的使用