OpenSSH
在当今
Linux
和
Unix
等非
Windows
系统中非常重要的一个基础应用,如:
Ssh
、
Scp
、
Sftp
等命令。它是最安全的系统
OpenBSD
的一个子计划。本文只介绍它密钥登录方面的应用。
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
下面以
Freebsd
、
Centos
及其他常见服务器间通过
RSA
或
DSA
密钥无需密码相互访问为例。
1
、在
Freebsd
下生成私、公密钥(
id_rsa
、
id_rsa.pub
或
id_dsa
、
id_dsa.pub
)
user1@Freebsd# ssh-keygen -t dsa(or rsa)
如果不带
-t dsa
则默认自动生成为
rsa
密钥。此后一路回车。(
1
)不要修改
Key
的路径(因为是
sshd_config
文件指定的位置,如果要修改,
sshd_config
文件相应的配置也要修改并重启才会生效;(
2
)不要给私钥设置密码,因为我们要使用自动登录功能,当然有其他用途另做他算。
user1@Freebsd# touch ~/.ssh/authorized_keys
user1@Freebsd# ls ~/.ssh
authorized_keys id_dsa id_dsa.pub
user1@Freebsd# chmod 600 ~/.ssh/*
2
、同样在
Centos
下生成私、公密钥
user2@Centos# ssh-keygen -t dsa(or rsa)
user2@Centos# touch ~/.ssh/authorized_keys
user2@Centos# ls ~/.ssh
authorized_keys id_dsa id_dsa.pub
user2@Centos# chmod 600 ~/.ssh/*
3
、在
Freebsd
下把
Centos
的公钥(
id_dsa.pub
)加入自己的
authorized_keys
user1@Freebsd# scp -P 22222 user2@Centos:/home/user2/.ssh/id_dsa.pub
~/.ssh/Centos.pubtmp
如果
Centos
的
sshd
监听端口没有修改过,
-P
(大写字母)
22222
可去掉。此时需要输入
Centos
上
user2
的密码才可继续拷贝。
user1@Freebsd# cat ~/.ssh/Centos.pubtmp >> ~/.ssh/authorized_keys
user1@Freebsd# rm -f ~/.ssh/Centos.pubtmp
到此为止
Centos
的
user2
就可以用以下命令,无密码登录到
Freebsd
的
user1
账户了
user2@Centos# ssh -p 11111 user1@Freebsd
注意此处
SSH
的
-p
是小写字母,不可与
SCP
的大写字母
-P
混淆。
4
、同样在
Centos
下把
Freebsd
的公钥(
id_dsa.pub
)加入自己的
authorized_keys
user2@Centos# scp -P 11111 user1@Freebsd:/home/user1/.ssh/id_dsa.pub
~/.ssh/Freebsd.pubtmp
user2@Centos# cat ~/.ssh/Freebsd.pubtmp >> ~/.ssh/authorized_keys
user2@Centos# rm -f ~/.ssh/Freebsd.pubtmp
到此为止
Freebsd
的
user1
就可以用以下命令,无密码登录到
Centos
的
user2
账户了
user1@Freebsd# ssh -p 22222 user2@Centos
5
、如果
Freebsd
和
Centos
不需要通过密钥无密码登录其他服务器的话,可以把他们
~/.ssh/
目录下的
id_dsa.pub
文件删除。
6
、如果两个服务器不需要区分私钥的话,
Freebsd
和
Centos
可以共用一个,公钥当然也一样,方法如下:
(
1
)、在
Freebsd
(或
Centos
)下生成私、公密钥(
rsa
)
user1@Freebsd# ssh-keygen
user1@Freebsd# ls ~/.ssh/
id_rsa id_rsa.pub
user1@Freebsd# mv ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
user1@Freebsd# chmod 600 ~/.ssh/*
(
2
)、在
Centos
(或
Freebsd
)上的操作
user2@Centos# mkdir ~/.ssh
user2@Centos# chmod 700 ~/.ssh
//
此步一定要做,我曾因为这个权限问题耽搁了好久。
user2@Centos# scp -P 11111 user1@Freebsd:/home/user1/.ssh/* ~/.ssh/
user2@Centos# chmod 600 ~/.ssh/*
(
3
)、登录检验
在
Freebsd
下用如下命令访问
Centos
user1@Freebsd# ssh -p 22222 user2@Centos
在
Centos
下用如下命令访问
Freebsd
user2@Centos# ssh -p 11111 user1@Freebsd
转载于:https://blog.51cto.com/ollll/352790