Centos ssh 服务配置

Openssh是ssh开源的实现

配置文件/etc/ssh 两个配置文件:

ssh_config    客户端工具的配置文件(基本用不着因为现在都是使用工具链接较多,xshell,等工具) sshd_config   服务器端的配置文件


配置文件/etc/ssh/sshd_config常用参数解释

#Port 22            // 默认监听端口 #ListenAddress 0.0.0.0 //监听地址,0.0.0.0表示本机的所有IP(服务网卡可以有多个IP地址,可以选择个IP允许ssh服务接入) #AdressFamily any (使用ipv4或ipv6协议) #Protocol 2          版本 ssh v2 #HostKey /etc/ssh/ssh_host_rsa_key    主机rsa的密钥 #HostKey /etc/ssh/ssh_host_dsa_key    主机dsa的密钥 #KeyRegenerationInterval 1h      会话密钥重新获取的时间(服务器给别人的密钥只能使用1个小时) #ServerKeyBits 1024        密钥的长度 #LoginGraceTime 2m        登录宽限时间(登录输入账号密码时间为2分钟) #PermitRootLogin yes        是否允许root登录 建议禁止 #MaxAuthTries 6          最多允许密码重试的次数 #MaxSessions 10            同时可以多少接受多少个链接数 #PubkeyAuthentication yes      是否启用公钥验证 #AuthorizedKeysFile     .ssh/authorized_keys  公钥所存放的位置(在用户家目录下) #PermitEmptyPasswords no      是否允许空密码登录 PasswordAuthentication yes      是否启用口令认证 UsePAM yes          PAM验证(如果安全要求高的地方,改为no#UseDNS yes          是否启用反向解析  建议禁止 DenyUsers          拒绝ssh连接的用户 AllowUsers          允许ssh连接的用户 DenyGroups          拒绝


修改完配置文件重启服务sshd

service sshd restart


查看守护进程:sshd

[root@localhost ~]# netstat -nltu |grep 22 tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN tcp        0      0 :::22                       :::*                        LISTEN [root@localhost ~]#


(一)ssh 免密码方式登录案例,ssh服务器生成私钥和公钥有2种方式,交换式和直接命令式,哪一种都行,用一种即可,公钥是给用户,私钥是服务器自己保留,用户拿公钥,私钥会验证。

1.在ssh服务器交换式192.168.100.100上生成私钥和公钥

[root@localhost ~]# ssh-keygen -t rsa                            (-t指定加密算法的类型 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):password            (可以为空,也可以自己想一个密码,会把密码生成密钥)                       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: 78:eb:4e:f8:c4:2a:56:60:57:55:55:2a:44:32:3d:4b root@localhost.localdomain  The key's randomart image is: +--[ RSA 2048]----+ |         .+++....| |        .  +E  . | |       .   ..o.  | |    o ..    ..   | |   . o. S        | |      .+ .       | |     .. =        | |    o  *         | |   . ...+        | +-----------------+ [root@localhost ~]# [root@localhost ~]# ssh-copy-id  root@192.168.100.101    (把公钥拷贝给另外一台服务器192.168.100.101) root@192.168.100.101's password: Now try logging into the machine, with "ssh 'root@192.168.100.101'", and check in:   .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. [root@localhost ~]#

在ssh服务器192.168.100.100上生成私钥和公钥

[root@localhost .ssh]# ssh-keygen -t rsa -P '1234@qwerT' -f '/root/.ssh/id_rsa'  (—P 指定密码也可以为空, -f指定密钥存放位置) Generating public/private rsa key pair. 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: d3:d7:64:bd:57:1d:ab:5e:2f:7b:96:cc:61:b8:f3:63 root@localhost.localdomain The key's randomart image is: +--[ RSA 2048]----+ |     |               .+| |              o.+| |         .   +. o| |        S . ..oo.| |         . ....oo| |             .* +| |             o Eo| |              =+.| +-----------------+ [root@localhost ~]# ssh-copy-id root@192.168.100.101    (把公钥拷贝给另外一台服务器192.168.100.101) root@192.168.100.101's password: Now try logging into the machine, with "ssh 'root@192.168.100.101'", and check in:     .ssh/authorized_keys   to make sure we haven't added extra keys that you weren't expecting.   [root@localhost ~]#  


2.另外一台服务器192.168.100.101,就可以实现101免密码登录100了


[root@localhost ~]# ls /root/.ssh/ authorized_keys [root@localhost ~]# 


(二)如何安全使用ssh建议:

1、密码一定要经常更改且复杂

2、使用非默认端口

3、限制登录用户的地址

4、禁止root直接登录

5、仅有限的用户登录

6、使用基于密钥对认证

7、禁止使用版本1

您可能还会对下面的文章感兴趣: