ssh自动登陆

本文记录一些登陆的操作。

简化记录

1
2
3
4
5
ssh-keygen -t rsa (一路回车)
ssh-copy-id latelee@172.18.18.18
存疑,测试发现需要使用:
ssh-copy-id -i ~/.ssh/id_rsa.pub latelee@172.18.18.18
也可手动将公钥文件的内容拷贝到~/.ssh/authorized_keys文件后面。

ssh密钥登陆Linux服务

环境:windows+git bash+mingw (注:本文命令适用于Windows和Linux系统)

服务器:Linux CentOS 7

打开git bash,产生密钥对:

1
ssh-keygen -t rsa -b 4096

出现输入项时,直接回车即可。

1
2
3
Enter file in which to save the key (/c/Users/Late Lee/.ssh/id_rsa): // 回车
Enter passphrase (empty for no passphrase): // 回车
Enter same passphrase again: // 回车

最后输出目录:

1
2
Your identification has been saved in /c/Users/Late Lee/.ssh/id_rsa.
Your public key has been saved in /c/Users/Late Lee/.ssh/id_rsa.pub.

产生文件:id_rsa为私钥,id_rsa.pub为公钥。

进入cd /c/Users/Late\ Lee/.ssh/目录。拷贝公钥到Linux服务器。

1
ssh-copy-id -i id_rsa.pub root@99.69.96.66

输出信息:

1
2
3
4
5
6
7
8
9
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
/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
root@99.69.96.66's password: // !! 此处输入目标系统的密码

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'root@99.69.96.66'"
and check to make sure that only the key(s) you wanted were added.

输入 ssh root@99.69.96.66,可以不需要密码登陆。

也可以将公钥文件id_rsa.pub的内容放到远程服务器的~/.ssh/authorized_keys中(可以同时存在多个,按顺排好即可),示例:
echo "公钥内容" >> ~/.ssh/authorized_keys

拷贝文件使用 scp 命令。在 Windows 系统可安装 cygwin 和 git 软件,以使用其提供的命令行。

Linux环境:

1
ssh-keygen -t rsa

其输出信息及操作(中文表示)如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 保存目录,默认,直接回车即
Enter passphrase (empty for no passphrase): 不输入密码,直接回车即可
Enter same passphrase again: 直接回车
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:7PSMhZ5+ocFnRLSLDYrJFtCp2PfO5CNwQMYmnJHCqYo root@ecs-cba4-0002.novalocal
The key's randomart image is:
+---[RSA 2048]----+
|oo*. . .. |
|o*=.o .. |
|oB .. ... |
|o +..+ o =.. |
|o o=...S.+ |
|E ... o+o*+ |
| o = ==o. |
| . =.. . |
| . ... |
+----[SHA256]-----+