SSH 证书认证配置方法

来源:互联网 发布:根据mac查ip 编辑:程序博客网 时间:2024/05/17 00:57

1、默认安装完以后用VI打开 /etc/ssh/sshd_config 修改几行内容就可以了
#ServerKeyBits 768 注释取消,将768改为1024
#PermitRootLogin yes 注释取消,将yes改为no 禁止root登录
#PermitEmptyPasswords no 取消注释,禁止空密码登录
#PasswordAuthentication no 取消注释,禁止使用密码方式登录,有密钥谁还用密码啊
注意一下,在centos5.0之前SSH服务需要指明版本,#Protocol 2,1 把前面的注释取消,选择自己需要的版本就行了。
保存退出。如果想做到最大化安全链接,可以考虑在配置有双网卡的服务器上设置只允许内网链接SSH,方法很简单,在/etc/hosts.deny文件最后一行添加一句sshd: ALL 然后在/etc/hosts.allow的最后一行加上一句sshd: 192.168.0. 然后保存退出。
重启一下SSH服务 service sshd restart 就OK了


2、制作密钥
先切换进入一个wheel组的普通用户,输入 ssh-keygen -t rsa.
第一步会让你先确认钥匙的文件名。保持默认就可以了。然后输入这个密钥的口令,再确认一次就可以了。
然后cd ~/.ssh 查看一下钥匙是不是都已经建立了。将公钥更改名称后删除
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
rm -rf ~/.ssh/id_rsa.pub 别把密钥误删就行了。
将公钥文件属性更改为400禁止被篡改
chmod 400 ~/.ssh/authorized_keys
剩下的就没什么了,把密钥COPY到U盘还是FTP服务器再转移或者是复制到磁盘上就看你自己的需要了。
测试一下看服务能不能正常连接
打开puttygen-x86.exe 在程序下面选择SSH-2(RSA)密钥强度改为1024,然后点击"Load"
选取服务器端生成的私钥(文件类型要选择“All Files”)如果没有改名字的话我们选的应该是id_rsa这个文件,开始转换私钥,这里需要输入在服务器端建立此私钥时的口令。在文本框中输入口令开始转换,保存转换后的私钥匙到适当的位置(转换后的私钥将做为PuTTY登录到服务器时使用的私钥)。点击“Save private key”,并选择适当的位置保存私钥。
PUTTY 估计做网管的朋友都很熟悉,下载地址就不废话了,没有可以下载我附件里提供的,我感觉这样的兵器绝对是每人必备一把的,双击启动 PuTTY ,在左侧找到Auth(认证方式)一项,点击Browse,选择刚刚用PuTTYGen转换后的私钥。然后点击左侧的Session,回到主机连接信息的设置。剩下的我就不罗嗦了,自己试试就行了。
 
服务器间通讯
假设两台Linux服务器:server1和server2,我们以用户dboper为例(即两台服务器上均创建有dboper用户)。
在server1上,以dboper登录。执行如下命令:
ssh-keygen -d
Generating public/private dsa key pair.
Enter file in which to save the key (/home/wuysh/.ssh/id_dsa): (采用缺省值,回车)
Enter passphrase (empty for no passphrase): (不用密码了,回车)
Enter same passphrase again: (回车)
Your identification has been saved in /home/wuysh/.ssh/id_dsa.
Your public key has been saved in /home/wuysh/.ssh/id_dsa.pub.
The key fingerprint is:
b2:a4:92:12:7f:15:9b:89:48:2a:7c:3a:d6:17:ea:ab dboper@localhost.localdomain
这样,在用户的.ssh目录下生成如下两个文件:
id_dsa
id_dsa.pub
在用户的.ssh目录下,将id_dsa.pub复制一份成 authorized_keys1 文件, 并将 authorized_keys1 的属性改为 600 即文件所属用户有读和写的权限,组用户和其它用户禁止读写。
cp id_dsa.pub authorized_keys1
chmod 600 authorized_keys1
然后在server2上,也以dboper登录,也执行上面的一次操作。
这样,两台服务器上都生成了用户的公钥和私钥,接下来就是进行互相认证了。即把公钥放到对方服务器。
server2信认server1,即server1登录server2不用输入密码:
复制server1上的id_dsa.pub到server2(不要覆盖server2上的id_dsa.pub),复制成id_dsa.pub_server1。然后将id_dsa.pub_server1的内容添加到server2上的dboper用户.ssh目录下的authorized_keys文件中
cat id_dsa.pub_server1 >> authorized_keys
OK,现在你在server1上以dboper用户来登录server2,将不再需要密码了。
ssh dboper@server2
立即进入,没有密码输入提示了。
如果要在server1上信认server2,按上面的方法,把server2上的id_dsa.pub中的内容添加到server1上用户.ssh目录下的authorized_keys文件中。
Linux系统导入xshell中导入的是私钥
xshell导入Linux系统导入的是公钥
Linux系统间认证交换的是公钥


#$OpenBSD: sshd_config,v 1.73 2005/12/06 22:38:28 reyk Exp $# This is the sshd server system-wide configuration file.  See# sshd_config(5) for more information.# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin# The strategy used for options in the default sshd_config shipped with# OpenSSH is to specify options with their default value where# possible, but leave them commented.  Uncommented options change a# default value.#Port 22#Protocol 2,1Protocol 2#AddressFamily any#ListenAddress 0.0.0.0#ListenAddress ::# HostKey for protocol version 1#HostKey /etc/ssh/ssh_host_key# HostKeys for protocol version 2#HostKey /etc/ssh/ssh_host_rsa_key#HostKey /etc/ssh/ssh_host_dsa_key# Lifetime and size of ephemeral version 1 server key#KeyRegenerationInterval 1h#ServerKeyBits 768# Logging# obsoletes QuietMode and FascistLogging#SyslogFacility AUTHSyslogFacility AUTHPRIV#LogLevel INFO# Authentication:#LoginGraceTime 2m#PermitRootLogin yes#StrictModes yes#MaxAuthTries 6#RSAAuthentication yes#PubkeyAuthentication yesAuthorizedKeysFile.ssh/authorized_keys# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts#RhostsRSAAuthentication no# similar for protocol version 2#HostbasedAuthentication no# Change to yes if you don't trust ~/.ssh/known_hosts for# RhostsRSAAuthentication and HostbasedAuthentication#IgnoreUserKnownHosts no# Don't read the user's ~/.rhosts and ~/.shosts files#IgnoreRhosts yes# To disable tunneled clear text passwords, change to no here!#PasswordAuthentication yes#PermitEmptyPasswords noPasswordAuthentication yes# Change to no to disable s/key passwords#ChallengeResponseAuthentication yesChallengeResponseAuthentication no# Kerberos options#KerberosAuthentication no#KerberosOrLocalPasswd yes#KerberosTicketCleanup yes#KerberosGetAFSToken no# GSSAPI options#GSSAPIAuthentication noGSSAPIAuthentication yes#GSSAPICleanupCredentials yesGSSAPICleanupCredentials yes# Set this to 'yes' to enable PAM authentication, account processing, # and session processing. If this is enabled, PAM authentication will # be allowed through the ChallengeResponseAuthentication mechanism. # Depending on your PAM configuration, this may bypass the setting of # PasswordAuthentication, PermitEmptyPasswords, and # "PermitRootLogin without-password". If you just want the PAM account and # session checks to run without PAM authentication, then enable this but set # ChallengeResponseAuthentication=no#UsePAM noUsePAM yes# Accept locale-related environment variablesAcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL#AllowTcpForwarding yes#GatewayPorts no#X11Forwarding noX11Forwarding yes#X11DisplayOffset 10#X11UseLocalhost yes#PrintMotd yes#PrintLastLog yes#TCPKeepAlive yes#UseLogin no#UsePrivilegeSeparation yes#PermitUserEnvironment no#Compression delayed#ClientAliveInterval 0#ClientAliveCountMax 3#ShowPatchLevel no#UseDNS yes#PidFile /var/run/sshd.pid#MaxStartups 10#PermitTunnel no#ChrootDirectory none# no default banner path#Banner /some/path# override default of no subsystemsSubsystemsftp/usr/libexec/openssh/sftp-server


原创粉丝点击