使用目的
由于最近需要使用Amazon EC2的服务器去跑程序,发现过程并不是很顺利,所以打算记录下来,方便以后查看。
最开始使用的是Amazon Windows OS的实例,但是再用SSH远程登录的时候总是提示operation timed out, 始终没有解决,因此换到Linux下。使用pem文件登陆比较方便的一点是无需自己输入用户密码,因此更加便捷。
SSH远程登录
- 切换工作区到pem所在文件夹;
$ cd Downloads/
- 定位到pem文件,wizad会自动检测到用于启动实例的密钥;
命令如下:
$ ssh -i identity_file user@hostname
示例如下:
$ ssh -i "xinshu.pem" [email protected]
- 继续输入yes之后,有可能会提示如下错误, 表明pem文件权限过大;
Permissions 0644 for ‘xinshu.pem’ are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key “xinshu.pem”: bad permissions
[email protected]: Permission denied (publickey).
- 需要设置为只有拥有者拥有读写权限;
$ sudo chmod 0600 xinshu.pem
- 输入密码之后重新输入ssh语句,就没有错误提示了
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-1032-aws x86_64)
- Documentation: https://help.ubuntu.com
- Management: https://landscape.canonical.com
- Support: https://ubuntu.com/advantage
- 为了方便起见,可以使用ssh-add命令将pem文件添加到系统中,下次就可以直接登录,无需再切换到pem文件所在的目录中;
$ ssh-add -K xinshu.pem
Identity added: xinshu.pem (xinshu.pem)
$ ssh [email protected]
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-1032-aws x86_64)
- 退出服务器,按control+d或者输入logout即可。
$ logout
参考链接: Mac ssh使用pem文件登录远程服务器.