Linux單網卡多個IP(或者多個網卡多個IP)設定

來源:互聯網
上載者:User

 Redhat Linux下

修改ip:
編輯檔案/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0 //裝置名稱,不要修改
BOOTPROTO=static //不要修改
BROADCAST=10.10.22.255 //廣播位址,一般為本網段的最後一個IP
IPADDR=10.10.22.145 //ip地址
NETMASK=255.255.255.0 //子網路遮罩
NETWORK=10.10.22.0 //網段地址
ONBOOT=yes //不要修改
TYPE=Ethernet //不要修改
注意: ifcfg-eth0是第一張網卡,ifcfg-eth1是第二張網卡,依次類推

增加ip:
可以重新編譯一個檔案,檔案名稱為/etc/sysconfig/network-scripts/ifcfg-eth0:0
也可以直接在/etc/sysconfig/network-scripts/ifcfg-eth0中添加(但是裝置eth0一定要有)
DEVICE=eth0:0 //裝置名稱,不要修改
BOOTPROTO=static //不要修改
BROADCAST=10.10.33.255 //廣播位址,一般為本網段的最後一個IP
IPADDR=10.10.33.145 //ip地址
NETMASK=255.255.255.0 //子網路遮罩
NETWORK=10.10.33.0 //網段地址
ONBOOT=yes //不要修改
TYPE=Ethernet //不要修改

依次類推,如果再增加一個ip,則再增加如下配置:
DEVICE=eth0:1 //裝置名稱,不要修改
BOOTPROTO=static //不要修改
BROADCAST=10.10.44.255 //廣播位址,一般為本網段的最後一個IP
IPADDR=10.10.44.145 //ip地址
NETMASK=255.255.255.0 //子網路遮罩
NETWORK=10.10.44.0 //網段地址
ONBOOT=yes //不要修改
TYPE=Ethernet //不要修改

同樣如果在第二張網卡,則需要增加:
DEVICE=eth1:0 //裝置名稱,不要修改
BOOTPROTO=static //不要修改
BROADCAST=10.10.33.255 //廣播位址,一般為本網段的最後一個IP
IPADDR=10.10.33.145 //ip地址
NETMASK=255.255.255.0 //子網路遮罩
NETWORK=10.10.33.0 //網段地址
ONBOOT=yes //不要修改
TYPE=Ethernet //不要修改

 

增加預設閘道的方法:
注意一台機器只能有一個預設網關,否則就應該給出具體的路由方式。
在相關的裝置配置中增加一項即可,例如在上述配置中,在第一張網卡的第一個IP增加一個預設網關:
DEVICE=eth0
BOOTPROTO=static
BROADCAST=10.10.22.255
IPADDR=10.10.22.145
NETMASK=255.255.255.0
NETWORK=10.10.22.0
ONBOOT=yes
TYPE=Ethernet
GATEWAY=10.10.22.3

修改完成後,要想使上述修改生效,則需要重起機器,或者重起網路。
重新啟動機器的方法是:reboot
重起網路的方法是:service network restart

以上為靜態增加ip的方法,即機器重起後仍然有效方法。

動態方法
注意:所有操作均使用root使用者
修改IP:
ifconfig eth0 10.10.22.145
則直接將第一張網卡的IP修改成10.10.22.145

增加IP:
ifconfig eth0 add 10.10.33.145 //增加一個IP
ifconfig eth0:0 broadcast 10.10.33.255 //修改剛剛增加IP的廣播位址

再增加一個IP:
ifconfig eth0:0 add 10.10.44.145
ifconfig eth0:0:1 broadcast 10.10.44.255 //修改剛剛增加IP的廣播位址
千萬不要如下操作:
ifconfig eth0 add 10.10.44.145
這樣就把剛剛加的IP10.10.33.145修改成了10.10.44.145

再增加一個IP:
ifconfig eth0:0:1 add 10.10.55.145
ifconfig eth0:0:1:1 broadcast 10.10.55.255 //修改剛剛增加IP的廣播位址

 

Debian下

http://wiki.debian.org/NetworkConfiguration

1)Debian下一個網卡綁定多個ip的方法和添加路由

修改/etc/network/interfaces
auto eth0
iface eth0 inet static
address 172.16.3.123
netmask 255.255.255.0
network 172.16.3.0
broadcast 172.16.3.255
gateway 172.16.3.1

auto eth0:1
iface eth0:1 inet static
address 10.16.3.123
netmask 255.255.0.0
network 10.16.0.0
broadcast 10.16.255.255
修改/etc/network/ifstate
lo=lo
eth0=eth0
eth0:1=eth0:1
然後/etc/init.d/networking restart就可以了。

 

 在debian中添加一條路由,
 方法一:修改/etc/network/interfaces
代碼:
auto eth0
iface eth0 inet static
        address 172.16.3.222
        netmask 255.255.0.0
        network 172.16.0.0
        broadcast 172.16.255.255
        gateway 172.16.2.1
   up route add -net 192.168.0.0 netmask 255.255.255.0 gw 172.16.0.1
   down route del -net 192.168.0.0 netmask 255.255.255.0 gw 172.16.0.1

方法二:在/etc/network/if-up.d目錄下建立一個簡單的指令檔,如static-route$(記得以$符號結尾,要不有個run-parts會跑出來告訴你一些東西)指令碼最簡單的就好啦,如:
代碼:
#!/bin/bash
route add -net 192.168.0.0 netmask 255.255.255.0 gw 172.16.0.1
嘿嘿,你也可以猜到/etc/network/目錄下的其他目錄的作用了吧。
發覺在debian中這個route的設定其實只是它的那些設定檔的一個比較簡單的應用而已,你完全可以做更複雜的應用。

2)如果是多網卡想配置不同的IP,同樣也很簡單

 複製eth0下所有的IP設定,拷貝到interface檔案下面,將eth0都改為eth1。並依次類推。最後重啟網路就可以了

 解析不了的Host Name ,修改/etc/hosts

---------------------------

下面是一個Debian 從DHCP伺服器自動獲得IP地址的樣本:

# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)# The loopback interfaceauto loiface lo inet loopback# The first network card - this entry was created during the Debian installation# (network, broadcast and gateway are optional)auto eth0iface eth0 inet dhcp
----------------------
Using DHCP to automatically configure the interface

If you're just using DHCP then all you need is something like:

    auto eth0    allow-hotplug eth0    iface eth0 inet dhcp
Configuring the interface manually

If you're configuring it manually then something like this will set the default gateway (network, broadcast and gateway are optional):

    auto eth0    iface eth0 inet static        address 192.168.0.7        netmask 255.255.255.0        gateway 192.168.0.254

If you want to add an IPv6 address, too, append something like:

    iface eth0 inet6 static        address 2001:db8::c0ca:1eaf        netmask 64        gateway 2001:db8::1ead:ed:beef
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.