linux配置基於ip的虛擬主機
阿新 • • 發佈:2018-12-31
技術QQ交流群:294088839
檢視自己的ip資訊:
- [root@slave5 conf]# ifconfig
- eth0 Link encap:Ethernet HWaddr E0:CB:4E:D0:EC:B2
- inet addr:192.168.30.149 Bcast:192.168.30.255 Mask:255.255.255.0
- inet6 addr: fe80::e2cb:4eff:fed0:ecb2/64 Scope:Link
-
UP BROADCAST RUNNING MULTICAST MTU:1500
- RX packets:48458926 errors:0 dropped:0 overruns:0 frame:0
- TX packets:40842760 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:1562626110 (1.4 GiB) TX bytes:330009772 (314.7 MiB)
- Interrupt:233 Base address:0xe000
-
lo 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:25711326 errors:0 dropped:0 overruns:0 frame:0
- TX packets:25711326 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:0
-
RX bytes:2575592225
eth0和本地迴環裝置lo,127.0.0.1,通常被稱為本地迴環地址(Loop back address),不屬於任何一個有類別地址類。它代表裝置的本地虛擬介面,所以預設被看作是永遠不會宕掉的介面。在windows作業系統中也有相似的定義,所以通常在不安裝網絡卡前就可以ping通這個本地迴環地址。一般都會用來檢查本地網路協議、基本資料介面等是否正常的。
在基礎上增加一個ip別名
- [root@slave5 conf]# ifconfig eth0:1192.168.30.121 broadcast 192.168.30.255 netmask 255.255.255.0 up
- [root@slave5 conf]# route add -host 192.168.30.121 dev eth0:1
檢視ifconfig
- [root@slave5 conf]# ifconfig
- eth0 Link encap:Ethernet HWaddr E0:CB:4E:D0:EC:B2
- inet addr:192.168.30.149 Bcast:192.168.30.255 Mask:255.255.255.0
- inet6 addr: fe80::e2cb:4eff:fed0:ecb2/64 Scope:Link
- UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
- RX packets:48460536 errors:0 dropped:0 overruns:0 frame:0
- TX packets:40843834 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:1000
- RX bytes:1562794737 (1.4 GiB) TX bytes:330131620 (314.8 MiB)
- Interrupt:233 Base address:0xe000
- eth0:1 Link encap:Ethernet HWaddr E0:CB:4E:D0:EC:B2
- inet addr:192.168.30.121 Bcast:192.168.30.255 Mask:255.255.255.0
- UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
- Interrupt:233 Base address:0xe000
- lo 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:25711330 errors:0 dropped:0 overruns:0 frame:0
- TX packets:25711330 errors:0 dropped:0 overruns:0 carrier:0
- collisions:0 txqueuelen:0
- RX bytes:2575592673 (2.3 GiB) TX bytes:2575592673 (2.3 GiB)
用另外一臺主機,如果能平通說明成功。
- [root@slave5 ~]# ping 192.168.30.123
- PING 192.168.30.123 (192.168.30.123) 56(84) bytes of data.
- 64 bytes from 192.168.30.123: icmp_seq=1 ttl=64 time=0.018 ms
- 64 bytes from 192.168.30.123: icmp_seq=2 ttl=64 time=0.011 ms
- 64 bytes from 192.168.30.123: icmp_seq=3 ttl=64 time=0.009 ms
- 64 bytes from 192.168.30.123: icmp_seq=4 ttl=64 time=0.008 ms
要注意的是,上述配置資訊重啟後會消失,如何配置不消失資訊。如下
將ifconfig和rout命令新增到/etc/rc.local檔案中
- vi /etc/rc.local
在檔案末尾叫上如下內容:
- [root@slave5 conf]# ifconfig eth0:1192.168.30.121 broadcast 192.168.30.255 netmask 255.255.255.0 up
- [root@slave5 conf]# route add -host 192.168.30.121 dev eth0:1