声明:本文比较菜,高手可以直接忽略。。
本人最近一直在学习centos,一般来说,我们都会用PUTTY,用默认的SSH使用密码进行远程登录管理,但是从安全角度上来说,这种做法只适合做实验来玩玩,下面我们来学习下使用SSH进行密钥登录。
系统:centos5.3
首先我们使用root登录本机,然后cd /etc/ssh,会看到有一个文件sshd_config,这个就是ssh服务的配置文件,下面我们来进行配置。使用命令vi sshd_config
# $OpenBSD: sshd_config,v 1.69 2004/05/23 23:59:53 dtucker 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 //配置端口,默认端口为22,我们可以自己设置一个
Protocol 2
#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 1024
# Logging
#obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
PermitRootLogin no //这里设置禁止root远程登录
#StrictModes yes
#MaxAuthTries 6
#RSAAuthentication yes
PubkeyAuthentication yes //使用key登录
#AuthorizedKeysFile .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 no
PermitEmptyPasswords no
PasswordAuthentication no //不允许password登录
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
# GSSAPI options
#GSSAPIAuthentication no
GSSAPIAuthentication yes
#GSSAPICleanupCredentials yes
GSSAPICleanupCredentials 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 no
UsePAM yes
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression yes
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10
#ShowPatchLevel no
# no default banner path
#Banner /some/path
# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server
上面我标注的就是要修改的地方,因为我是关闭了远程密码登录,你也可以选择开启,这个是随意的。设置好以后我们保存退出。
然后我们新建账户$useradd xxxx xxxx为账户名
切换到新建账户下$su - xxxx
生成公钥/私钥对$ssh-keygen -t rsa
切换到.ssh目录下$cd .ssh
$cat id_rsa.pub >> ~/.ssh/authorized_keys
把公钥文件authorized_keys权限设置为400$chmod 400 authorized_keys //这里如果不设置,你在客户机生成私钥以后,会提示你安全性不够高。
到了这里,基本上服务器就已经设置完成了,然后我们用WinSCPPortable把.ssh/id_rsa文件下载到本地电脑上,打开puttygen,点击load,导入id_rsa,密码自己设置,直接点击save private key,生成私钥文件。这时我们再去服务器上把sshd服务重启一下,然后用putty进行登录,用putty进行登录,端口设置为我们设置的,connection -> SSH -> Auth 中private key file authentication 设置为经过转换的密钥,保存以后直接双击,就出现登录界面,用我们新建的账户跟生成私钥时设置的密码进行登录就OK了~