【OpenSSH服务器端组件-sshd】
配置文件:/etc/ssh/sshd_config(通过修改此文件可以修改ssh的默认监听端口与其他参数)
服务脚本:/etc/rc.d/init.d/sshd
服务启动|停止|重启:serveice sshd start|stop|restart
脚本配置文件:/etc/sysconfig/sshd
配置参数
# man sshd_config 查看配置参数的说明;
# vim /etc/sysconfig/sshd 通过编辑配置文件来修改配置参数;
#+空格+文字:以此格式开头的行表示改行为注释说明;
#+文字:以此格式开头的行表示可启用选项,不改变则表示使用该选项的默认设置,反之使用设定值“#”要去掉哦!
例:#Port 22 如不去掉#且22不变,表示使用默认的22号端口;
若把#Port 22改成port 7777,表示把sshd的监听端口改成7777;
注意:修改参数与配置后,必须重启服务(service sshd restart).
经常需要修改的参数:
[root@www ~]# cat /etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm 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 #修改默认监听的端口;
port 7777 #把sshd的监听端口改成7777;
#AddressFamily any #监听的地址家族,指定是监听在IPV4上还是IPV6上,any表示所有;
#ListenAddress 0.0.0.0 #指定监听的地址 (0.0.0.0表示本机的所有地址);
#ListenAddress ::
# Disable legacy (protocol version 1) support in the server for new
# installations. In future the default will change to require explicit
# activation of protocol 1
Protocol 2
# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key #使用shhv1用到的主机密钥;
# 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 yes #是否允许管理员直接登录;
#StrictModes yes
#MaxAuthTries 6 #最大密码输入错误次数;
#MaxSessions 10 #最大会话个数;
#RSAAuthentication yes #是否允许使用RSA机制来认证;
#PubkeyAuthentication yes
#--------中间不长改变的配置参数略----------
Subsystem sftp /usr/libexec/openssh/sftp-server #表示是否启动sftp功能;
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# ForceCommand cvs server
sshd认证方式:
1、基于口令的认证;
2、基于密钥的认证;
# ssh-keygen -t rsa 用rsa算法生成密钥,默认密钥为id_rsa(私钥), id_rsa.pub(公钥)
# ssh-keygen -f /path/to/somefile -P oldpassword 根据现有的密钥文件生成密钥
-f /path/to/somefile: 密钥文件保存在的位置;
-P '': 指定生成旧密钥时使用的密码;
方法一:把本地主机生成的公钥 id_rsa.pub使用scp复制到远程主机的上,在远程主机使用cat id_rsa.pub>>.ssh/authorized_keys追加该公钥信息,这样就可以实现基于密钥认证的ssh登录;
方法二:# ssh-copy-id -i .ssh/id_rsa.pub USERNAME@HOST
[root@www ~]# ssh-keygen -t rsa #用rsa算法生成密钥;
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #指定密钥存放路径及名称,一般不用
#修改,直接回车;
Enter passphrase (empty for no passphrase): #输入私钥密码;
Enter same passphrase again: #确认输入私钥密码;
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
c2:f9:c2:3d:4d:ca:52:39:7a:a7:33:de:42:11:d3:8f root@
The key's randomart image is:
+--[ RSA 2048]----+
| . |
| o . |
| o o |
| . ...E . |
| + S.. |
| . B.= |
| =.B o |
| ++= |
| .o+. |
+-----------------+
[root@www ~]# ssh-keygen -f .ssh/id_rsa -P '' #根据现有密钥文件重新生成密钥;
Generating public/private rsa key pair.
.ssh/id_rsa already exists.
Overwrite (y/n)? y #提示是否确定要覆盖;
Your identification has been saved in .ssh/id_rsa.
Your public key has been saved in .ssh/id_rsa.pub.
The key fingerprint is:
bf:55:f0:0b:a5:ee:4e:4a:1d:d3:b1:0e:66:ee:55:9b root@
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| . o |
| * o |
| S O = .|
| . * B oo|
| o * +E |
| . B . |
| o.+ |
+-----------------+
#-----使用方法一:实现通过密钥文件完成身份验证(不需要输入密码)-----
[root@www ~]# scp .ssh/id_rsa.pub root@192.168.0.110:/root/ #使用spc命令复制公钥文件到远程
#主机的用户家目录下的.ss/路径下;
root@192.168.0.110's password: #输入登录远程主机的密码;
id_rsa.pub 100% 397 0.4KB/s 00:00 #提示复制成功;
[root@mailCentOS6 ~]# ls .ssh/ #验证确认文件复制成功;
id_rsa.pub known_hosts
[root@mailCentOS6 ~]# touch .ssh/authorized_keys #路径内没有自动验证密钥文件,创建一个;
[root@mailCentOS6 ~]# cat .ssh/id_rsa.pub >> .ssh/authorized_keys #把公钥追加到自动验证密钥文件;
[root@www ~]# ssh 192.168.0.110
Last login: Mon May 11 20:45:10 2015 from 192.168.0.111
[root@mailCentOS6 ~]# #OK了,看到了没有,不用输入密码我们就直接可以远程登录了!!
#-----使用方法二:实现通过密钥文件完成身份验证(不需要输入密码)-----
[root@mailCentOS6 ~]# rm -f .ssh/authorized_keys #删除原有保存的自动验证密钥文件;
[root@www ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.0.110 #使用命令自动传输生成自动验证密钥文件;
root@192.168.0.110's password:
Now try logging into the machine, with "ssh 'root@192.168.0.110'", and check in:
.ssh/authorized_keys #提示生成的文件;
to make sure we haven't added extra keys that you weren't expecting.
[root@www ~]# ssh 192.168.0.110 #验证看看是否可以登录;
Last login: Mon May 11 21:02:29 2015 from 192.168.0.111
[root@mailCentOS6 ~]# ls .ssh/ #看到了没有,我们现在已经登录到了mailCentOS6这台主机上了;
authorized_keys known_hosts
【命令补充】