在实际运维中,防火墙把自己挡在机器外面的情况会时有发生。如何快速的恢复对运维人员是很重要的。

本文将介绍如何用Azure Extension实现不通过ssh对VM进行操作的方法。

之前写过一遍Blog介绍如何部署Azure的CustomScriptExtension:

http://www.cnblogs.com/hengwei/p/5862200.html

在CustomScriptExtension的基础上,如果实现关闭防火墙。

1.添加防火墙规则

通过添加iptables规则关闭外部访问该VM的ssh:

iptables -A INPUT -p tcp --dport  -j DROP

所有22端口都被关闭了。

通过psping进行观察VM 22端口的情况:

psping -t 139.219.237.69:
Connecting to 139.219.237.69:: .94ms
Connecting to 139.219.237.69:: .50ms
Connecting to 139.219.237.69:: .93ms
Connecting to 139.219.237.69:: .51ms
Connecting to 139.219.237.69:: .42ms
Connecting to 139.219.237.69:: .54ms
Connecting to 139.219.237.69:: .16ms
Connecting to 139.219.237.69:: This operation returned because the timeout period expired.
Connecting to 139.219.237.69:: This operation returned because the timeout period expired.
Connecting to 139.219.237.69:: This operation returned because the timeout period expired.
Connecting to 139.219.237.69:: This operation returned because the timeout period expired.

为防止出现脚本不成功,导致再不能访问VM的情况,执行如下脚本,过5分钟自动去除防火墙:

[root@hwcentos ~]#./remove_iptables.sh &
#!/bin/bash
while true
  do
  sleep
  iptables -F
  echo `date` >> a.txt
done

2.通过CustomScriptExtension去除防火墙规则

运行PowerShell脚本:

$mycred = Get-Credential -UserName admin@xxx.partner.onmschina.cn -Message hello
Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $mycred
 
#定义Resource Group、VM和Location变量
$RGName = 'hwextensiontest'
$VmName = 'hwcentos'
$Location = 'China East'
 
#定义Extension相关信息
$ExtensionName = 'CustomScriptForLinux'
$Publisher = 'Microsoft.OSTCExtensions'
$version = '1.5'
 
$PublicConf = '{"commandToExecute": "iptables -F"}' 
 
#执行Set-AzureRmVMExtension命令,安装extension:
Set-AzureRmVMExtension -ResourceGroupName $RGName -VMName $VmName -Location $Location `
  -Name $ExtensionName -Publisher $Publisher `
  -ExtensionType $ExtensionName -TypeHandlerVersion $Version `
-Settingstring $PublicConf RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
True OK OK

通过iptables -F的命令关闭所有的防火墙。

PsPing的输出结果如下:

Connecting to 139.219.237.69:: This operation returned because the timeout period expired.
Connecting to 139.219.237.69:: This operation returned because the timeout period expired.
Connecting to 139.219.237.69:: This operation returned because the timeout period expired.
Connecting to 139.219.237.69:: This operation returned because the timeout period expired.
Connecting to 139.219.237.69:: This operation returned because the timeout period expired.
Connecting to 139.219.237.69:: .12ms
Connecting to 139.219.237.69:: .16ms
Connecting to 139.219.237.69:: .64ms

这时已经可以通过ssh登录这台VM。

观察extension的日志:

[root@hwcentos 1.5.2.0]# pwd
/var/log/azure/Microsoft.OSTCExtensions.CustomScriptForLinux/1.5.2.0
[root@hwcentos 1.5.2.0]# less extension.log
..........
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Config decoded correctly.
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Will try to download files, number of retries = , wait SECONDS between retrievals = 20s
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Command to execute:iptables -F
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]fileUris value provided is empty or invalid. Continue with executing command...
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Succeeded to download files, retry count =
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Internal DNS is ready, retry count =
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Command to execute:iptables -F
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Command is finished.
// :: ---stdout---
// ::
// :: ---errout---
// ::
// ::
// :: [Microsoft.OSTCExtensions.CustomScriptForLinux-1.0]Daemon,success,,Command is finished.
// :: ---stdout---
// ::
// :: ---errout---
// ::

查看防火墙状态:

[root@hwcentos ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination Chain FORWARD (policy ACCEPT)
target prot opt source destination Chain OUTPUT (policy ACCEPT)
target prot opt source destination

防火墙已经关闭。

3.延展

有时客户VM没有响应的原因并不是因为防火墙,但原因并不是防火墙。此时我们可以通过收集sosreport的信息,通过复制到其他VM等方法获得这个文件后,进行分析。

当然也可以通过ping其它VM,并在另外一台VM上抓包,看这台VM是否网络存活,以进行下一步的分析和动作。

4.需要注意的问题

当多次执行CustomScript时,这个extension会判断内容是否相同,如果相同extension会认为是重复执行,脚本将不再执行。

更新:

Azure VM的Extension的通讯机制是通过HTTPS对外发起的。如下的输出:

[root@hwcentos ~]# netstat -tunp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 10.3.0.4: 167.220.255.53: SYN_RECV -
tcp 10.3.0.4: 168.63.129.16: TIME_WAIT -
tcp 10.3.0.4: 40.126.88.72: TIME_WAIT -
tcp 10.3.0.4: 167.220.255.53: ESTABLISHED /sshd
tcp 10.3.0.4: 40.126.88.72: TIME_WAIT -
tcp 10.3.0.4: 40.126.88.72: TIME_WAIT -
tcp 10.3.0.4: 40.126.88.72: TIME_WAIT -
tcp 10.3.0.4: 168.63.129.16: TIME_WAIT -

可以看到VM上很多到40.x.x.x的https请求。只要VM可以访问外部的HTTPS,VM Extension就可以工作。

所以,我们的INPUT方向的iptables添加:

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT  

iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT

可以确保出方向的流量不被防火墙挡住。

在添加如下防火墙规则后,VM extension仍然可以把iptables的规则清除:

iptables -A INPUT -s / -j DROP

Azure上Linux VM误配防火墙的恢复方法的更多相关文章

  1. Windows Azure 上 Linux VM 中的交换空间 – 第 2 部分

    本文章由 Azure CAT 团队的 Piyush Ranjan (MSFT) 撰写. 在前一篇文章 Windows Azure 上Linux VM 中的交换空间第 1 部分中,我介绍了在默认情况下, ...

  2. Azure上Linux VM防DDOS攻击:使用Apache mod_evasive

    部署在云端的虚拟机和web服务,很容易受到DoS护着DDoS的服务攻击,让一些新上线的业务苦不堪言,当然各个云服务提供商也有不同层面DDOS的防护,然而由于防护粒度,攻击复杂度的关系,未必可以满足你的 ...

  3. Azure上Linux VM DDOS攻击预防: 慢速攻击

    在上篇博客(http://www.cnblogs.com/cloudapps/p/4996046.html)中,介绍了如何使用Apache的模块mod_evasive进行反DDOS攻击的设置,在这种模 ...

  4. 在Windows Azure上配置VM主备切换(1)——Linux篇

    对任何一个上线系统来说,高可用设计是不可或缺的一个环节,这样才可以确保应用可以持续.稳定的运行,而不是频繁的掉线.停机.高可用设计的核心思路很简单,就是消除一切单点故障,将单点链路或者节点升级为多点. ...

  5. 对连接到 Azure 中 Linux VM 时出现的问题进行详细的 SSH 故障排除的步骤

    有许多可能的原因会导致 SSH 客户端无法访问 VM 上的 SSH 服务. 如果已经执行了较常规的 SSH 故障排除步骤,则需要进一步排查连接问题. 本文指导用户完成详细的故障排除步骤,以确定 SSH ...

  6. 在Azure上的VM镜像库中找到想要的镜像

    Azure上的虚机镜像库中, 有很多的镜像,其中当然也包括了用户自定义上传的镜像. 在Powershell中如果想使用这些镜像的话, 则需要知道其名称 下面这条命令,可以获得所有的镜像信息 $imag ...

  7. Azure上每个VM多个IP地址

    Azure的每个VM都有多种IP地址,包括DIP.VIP和PIP.具体如下: DIP地址是在VM里能够看到的IP地址,即私网地址:PIP地址是这个VM关联的公网IP地址,即公网地址:VIP地址是负载均 ...

  8. 转://Linux下误删除/home目录的恢复方法

    一般情况下,我们在安装Oracle数据库的时候,都会创建一个Oracle用户,用该用户来安装和管理Oracle.Oracle用户的根目录就是/home/oracle. 通常安装Oracle数据库是按照 ...

  9. Azure上Linux虚拟机Mac地址的持久化

    有些用户在使用Azure Linux 虚拟机安装软件时,有些软件的license会和当前系统的mac地址绑定,那么在Azure VM重启,reszie(改变尺寸大小),停止然后再启动的时候,虚拟机的M ...

随机推荐

  1. list— 把数组中的值赋给一组变量

    (PHP 4, PHP 5, PHP 7) list — 把数组中的值赋给一组变量 array list ( mixed $var1 [, mixed $... ] ) 像 array() 一样,这不 ...

  2. MapReduce-二进制输入

    Hadoop的MapReduce不只是可以处理文本信息,它还可以处理二进制格式的数据1. 关于SequenceFileInputFormat类Hadoop的顺序文件格式存储二进制的键/值对的序列.由于 ...

  3. Apache Phoenix的子查询

    Phoenix现在支持在WHERE 和FROM 中使用子查询.子查询可以被指定在很多地方,比如 IN/NOT IN, EXISTS/NOTEXISTS等. Subqueries with INor N ...

  4. JavaScript -- 节点操作, 事件触发, 表单伸缩

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 【转载】IntelliJ IDEA WEB项目的部署配置

    最近使用了一下IDEA,确实强大.在部署时出现了些问题.看了这篇文章,对ieda的一些部署配置有了些许了解,在此感谢原博.原文链接:http://blog.csdn.net/z69183787/art ...

  6. Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等

    实现两个整数的加减乘除,在每个方法执行前后打印日志. ArithmeticCalculator.java: package spring.aop.impl.xml; public interface ...

  7. Codeforces Round #283 (Div. 2) A ,B ,C 暴力,暴力,暴力

    A. Minimum Difficulty time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  8. tyvj 1884 [NOIP2000T4]方格取数 || codevs 1043 dp

    P1884 [NOIP2000T4]方格取数 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 [noip2000T4]方格取数 描述 设有N*N的方格图(N& ...

  9. XE8_DataSnap设置

    1.参考文章 http://blog.csdn.net/l799623787/article/details/17002979 ZC: 系列在这里:http://www.cnblogs.com/Chi ...

  10. php特级课---2、网站大数据如何存储

    php特级课---2.网站大数据如何存储 一.总结 一句话总结: mysql主从,分库分表,mysql分区,mysql集群,Nosql 1.mysql主从服务器各自的功能是什么? 增删改,主服务器 查 ...