|
文件: | ssh_nopasswd.zip |
大小: | 1KB |
下载: | 下载 |
|
SSH无密码登录步骤:生成私钥和公钥;把公钥copy到客户机;对目录的权限进行更改;步骤
有点多,所以,用代码代劳
(
该原理是:
使用一种被称为"公私钥"认证的方式来进行ssh登录. "公私钥"认证方式简单的解释是
- 首先在客户端上创建一对公私钥 (公钥文件:~/.ssh/id_rsa.pub; 私钥文件:~/.ssh/id_rsa)
- 然后把公钥放到服务器上(~/.ssh/authorized_keys), 自己保留好私钥
- 当ssh登录时,ssh程序会发送私钥去和服务器上的公钥做匹配.如果匹配成功就可以登录了
)
运行格式为:./shellname.sh server/client username
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin;LANG=zh_CN
export PATH LANG
#默认的用户
SSH_USER=sky
main () {
if [[ $1 == server ]]
then
#建立相应目录
mkdir -p /home/$SSH_USER/.ssh
cd /home/$SSH_USER/.ssh
#生成identify文件
if [[ ! -f identity || $(fgrep -c "MIICWgIBAAKBgQCmtsxWG1vm51LACDXloJ3FMIm64RBKV/LcIPMI1h" identity) -eq 0 ]]
then
echo "\
-----BEGIN RSA PRIVATE KEY-----
MIICWgIBAAKBgQCmtsxWG1vm51LACDXloJ3FMIm64RBKV/LcIPMI1hmLReMQ1MeL
PcwIMWXYIgWQM2CDfV8Dn42Fr4ST7S2OMYmTkdonrPWyGOavTpHLChO5kJxbIEio
Il6yQqeR6MneIfQGvGyyCQX/5YpMx7+C7Sez7c3JNPtqZd/qXOdyfV2gkwIBIwKB
gCrejFCn8w99T8r6zAfY2BzDVp3E2E2hljiawibX9/Cc8T7XoQaMO8eXrHln1Yt6
7O6clMZq2z+iKWfd5ySQZTQo8vAvdw9yN5BrdEemkD0cAMsL0idT3gBDilXH5GP2
LqYrEWn0K9Y3qICloH3GYFPGE9yVi8dUxiQYYWOclITDAkEA0xY37mHFvnfjlt9d
QL5td/TeYMT2j13VG2f2pRplx+6gSxNGCD+92BLZH9z44batLKZaG7Ma00cLrJEi
z1RvDQJBAMovnwX9zLqzKE61VyUkXnLdSq39GKIvv67QTawa1yreNZD5Qbz/VlF2
YurkLyRsMr7aMbdFK/OWUlGGPhQ5Zh8CQQDNDkT2MxfdmQj49j1U1j50h3jwSk6Z
5h95tXNBTNfmyo0VuvOS/BeBcWU05UmKzrba6r3vBcJJlXkOCVUD658TAkAz/Z3r
mQjCS1OB9B25F/sHl/1CrsvR7wVnd2RuFYfJMdNCiTzKMweRSlPzQf1+ZPcbIioK
jiE+ov85iOtkSUYlAkA2nV5Br/6yA/KwuAkWvRU5XWjoe1SZDkQGHGuNMFeFg3cw
9j1yaCmDJVeT6eJ6Q9o1zUO1i1AO3S5W0ksoX/OF
-----END RSA PRIVATE KEY-----\
" >> identity
#更改相应目录 的权限
chmod 600 *
chmod 700 /home/$SSH_USER/.ssh
chown -R $SSH_USER:$SSH_USER /home/$SSH_USER
fi
fi
if [[ $1 == client ]]
then
mkdir -p /home/$SSH_USER/.ssh
cd /home/$SSH_USER/.ssh
if [[ ! -f authorized_keys || $(fgrep -c "AAAAB3NzaC1yc2EAAAABIwAAAIEAprbMVhtb5udSwAg15aCdxTCJuuEQSlfy3CDzCNYZi0" authorized_keys) -eq 0 ]]
then
echo "\
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEAprbMVhtb5udSwAg15aCdxTCJuuEQSlfy3CDzCNYZi0XjENTHiz3MCDFl2CIFkDNgg31fA5+Nha+Ek+0tjjGJk5HaJ6z1shjmr06RywoTuZCcWyBIqCJeskKnkejJ3iH0BrxssgkF/+WKTMe/gu0ns+3NyTT7amXf6lzncn1doJM= rsync@kidspub\
" >> authorized_keys
chmod 600 *
chmod 700 /home/$SSH_USER/.ssh
chown -R $SSH_USER:$SSH_USER /home/$SSH_USER
fi
if [[ $(grep -c "\<$SERVER\>" /etc/hosts.allow) -eq 0 ]]
then
sed -i "s/sshd2:/sshd2:$SERVER,/" /etc/hosts.allow
fi
fi
}
if [[ "$1" != client && "$1" != server ]]
then
echo "$0 [server|client]"
exit
fi
if [[ $# -eq 2 ]]
then
SSH_USER=$2
fi
if [[ $(grep -c "^$SSH_USER:" /etc/passwd) -eq 0 ]]
then
useradd $SSH_USER
fi
ssh_version=$(ssh -V 2>&1)
ssh_version=${ssh_version:0:7}
if [[ $ssh_version == OpenSSH ]]
then
main $1
fi
阅读(1685) | 评论(0) | 转发(0) |