《服务器SSH Public Key认证指南》-补充

  • Post author:
  • Post category:其他


《服务器SSH Public Key认证指南》-补充

对于服务器SSH-key有疑问,可以参考

《服务器SSH Public Key认证指南》

下面讲讲,我经历的配置过程:



1.1 在Windows客户端生成Key Pair

在这主要介绍一下SecureCRT中OpenSSH连接的使用。(以Version5.0为准介绍)

第一步 生成Key Pair

运行SecureCRT, 选择菜单栏Tools->Create Public Key,点击“下一步”,选择Key Pair的算法类型为“RSA”(也可以使用“DSA”,但推荐使用RSA)


wps_clip_image-19074

输入加密Key的保护口令(Passphrase):


wps_clip_image-13228


wps_clip_image-21356

请牢记此Passphrase,如果遗忘,只能重新生成Key Pair。

选择Key的长度(默认的1024已经基本可以满足,可以根据需要增减长度,不过请注意长度越短越低保障,安全强度越低):


wps_clip_image-10407

生成Key Pair,在如下图的程序框内随意移动鼠标以给它生成Key的足够的随机量:


wps_clip_image-30959

将Key Pair保存到妥善的地方,如d:\sshkey\id_rsa,Public Key即为id_rsa.pub(建议改成登录的用户名作为区分)


wps_clip_image-4058

注意:SecureCRT5生成的公钥,在linux服务器上需要进行转换:

SecureCRT5生成的公钥,例如:

—- BEGIN SSH2 PUBLIC KEY —-

Subject: Administrator

Comment: “Administrator@leolee-PC”

ModBitSize: 1024

AAAAB3NzaC1yc2EAAAADAQABAAAAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==

—- END SSH2 PUBLIC KEY —-

上传到服务器(利用rz,需要服务器安装软件支持)之后,使用ssh-keygen -i –f pubkey.file来转换非OpenSSH的pubkey格式

转换为:

ssh-rsa xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==

实际上就是去掉了头尾注释 变成一行,然后加了前缀“ssh-rsa”和空格

1.2 在服务器端增加用户

脚本:/bin/sh key.sh test

test 是需要新增的用户名,

Key.sh:

#!/bin/sh

if [ $# -eq 1 ];then

/usr/sbin/userdel $1;

/usr/sbin/groupdel $1;

/usr/sbin/groupadd $1;

#/usr/sbin/useradd $1;

/usr/sbin/useradd -g $1 $1;

/usr/bin/passwd -u $1;

rm -rf /home/$1;

mkdir /home/$1

mkdir /home/$1/.ssh;

cp /key/$1.key  /home/$1/$1.key;

cat /home/$1/$1.key > /home/$1/.ssh/authorized_keys;

chown -R $1 /home/$1;

chgrp -R $1 /home/$1;

chmod 700 /home/$1;

chmod 700 /home/$1/.ssh;

chmod 600 /home/$1/.ssh/*;

chown -R $1 /home/$1/.ssh;

rm -f /home/$1/$1.key;

Fi

请注意:

chmod 700 /home/$1;

chmod 700 /home/$1/.ssh;

chmod 600 /home/$1/.ssh/*;

还有解锁用户,ubuntu貌似出点小问题,解锁不了 ,我直接修改:

Vi /etc/shadow

test:!:14999:0:99999:7:::

将!号去掉

执行完毕后:

root@SNP02:/key# cd /home/test/

root@SNP02:/home/test# ll

total 12

drwx—— 3 test test 4096 Jan 25 16:40 ./

drwxr-xr-x 9 root  root  4096 Jan 26 12:57 ../

drwx—— 2 test test 4096 Jan 25 16:46 .ssh/

root@SNP02:/home/test# ll .ssh/

total 12

drwx—— 2 test test 4096 Jan 25 16:46 ./

drwx—— 3 test test 4096 Jan 25 16:40 ../

-rw——- 1 test test  213 Jan 25 16:46 authorized_keys

1.3 配置ssh

cat /etc/ssh/sshd_config

Port 22

Protocol 2

HostKey /etc/ssh/ssh_host_rsa_key

HostKey /etc/ssh/ssh_host_dsa_key

UsePrivilegeSeparation yes

KeyRegenerationInterval 3600

ServerKeyBits 768

SyslogFacility AUTH

LogLevel INFO

LoginGraceTime 120

PermitRootLogin no

StrictModes yes

RSAAuthentication yes

PubkeyAuthentication yes

AuthorizedKeysFile .ssh/authorized_keys

IgnoreRhosts yes

RhostsRSAAuthentication no

HostbasedAuthentication no

PermitEmptyPasswords no

ChallengeResponseAuthentication no

X11Forwarding yes

X11DisplayOffset 10

PrintMotd no

PrintLastLog yes

TCPKeepAlive yes

AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

AllowUsers *

UsePAM no

备注:

可以参考:

http://blog.licess.org/sshd_config/



版权声明:本文为weixin_33735077原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。