如何设置防火墙规则以仅允许SSH从Google Cloud Console进入实例

时间:2020-05-04 04:40:54

标签: ssh google-cloud-platform firewall

我可以允许Bastian主机的IP,但是如何在防火墙规则中允许Google Cloud Console的IP?

2 个答案:

答案 0 :(得分:1)

  • 如果您使用的是SSH按钮,则它是您的外部IP。
  • 如果您使用的是Cloud Shell,则从技术上讲它是VM实例,因此它是(Google Cloud的)随机外部IP。

如果要从控制台访问,可以选择GCP open firewall only to cloud shell中的答案。

答案 1 :(得分:1)

1。。如果您使用默认网络配置,则Compute Engine会创建防火墙规则,该规则允许您通过端口22进行TCP连接。您可以在GCP控制台中看到它们:

GCP Console => VPC network => Firewall rules

默认网络具有预配置的防火墙规则,该规则允许网络中的所有实例相互通信。特别是,这些防火墙规则允许来自任何地方(0.0.0.0/0)的ICMP,RDP和SSH入口流量。 SSH应该有一个Ingress防火墙规则:default-allow-ssh

2。。如果使用自定义网络,则应手动创建SSH防火墙规则。

使用Cloud Console

GCP Console => VPC network => Firewall rules => Create Firewall Rule 
    Name:   mynet-allow-ssh
    Network:    mynet
    Targets:    All instances in the network
    Source filter:  IP Ranges
    Source IP ranges:   0.0.0.0/0 
    Protocols and ports:    Specified protocols and ports
        tcp: ports 22 

使用命令行

$ gcloud compute --project=myproject firewall-rules create mynet-allow-ssh --direction=INGRESS --priority=1000 --network=mynet --action=ALLOW --rules=tcp:22 --source-ranges=0.0.0.0/0

有关更多详细信息,请参见Compute Engine => Documentation => Connecting to instances

对于在您按下Cloud Console中的“ SSH”按钮的情况下,将“ Google Cloud Console IP”列入白名单,这是不可行的,因为SSH连接是通过中继服务器通过HTTPS建立的,该中继服务器可能具有Google外部IP池中的地址无法预测。从这个角度来看,使用带有单个静态IP的堡垒主机更为合理。