【Linux命令015】ssh + ipaddr,远程修改ip

Scroll Down

前言

在做mysql集群mha的时候,需要做个虚拟ip,进行故障转移工作

命令

1、查看ip信息

ip addr show、ip a show,这两个命令效果一样的

[root@yaomm ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:16:3e:16:98:69 brd ff:ff:ff:ff:ff:ff
    inet 172.17.102.209/20 brd 172.17.111.255 scope global dynamic eth0
       valid_lft 307481042sec preferred_lft 307481042sec
3: br-01593e0f6789: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:0a:12:96:0e brd ff:ff:ff:ff:ff:ff
    inet 172.19.0.1/16 scope global br-01593e0f6789
       valid_lft forever preferred_lft forever
4: docker_gwbridge: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:68:3d:26:1d brd ff:ff:ff:ff:ff:ff
    inet 172.20.0.1/16 scope global docker_gwbridge
       valid_lft forever preferred_lft forever
5: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:83:4e:3a:ba brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
71: vethf1f6f06@if70: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker_gwbridge state UP group default 
    link/ether 26:6e:b8:31:89:63 brd ff:ff:ff:ff:ff:ff link-netnsid 9

2、添加虚拟ip

ip addr add 10.0.0.55 dev eth0

3、查看缩略ip信息

ip -o -f inet addr show

[root@yaomm ~]# ip -o -f inet addr show
1: lo    inet 127.0.0.1/8 scope host lo\       valid_lft forever preferred_lft forever
2: eth0    inet 172.17.102.209/20 brd 172.17.111.255 scope global dynamic eth0\       valid_lft 307480136sec preferred_lft 307480136sec
2: eth0    inet 10.0.0.55/32 scope global eth0\       valid_lft forever preferred_lft forever
3: br-01593e0f6789    inet 172.19.0.1/16 scope global br-01593e0f6789\       valid_lft forever preferred_lft forever
4: docker_gwbridge    inet 172.20.0.1/16 scope global docker_gwbridge\       valid_lft forever preferred_lft forever
5: docker0    inet 172.18.0.1/16 scope global docker0\       valid_lft forever preferred_lft forever

4、删除ip

# 删除ip
ip addr del 10.0.0.55 dev eth0

# 查看ip
ip -o -f inet addr show

注意:删除网卡主ip地址,会删除该网卡的所有ip地址;反之则不会。

5、远程设置ip

在mysql集群故障的时候,mha脚本在检测到故障时,会,用ssh的方式给主节点添加vip,命令如下

ssh root@172.31.145.38 /usr/sbin/ip addr add 172.31.145.200 brd 172.31.159.255 dev eth0 label eth0:0;/usr/sbin/arping -q -A -c 1 -I eth0 172.31.145.200;iptables -F;

其他几种写法,如写法2:

my $vip = '10.0.0.66/24';
my $key = '2';

…………

# 在sub main 函数下添加内容
sub drop_vip {
    my $output = `ssh -o ConnectTimeout=15  -o ConnectionAttempts=3 $orig_master_host /sbin/ip addr del $vip/32 dev $key`;
}

sub add_vip {
    my $output = `ssh -o ConnectTimeout=15  -o ConnectionAttempts=3 $new_master_host /sbin/ip addr add $vip/32 dev $key`;
}

写法3:

这个是ipconfig命令设置。

# 在my函数前声明
my $vip = '10.0.0.66/24';
my $key = '2';
my $ssh_start_vip = "/sbin/ifconfig eno16777736:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eno16777736:$key down";


# 在sub main 函数下添加内容
sub start_vip() {
 `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}

sub stop_vip() {
     return 0 unless ($ssh_user);
 `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}

后记

ipconfig也可以设置ip,并且ip命令的作用远不止于此。

需要注意的是,ipconfig设置的ip,ip addr show 可以查看到,而ip addr add命令设置的ip,ipconfig则查不到。