2013年3月1日
为IP添加别名
一、查看状态
[root@desktop7 ~]# ifconfig
br0 Link encap:Ethernet HWaddr 50:46:5D:71:C2:F1
inet addr:192.168.0.7 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::5246:5dff:fe71:c2f1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:27002 errors:0 dropped:0 overruns:0 frame:0
TX packets:6410 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:33859254 (32.2 MiB) TX bytes:513684 (501.6 KiB)
eth0 Link encap:Ethernet HWaddr 50:46:5D:71:C2:F1
inet6 addr: fe80::5246:5dff:fe71:c2f1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1564063 errors:0 dropped:0 overruns:0 frame:0
TX packets:392416 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2157659980 (2.0 GiB) TX bytes:26901971 (25.6 MiB)
Interrupt:233 Base address:0x6000
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:33670 errors:0 dropped:0 overruns:0 frame:0
TX packets:33670 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:130529766 (124.4 MiB) TX bytes:130529766 (124.4 MiB)
virbr0 Link encap:Ethernet HWaddr FE:52:00:48:13:31
inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:692 errors:0 dropped:0 overruns:0 frame:0
TX packets:75 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:76119 (74.3 KiB) TX bytes:11775 (11.4 KiB)
vnet0 Link encap:Ethernet HWaddr FE:52:00:48:13:31
inet6 addr: fe80::fc52:ff:fe48:1331/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:692 errors:0 dropped:0 overruns:0 frame:0
TX packets:13836 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:85819 (83.8 KiB) TX bytes:728262 (711.1 KiB)
vnet1 Link encap:Ethernet HWaddr FE:54:00:00:00:6B
inet6 addr: fe80::fc54:ff:fe00:6b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:258 errors:0 dropped:0 overruns:0 frame:0
TX packets:3093 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:26456 (25.8 KiB) TX bytes:483828 (472.4 KiB)
vnet2 Link encap:Ethernet HWaddr FE:54:00:00:00:CF
inet6 addr: fe80::fc54:ff:fe00:cf/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:241 errors:0 dropped:0 overruns:0 frame:0
TX packets:3017 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:24669 (24.0 KiB) TX bytes:472463 (461.3 KiB)
二、添加别名
1、用ifconfig添加 IP 别名
[root@desktop7 ~]# ifconfig eth0:1 192.168.2.1 broadcast 192.168.0.7 netmask 255.255.255.0 up
2、设置路由
[root@desktop7 ~]# route add -host 192.168.2.1 dev eth0:1
3、查看设置状况
[root@desktop7 ~]# ifconfig
br0 Link encap:Ethernet HWaddr 50:46:5D:71:C2:F1
inet addr:192.168.0.7 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::5246:5dff:fe71:c2f1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:29549 errors:0 dropped:0 overruns:0 frame:0
TX packets:7008 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:36108533 (34.4 MiB) TX bytes:557643 (544.5 KiB)
eth0 Link encap:Ethernet HWaddr 50:46:5D:71:C2:F1
inet6 addr: fe80::5246:5dff:fe71:c2f1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1566606 errors:0 dropped:0 overruns:0 frame:0
TX packets:393031 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2159945517 (2.0 GiB) TX bytes:26949905 (25.7 MiB)
Interrupt:233 Base address:0x6000
eth0:1 Link encap:Ethernet HWaddr 50:46:5D:71:C2:F1
inet addr:192.168.2.1 Bcast:192.168.0.7 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:233 Base address:0x6000
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:33670 errors:0 dropped:0 overruns:0 frame:0
TX packets:33670 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:130529766 (124.4 MiB) TX bytes:130529766 (124.4 MiB)
virbr0 Link encap:Ethernet HWaddr FE:52:00:48:13:31
inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:747 errors:0 dropped:0 overruns:0 frame:0
TX packets:81 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:82101 (80.1 KiB) TX bytes:12821 (12.5 KiB)
vnet0 Link encap:Ethernet HWaddr FE:52:00:48:13:31
inet6 addr: fe80::fc52:ff:fe48:1331/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:747 errors:0 dropped:0 overruns:0 frame:0
TX packets:14828 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:92571 (90.4 KiB) TX bytes:780580 (762.2 KiB)
vnet1 Link encap:Ethernet HWaddr FE:54:00:00:00:6B
inet6 addr: fe80::fc54:ff:fe00:6b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:260 errors:0 dropped:0 overruns:0 frame:0
TX packets:4252 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:26684 (26.0 KiB) TX bytes:855793 (835.7 KiB)
vnet2 Link encap:Ethernet HWaddr FE:54:00:00:00:CF
inet6 addr: fe80::fc54:ff:fe00:cf/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:243 errors:0 dropped:0 overruns:0 frame:0
TX packets:4176 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:24897 (24.3 KiB) TX bytes:844428 (824.6 KiB)
三、验证
1、SSH本机虚拟机
[root@desktop7 ~]# ssh 192.168.0.230
The authenticity of host '192.168.0.230 (192.168.0.230)' can't be established.
RSA key fingerprint is fc:3b:67:f0:66:44:a5:2f:84:c4:e0:b6:74:4d:ff:93.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.230' (RSA) to the list of known hosts.
[email protected]'s password:
Last login: Fri Mar 1 03:15:21 2013
2、查看虚拟机IP
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 52:54:00:00:00:6B
inet addr:192.168.0.230 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::5054:ff:fe00:6b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5429 errors:0 dropped:0 overruns:0 frame:0
TX packets:450 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1146759 (1.0 MiB) TX bytes:47530 (46.4 KiB)
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:1580 errors:0 dropped:0 overruns:0 frame:0
TX packets:1580 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:4868050 (4.6 MiB) TX bytes:4868050 (4.6 MiB)
3、PING新IP 192.168.2.1
[root@localhost ~]# ping 192.168.2.1
PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.
64 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=1.63 ms
From 192.168.0.254: icmp_seq=2 Redirect Host(New nexthop: 192.168.2.1)
64 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.328 ms
From 192.168.0.254: icmp_seq=3 Redirect Host(New nexthop: 192.168.2.1)
64 bytes from 192.168.2.1: icmp_seq=3 ttl=64 time=0.314 ms
64 bytes from 192.168.2.1: icmp_seq=4 ttl=64 time=0.247 ms
64 bytes from 192.168.2.1: icmp_seq=5 ttl=64 time=0.256 ms
64 bytes from 192.168.2.1: icmp_seq=6 ttl=64 time=0.217 ms
64 bytes from 192.168.2.1: icmp_seq=7 ttl=64 time=0.602 ms
64 bytes from 192.168.2.1: icmp_seq=8 ttl=64 time=0.216 ms
64 bytes from 192.168.2.1: icmp_seq=9 ttl=64 time=0.206 ms
64 bytes from 192.168.2.1: icmp_seq=10 ttl=64 time=0.185 ms
--- 192.168.2.1 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9002ms
rtt min/avg/max/mdev = 0.185/0.420/1.631/0.419 ms
再PING 192.168.2.1
[root@localhost ~]# ping 192.168.2.1
PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.
64 bytes from 192.168.2.1: icmp_seq=1 ttl=64 time=0.176 ms
64 bytes from 192.168.2.1: icmp_seq=2 ttl=64 time=0.250 ms
64 bytes from 192.168.2.1: icmp_seq=3 ttl=64 time=0.211 ms
64 bytes from 192.168.2.1: icmp_seq=4 ttl=64 time=0.213 ms
64 bytes from 192.168.2.1: icmp_seq=5 ttl=64 time=0.238 ms
64 bytes from 192.168.2.1: icmp_seq=6 ttl=64 time=0.173 ms
--- 192.168.2.1 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5000ms
rtt min/avg/max/mdev = 0.173/0.210/0.250/0.029 ms
[root@localhost ~]#
四、永久生效,配置文件
[root@localhost ~]# vi /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
(增加以下两行内容)
ifconfig eth0:1 192.168.2.1 broadcast 192.168.0.7 netmask 255.255.255.0 up
route add -host 192.168.2.1 dev eth0:1