如何在AWS管理控制台中更改ec2实例的密钥对?我可以停止实例,我可以创建新的密钥对,但我没有看到任何链接来修改实例的密钥对。
当前回答
这个问题有两种情况:-
1)你没有权限访问。pem文件,这就是为什么你想创建一个新的文件。
2)您拥有.pem文件的访问权限,但您只是想更改或创建一个新的.pem文件,以解决某些漏洞或安全问题。
所以如果你的钥匙丢了,你可以向上滚动查看其他答案。但是,如果您只是为了安全目的而更改您的.pem文件,请遵循以下步骤:-
1)进入AWS控制台登录,从密钥对创建一个新的.pem文件 那边的区域。它会自动下载。pem文件到 你的电脑 2)修改权限到400如果你使用Linux/ubuntu点击下面 命令
chmod 400 yournewfile.pem
3)在本地生成新下载文件的RSA
ssh-keygen -f yournewfile.pem -y
4)从这里复制RSA代码 5)现在SSH到您的实例通过之前的。pem文件
ssh -i oldpemfileName.pem username@ipaddress
sudo vim ~/.ssh/authorized_keys
6)给1 - 2行空间,并粘贴复制的新文件的RSA 然后保存文件 7)现在您的新.pem文件与正在运行的实例链接 8)如果你想禁用之前的.pem文件访问,那么只需编辑即可 的
sudo vim ~/.ssh/authorized_keys
文件,并从这里删除或更改之前的RSA。
注意:-小心删除,以便新创建的RSA不会被更改。
通过这种方式,您可以将新的.pem文件与正在运行的实例更改/连接。
出于安全考虑,您可以撤销对先前生成的.pem文件的访问权。
希望对大家有所帮助!
其他回答
来自AWS EC2支持的指令:
Change pem login go to your EC2 Console Under NETWORK & SECURITY, click on Key Pair Click on Create Key Pair Give your new key pair a name, save the .pem file. The name of the key pair will be used to connect to your instance Create SSH connection to your instance and keep it open in PuttyGen, click "Load" to load your .pem file Keep the SSH-2 RSA radio button checked. Click on "Save private key" You'll get pop-up window warning, click "Yes” click on "Save public key" as well, so to generate the public key. This is the public key that we're going to copy across to your current instance Save the public key with the new key pair name and with the extension .pub Open the public key content in a notepad copy the content below "Comment: "imported-openssh-key" and before "---- END SSH2 PUBLIC KEY ---- Note - you need to copy the content as one line - delete all new lines on your connected instance, open your authorized_keys file using the tool vi. Run the following command: vi .ssh/authorized_keys you should see the original public key in the file also move your cursor on the file to the end of your first public key content :type "i" for insert on the new line, type "ssh-rsa" and add a space before you paste the content of the public key , space, and the name of the .pem file (without the .pem) Note - you should get a line with the same format as the previous line press the Esc key, and then type :wq!
这将保存更新后的authorized_keys文件
现在尝试使用新的密钥pai打开一个新的SSH会话到您的实例
当您确认能够使用新的密钥对SSH进入实例时,您可以vi . SSH /authorized_key并删除旧的密钥。
对Shaggie评论的回答:
如果您无法连接到实例(例如密钥损坏),请使用AWS控制台分离卷(http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-detaching-volume.html)并将其重新连接到工作实例,然后更改卷上的密钥并将其重新连接到前一个实例。
这是为他们谁有两个不同的pem文件,并出于任何安全目的想要丢弃其中一个。假设我们想要丢弃1。pem
连接到服务器2,从~/.ssh/authorized_keys中复制ssh密钥 在另一个终端上连接服务器1,并将密钥粘贴到~/.ssh/authorized_keys中。现在您将有两个公共ssh密钥 现在,为了增强自信心,尝试使用2.pem连接服务器1。您将能够将服务器1与这两个1连接。Pem和2.pem 现在,注释1。Pem SSH,使用SSH -i连接。pem user@server1
我认为最简单的方法是:
创建现有实例的AMI映像。 使用AMI映像(由步骤1创建)和新的密钥对启动新的EC2实例。 使用新密钥登录到新的EC2实例。
您有几个选项可以替换EC2实例的密钥。
您可以在.ssh/authorized_keys文件中手动替换密钥。但是,这需要您实际访问实例或卷(如果未加密)。 您可以使用AWS系统管理器。这需要安装代理。
由于第一个选项可以很容易地在答案中或在您选择的搜索引擎中找到,所以我想重点介绍系统管理器。
打开服务系统管理器 单击左侧的Automation。 单击执行自动化 选择AWSSupport-TroubleshootSSH(通常在最后一页)
您可以在AWS官方文档中找到更多信息
一旦启动了实例,就无法在元数据级别上更改与该实例关联的密钥对,但是可以更改用于连接该实例的ssh密钥。
在大多数ami上都有一个启动过程,下载公共ssh密钥并将其安装在.ssh/authorized_keys文件中,以便您可以作为该用户使用相应的私有ssh密钥进行ssh登录。
如果您希望更改用于访问实例的ssh密钥,则需要编辑实例本身的authorized_keys文件并将其转换为新的ssh公钥。
authorized_keys文件位于您正在登录的用户的主目录下的.ssh子目录下。取决于你正在运行的AMI,它可能在以下情况之一:
/home/ec2-user/.ssh/authorized_keys
/home/ubuntu/.ssh/authorized_keys
/root/.ssh/authorized_keys
编辑authorized_keys文件后,在断开用于编辑该文件的会话之前,总是使用不同的终端来确认您能够通过ssh登录到该实例。您不希望犯错误并将自己完全锁定在实例之外。
当您在考虑EC2上的ssh密钥对时,我建议您将自己的个人ssh公钥上传到EC2,而不是让Amazon为您生成密钥对。
这是我写的一篇文章:
上传个人ssh密钥到Amazon EC2 http://alestic.com/2010/10/ec2-ssh-keys
这只适用于您运行的新实例。
推荐文章
- 如何查看所有地区所有正在运行的Amazon EC2实例?
- 如何从命令行使用多个AWS帐户?
- 如何搜索亚马逊s3桶?
- 通过SSH使用Eclipse完成远程项目
- 如何强制从另一个SSH会话分离屏幕?
- com.jcraft.jsch.JSchException: UnknownHostKey
- 如何在Python中scp ?
- 在SSH会话中查找客户端的IP地址
- 拒绝访问;您需要(至少一个)SUPER特权来执行此操作
- 我如何使用通配符“cp”一组文件与AWS CLI
- 自定义SSH端口上的Git
- 我如何获得亚马逊的AWS_ACCESS_KEY_ID ?
- 如何使所有对象在AWS S3桶公共默认?
- Git显示“警告:永久添加到已知主机列表”
- 为什么git在Windows下记不住我的密码