# SSH登录
SSH是一种网络协议,用于计算机之间的加密登录(服务器本质也是计算机)。
# 基本命令
ssh <user name>@<ipaddress or computer name>
1
注释:
<user name>
是你想要登录到的计算机上的一个用户的名字;<ipaddress or computer name>
是你想要登录到的计算机的IP地址或计算机名,二选一即可;- 如不理解,请看示例。
# 示例
本机系统为ubuntu16.04,通过终端登录到阿里云服务器,服务器系统也为ubuntu16.04,阿里云给的服务器公网IP地址为:139.23.43.186
一般有两种方式登录到别的计算机:
# 输入密码登录
honkerzhou@honkerzhou-X450VC:~$ ssh keweng@139.23.43.186
keweng@139.23.43.186's password: # 注释:此处会要求你输入服务器上ubuntu16.04系统里用户名为keweng的登录密码
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-62-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.
Welcome to Alibaba Cloud Elastic Compute Service !
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
如果你的电脑是第一次登录到该服务器,则会出现以下提示:
The authenticity of host 'host (139.23.43.186)' can't be established.
RSA key fingerprint is 28:2e:e7:e0:de:9f:ac:67:68:c2:42:2d:37:16:38:4d.
Are you sure you want to continue connecting (yes/no)?
# 注释:这段话大意为:无法确认host主机的真实性,只知道它的公钥指纹,问你还想继续连接吗?
1
2
3
4
2
3
4
没关系,直接输入yes然后回车,会出现以下提示:
Warning: Permanently added 'host,12.18.429.21' (RSA) to the list of known hosts.
1
然后会要求输入密码,接下来便和起前面情况一样了。
# 公钥登录
想想每次登录都要输入密码,是不是贼麻烦?那就用公钥登录吧,一次验证,终生信任。
1.在你电脑的终端里,输入ssh-keygen
生成公钥(请注意区分,不要还停留在前面登录到服务器的终端里,无法区分的话就新建个终端窗口再继续,快捷键:Ctrl+Alt+t
)。
honkerzhou@honkerzhou-X450VC:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/honkerzhou/.ssh/id_rsa): #回车
Enter passphrase (empty for no passphrase): #回车(此处用来设私钥密码,可跳过)
Enter same passphrase again: #回车
Your identification has been saved in /home/honkerzhou/.ssh/id_rsa. #私钥存储路径
Your public key has been saved in /home/honkerzhou/.ssh/id_rsa.pub. #公钥存储路径
1
2
3
4
5
6
7
2
3
4
5
6
7
如果你想看私钥和公钥长啥样的话,可以通过上面显示的相应路径去看看,加深你对私钥公钥的印象。
2.继续输入ssh-copy-id <user name>@<ipaddress or computer name>
将公钥传送到服务器上面。
honkerzhou@honkerzhou-X450VC:~$ ssh-copy-id keweng@139.23.43.186
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
keweng@139.23.43.186's password: #要求输入密码,输完这次就解放了
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'keweng@139.23.43.186'"
and check to make sure that only the key(s) you wanted were added.
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
3.看,人家上面都告诉你了,下次再想要登录到服务器的话,直接输入ssh 'keweng@139.23.43.186
就行啦,再也不用输入密码啦!