使用ssh连接虚拟机_如何使用SSH创建并连接到Google Cloud虚拟机
使用ssh连接虚拟机by Nezar Assawiel 由Nezar Assawiel如何使用SSH创建并连接到Google Cloud虚拟机 (How to create and connect to Google Cloud Virtual Machine with SSH)Google Cloud offers many tools and services. One of thes...
使用ssh连接虚拟机
by Nezar Assawiel
由Nezar Assawiel
如何使用SSH创建并连接到Google Cloud虚拟机 (How to create and connect to Google Cloud Virtual Machine with SSH)
Google Cloud offers many tools and services. One of these services is creating highly customizable virtual machines. If you are not familiar with what a virtual machine is, here is a definition from Microsoft:
Google Cloud提供了许多工具和服务。 这些服务之一是创建高度可定制的虚拟机。 如果您不熟悉什么是虚拟机,则以下是Microsoft的定义:
A virtual machine is a computer file, typically called an image, that behaves like an actual computer. In other words, creating a computer within a computer. It runs in a window, much like any other program, giving the end user the same experience on a virtual machine as they would have on the host operating system itself. The virtual machine is sandboxed from the rest of the system, meaning that the software inside a virtual machine can’t escape or tamper with the computer itself.
虚拟机是计算机文件,通常称为映像,其行为类似于实际的计算机。 换句话说,在计算机内创建计算机。 它在窗口中运行,就像在其他程序中一样,为最终用户提供了与在主机操作系统上一样的虚拟机体验。 虚拟机是从系统的其余部分沙箱中提取出来的,这意味着虚拟机内部的软件无法逃脱或篡改计算机本身。
Virtual machines are needed in many situations to test applications against other operating systems, to access virus-infected data, or to experiment with other operating systems. You can install virtual machines on your computer. You can also create them in the cloud and simply connect to them.
在许多情况下,都需要虚拟机来针对其他操作系统测试应用程序,访问受病毒感染的数据或尝试其他操作系统。 您可以在计算机上安装虚拟机。 您也可以在云中创建它们,然后简单地连接到它们。
In this tutorial, I will walk you through how to create a virtual machine in Google Cloud. We can connect to it with SSH from your computer.
在本教程中,我将指导您如何在Google Cloud中创建虚拟机。 我们可以从您的计算机通过SSH连接到它。
If you don’t have one already, create a Google Cloud account from here.
如果您还没有,请从这里创建一个Google Cloud帐户。
You will get $300 credit to play around with for a year! It is more than enough to learn and play with everything Google Cloud offers.
您将获得300美元的信用额度,可以使用一年! 学习和使用Google Cloud提供的所有功能已绰绰有余。
2. Create a new project or use an existing one. You can create a new project called project1, for example, as in the following gif:
2。 创建一个新项目或使用现有项目。 例如,您可以创建一个名为project1的新项目,如以下gif所示:
3. Now you are set to create a virtual machine. Go to the top left corner of your Google Cloud home page, click on the triple bar icon ≡ and select Compute Engine ->VM instance and click Create.
3.现在,您已设置为创建虚拟机。 转到您的谷歌云主页的左上角,点击三重栏图标≡和选择的Compute Engine - > VM斯塔ñCE和CLI CK的Cre吃。
Enter whatever name you want in the Name field as shown below:
在“ 名称”字段中输入所需的任何名称,如下所示:
Keep the default region and zone. Any region/zone will do for this tutorial. If you are curious about what they mean, you can read Google Cloud’s documentation about them here.
保留默认的区域和区域。 任何区域/区域都可以完成本教程。 如果您对它们的含义感到好奇,可以在此处阅读有关它们的Google Cloud文档。
You can keep default machine type or click Customize to select the number of CPU cores, memory, and GPUs you would like your virtual machine to have. You will see the cost on the right side changes!
您可以保留默认计算机类型,或单击“ 自定义为 选择您希望虚拟机拥有的CPU内核,内存和GPU的数量。 您会在右侧看到成本的变化!
For your first experiments with Google Cloud, you can be conservative with the $300 credit for some actual work. In such a case, you can choose the following configuration:
在您首次使用Google Cloud进行实验时,您可以保守一些实际工作获得的$ 300赠金。 在这种情况下,您可以选择以下配置:
Next choose a boot disk. For example, you can choose 20 GB, SSD, Ubuntu 16.04 LTS as shown below:
接下来选择启动盘。 例如,您可以选择20 GB,SSD,Ubuntu 16.04 LTS ,如下所示:
Then set the Service Account under Identity and API access to No service account as shown below:
然后将“ 身份和API访问”下的“ 服务帐户”设置为“ 无服务帐户” ,如下所示:
Finally, go to the Security tab under Firewall. You will see an SSH Key field as shown below:
最后,转到“ 防火墙”下的“ 安全性”选项卡。 您将看到一个SSH密钥字段,如下所示:
This where you are going to connect your computer to the virtual machine using your SSH Key!
您将在这里使用SSH密钥将计算机连接到虚拟机!
If you are not familiar with SSH (Secure Shell) and why you may want to use it, it is a network protocol that provides encrypted data communication between two computers (your computer and Google’s servers, in this case) which are connected over an insecure network (the Internet here).
如果您不熟悉SSH(安全外壳)以及为什么要使用它,它是一种网络协议,可在通过不安全的方式连接的两台计算机(在本例中为您的计算机和Google的服务器)之间提供加密的数据通信。网络(此处为Internet)。
To establish an SSH connection, you may need an application that can do that, depending on your operating system. Follow the rest of this post depending on your operating system (Windows or Mac/Linux).
要建立SSH连接,您可能需要一个可以执行此操作的应用程序,具体取决于您的操作系统。 根据您的操作系统(Windows或Mac / Linux),按照本文的其余部分进行操作。
视窗 (Windows)
I recommend PuTTY. It is an open-source and easy to use SSH client. You can download PuTTY and install it from here.
我推荐腻子 。 它是一个开源且易于使用的SSH客户端。 您可以从此处下载PuTTY并进行安装。
After installing PuTTY, open PuTTY Key Generator and click create. It will generate a random key by you moving the mouse over the blank area. After it is done, you will get something like this:
安装PuTTY后,打开PuTTY Key Generator ,然后单击create 。 它会产生由你在空白区域移动鼠标随机密钥。 完成后,您将获得以下内容:
Change the key comment field to something recognizable and easy to type, as this will become a user name later!
将关键注释字段更改为可识别且易于键入的内容,因为以后它将成为用户名!
Then save both the public and private keys by clicking the corresponding icons shown in the picture above.
然后,通过单击上图所示的相应图标,保存公钥和私钥。
Highlight the whole Key field from the PuTTY Key Generator, and copy and paste it in the key data field in Google Cloud:
突出显示PuTTY密钥生成器中的整个密钥字段,然后将其复制并粘贴到Google Cloud的密钥数据字段中:
Click create and wait for the virtual machine instance to be created.
单击创建,然后等待创建虚拟机实例。
In the meantime, you can go to PuTTY. Go to SSH ->Auth and browse for the private key file that you saved.
在此期间,您可以转到PuTTY。 转到SSH- > Auth,然后浏览保存的私钥文件。
Next, go to Google Cloud and copy the external IP from the virtual machine instance that you just created as shown below:
接下来,转到Google Cloud,并从刚刚创建的虚拟机实例复制外部IP,如下所示:
And paste it on the Host field under Sessions in PuTTY and hit Enter:
并将其粘贴到PuTTY中“ 会话”下的“主机”字段中,然后按Enter :
Note: you might get an error message. Ignore it and click yes. (It just says the key is not already in the registry. Are you sure you want to connect?)
注意:您可能会收到一条错误消息。 忽略它,然后单击“ 是” 。 (它只是说密钥不在注册表中。确定要连接吗?)
Then enter the username you created when generating the key (key comment above). Boom! you are in the virtual machine that you just created.
然后输入您在生成密钥时创建的用户名(上面的密钥注释 )。 繁荣! 您位于刚创建的虚拟机中。
You can install python and Google APIs on it, for example, to start making some magic! Don’t forget to shut it down in Google Cloud after you are done to be economic with your credit :)
您可以在上面安装python和Google API,例如,开始制作魔术! 为了节省金钱,别忘了在Google Cloud中关闭它:)
Mac / Linux (Mac/Linux)
Mac and Linux support SSH connection natively. You just need to generate an SSH key pair (public key/private key) to connect securely to the virtual machine.
Mac和Linux本机支持SSH连接。 您只需要生成SSH密钥对(公钥/私钥)即可安全地连接到虚拟机。
The private key is equivalent to a password. Thus, it is kept private, residing on your computer, and should not be shared with any entity. The public key is shared with the computer or server to which you want to establish the connection. To generate the SSH key pair to connect securely to the virtual machine, follow these steps:
私钥等效于密码。 因此,它是私有的,驻留在您的计算机上,并且不应与任何实体共享。 公用密钥与您要建立连接的计算机或服务器共享。 要生成SSH密钥对以安全地连接到虚拟机,请按照下列步骤操作:
Enter the following command in Terminal: ssh-keygen -t rsa
. It will start the key generation process. You will be prompted to choose the location to store the SSH key pair. Press ENTER to accept the default location as shown below:
在终端中输入以下命令: ssh-keygen -t rsa
。 它将开始密钥生成过程。 系统将提示您选择存储SSH密钥对的位置。 按Enter接受默认位置,如下所示:
Next, choose a password for your login to the virtual machine or hit ENTER if you wish not to use a password. The private key (i.e. identification) and the public key will be generated as shown below:
接下来,选择用于登录虚拟机的密码,或者如果您不想使用密码,请按Enter。 私钥(即标识)和公钥将生成,如下所示:
Now run the following command: cat ~/.ssh/id_rsa.pub
. It will display the public key in the terminal as shown below. Highlight and copy this key:
现在运行以下命令: cat ~/.ssh/id_rsa.pub
。 它将在终端中显示公钥,如下所示。 突出显示并复制以下密钥:
and paste it in the SSH key field in Google Cloud and hit Create:
并将其粘贴到Google Cloud的SSH密钥字段中,然后点击Create :
Now you can use the External IP of the virtual machine you just created:
现在,您可以使用刚刚创建的虚拟机的外部IP :
to ssh to it as follows:
ssh到它,如下所示:
You will get “The authenticity of host…etc.” warning as shown in the picture below. This is normal. Whenever SSH connects to a system it hasn’t seen before, it will generate a warning like this. Reply yes to connect, and bingo! You are in the virtual machine, as you can see from host name instance-3. To exit the virtual machine, just type exit.
您将获得“主机的真实性……等”。 警告,如下图所示。 这是正常的。 每当SSH连接到以前从未见过的系统时,它都会生成这样的警告。 回答“ 是”以连接,然后宾果游戏! 您在虚拟机中,如主机名instance-3所示。 要退出虚拟机,只需键入exit。
Don’t forget to shut the virtual machine in Google Cloud after you are done to save that $300 credit!
在节省$ 300信用额之后,请不要忘记关闭Google Cloud中的虚拟机!
Originally published at assawiel.com/blog on December 23, 2017. Updated: Oct 10, 2018
最初于2017年12月23日发布在assawiel.com/blog 。更新:2018年10月10日
使用ssh连接虚拟机
更多推荐
所有评论(0)