2012年1月27日金曜日

[工具使用] ssh 免密码登录远程主机

1. 免密码访问远程主机

1-1. 目标主机上创建 .ssh 文件夹 (没有则创建)
[go@target ~]$ mkdir .ssh
[go@target ~]$ chmod 700 .ssh

1-2. 本地生成密钥对
[go@localhost ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/go/.ssh/id_rsa): <= 回车
Created directory '/home/go/.ssh'.
Enter passphrase (empty for no passphrase): <= 回车
Enter same passphrase again: <= 回车
Your identification has been saved in /home/go/.ssh/id_rsa.
Your public key has been saved in /home/go/.ssh/id_rsa.pub.
The key fingerprint is:
3a:5f:79:80:e5:f2:67:e6:3e:a9:9f:f1:3f:a5:43:f6 go@localhost

1-3. 将公钥上传到目标主机 (文件存在则追加到 authorized_keys 中)
[go@localhost ~]$ scp .ssh/id_rsa.pub go@target:~/.ssh/authorized_keys

1-4. 目标主机上修改认证文件权限 (644)
[go@target ~]$ chmod 644 .ssh/authorized_keys

1-5. 测试
[go@localhost ~]$ ssh go@target ls

2. 免密码多台主机互访

2-1. 在各个主机上生成密钥对
[go@centos56 ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/go/.ssh/id_rsa): <= 回车
Created directory '/home/go/.ssh'.
Enter passphrase (empty for no passphrase): <= 回车
Enter same passphrase again: <= 回车
Your identification has been saved in /home/go/.ssh/id_rsa.
Your public key has been saved in /home/go/.ssh/id_rsa.pub.
The key fingerprint is:
3a:5f:79:80:e5:f2:67:e6:3e:a9:9f:f1:3f:a5:43:f6 go@centos56

[go@centos57 ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/go/.ssh/id_rsa): <= 回车
Created directory '/home/go/.ssh'.
Enter passphrase (empty for no passphrase): <= 回车
Enter same passphrase again: <= 回车
Your identification has been saved in /home/go/.ssh/id_rsa.
Your public key has been saved in /home/go/.ssh/id_rsa.pub.
The key fingerprint is:
59:81:95:b5:59:60:84:bd:4f:17:f5:cd:6e:44:fe:d1 go@centos57

2-2. 在各个主机上拷贝公钥到其他主机
[go@centos56 ~]$ ssh-copy-id -i .ssh/id_rsa.pub go@centos57
15
go@centos57's password:
Now try logging into the machine, with "ssh 'go@centos57'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[go@centos57 ~]$ ssh-copy-id -i .ssh/id_rsa.pub go@centos56
15
The authenticity of host 'centos56 (centos56)' can't be established.
RSA key fingerprint is 88:01:a5:f8:af:c9:5a:3d:19:30:5b:fc:d4:19:07:29.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'centos56' (RSA) to the list of known hosts.
go@centos56's password:
Now try logging into the machine, with "ssh 'go@centos56'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

2-3. 测试
[go@centos56 ~]$ ssh go@centos57 ls
[go@centos57 ~]$ ssh go@centos56 ls

0 件のコメント:

コメントを投稿