在linux系统添加ip别名

使用标准的网络配置工具:ifconfig和route命令  添加ip别名。

使用ifconfig命令查看该服务器的ip地址。示例服务器有一块物理网卡设备eth0和本地回环设备lo,eth0的ip地址为192.168.136.255,本地回环lo的IP地址为127.0.0.1.

本地回环代表设备的本地虚拟接口,所以默认被看做永远不会宕掉的接口。它的主要作用有两个:

一是测试本机的网络配置,能够ping通127.0.0.1说明本机的网卡和ip协议安装都没有问题。

二是某些server/client的应用程序在运行时必须调用服务器上的资源,一般要指定server的ip地址,但当该程序要在同一个机器上运行且没有别的server时,就可以把server的资源装在本机上,server的ip地址设为127.0.0.1也可以运行。

下面我们来查看本地的网卡设备信息:

root@ocsdb1[/root]#ifconfig -a
bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        inet 192.168.254.131  netmask 255.255.255.0  broadcast 192.168.254.255
        inet6 fe80::a00:27ff:feda:c3d5  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:da:c3:d5  txqueuelen 1000  (Ethernet)
        RX packets 2095  bytes 217367 (212.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 967  bytes 211369 (206.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s3: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 08:00:27:da:c3:d5  txqueuelen 1000  (Ethernet)
        RX packets 1831  bytes 198849 (194.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 967  bytes 211369 (206.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 08:00:27:da:c3:d5  txqueuelen 1000  (Ethernet)
        RX packets 264  bytes 18518 (18.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s9: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 193.169.1.1  netmask 255.255.255.0  broadcast 193.169.1.255
        inet6 fe80::a00:27ff:fe0a:d873  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:0a:d8:73  txqueuelen 1000  (Ethernet)
        RX packets 246  bytes 17412 (17.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 1008 (1008.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s10: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 193.169.2.1  netmask 255.255.255.0  broadcast 193.169.2.255
        inet6 fe80::a00:27ff:fecf:4b94  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:cf:4b:94  txqueuelen 1000  (Ethernet)
        RX packets 251  bytes 17712 (17.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 1008 (1008.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 522  bytes 38571 (37.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 522  bytes 38571 (37.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

我们下面对bond0添加一个ip别名:

root@ocsdb1[/root]#ifconfig bond0:1 192.168.254.133 broadcast 192.168.254.255 netmask 255.255.255.0 up
root@ocsdb1[/root]#route add -host 192.168.254.133 dev bond0:1

此时在ifconfig -a时在bond0上多了一个子设备bond0:1:

root@ocsdb1[/root]#ifconfig -a
bond0: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        inet 192.168.254.131  netmask 255.255.255.0  broadcast 192.168.254.255
        inet6 fe80::a00:27ff:feda:c3d5  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:da:c3:d5  txqueuelen 1000  (Ethernet)
        RX packets 2296  bytes 239627 (234.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1066  bytes 231353 (225.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

bond0:1: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        inet 192.168.254.133  netmask 255.255.255.0  broadcast 192.168.254.255
        ether 08:00:27:da:c3:d5  txqueuelen 1000  (Ethernet)

enp0s3: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 08:00:27:da:c3:d5  txqueuelen 1000  (Ethernet)
        RX packets 2026  bytes 220749 (215.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1066  bytes 231353 (225.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=6211<UP,BROADCAST,RUNNING,SLAVE,MULTICAST>  mtu 1500
        ether 08:00:27:da:c3:d5  txqueuelen 1000  (Ethernet)
        RX packets 270  bytes 18878 (18.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s9: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 193.169.1.1  netmask 255.255.255.0  broadcast 193.169.1.255
        inet6 fe80::a00:27ff:fe0a:d873  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:0a:d8:73  txqueuelen 1000  (Ethernet)
        RX packets 252  bytes 17772 (17.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 1008 (1008.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s10: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 193.169.2.1  netmask 255.255.255.0  broadcast 193.169.2.255
        inet6 fe80::a00:27ff:fecf:4b94  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:cf:4b:94  txqueuelen 1000  (Ethernet)
        RX packets 257  bytes 18072 (17.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 1008 (1008.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 541  bytes 40767 (39.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 541  bytes 40767 (39.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

此时通过其它服务器就可以ping通这个ip地址了:

root@ocsdb2[/root]#ping 192.168.254.133
PING 192.168.254.133 (192.168.254.133) 56(84) bytes of data.
64 bytes from 192.168.254.133: icmp_seq=1 ttl=64 time=0.276 ms
64 bytes from 192.168.254.133: icmp_seq=2 ttl=64 time=0.333 ms
^C
--- 192.168.254.133 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.276/0.304/0.333/0.033 ms
root@ocsdb2[/root]#

但是只通过ifconfig和route命令配置的ip别名在系统重启后就会消失。因此我们需要修改文件 /etc/rc.local,让ifconfig和routr,命令在系统开机时自动运行。

vi /etc/rc.local

在文件中写入

ifconfig bond0:1 192.168.254.133 broadcast 192.168.254.255 netmask 255.255.255.0 up
route add -host 192.168.254.133 dev bond0:1

保存退出即可。

参考自:https://blog.csdn.net/ldwtill/article/details/8525835
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值