sshd的配置和优化
sshd的配置和优化
sshd服务器端的配置文件为/etc/ssh_config
详细用法: [[email?protected] ~]# vim /etc/ssh/sshd_config Port 2222 [[email?protected] ~]# service sshd restart Stopping sshd: [ OK ] Starting sshd: [ OK ] #此时sshd服务已经监听在2222端口上 [[email?protected] ~]# lsof -i:2222 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 2424 root 3u IPv4 13945 0t0 TCP *:EtherNet/IP-1 (LISTEN) sshd 2424 root 4u IPv6 13947 0t0 TCP *:EtherNet/IP-1 (LISTEN) 2.LinstenAddress vim /etc/ssh/sshd_config Port 2222 ListenAddress 172.22.27.12 [[email?protected] ~]# service sshd restart Stopping sshd: [ OK ] Starting sshd: [ OK ] #此时sshd服务监听在172.22.27.124的2222端口上 [[email?protected] ~]# ss -tnl |grep 2222 LISTEN 0 128 172.22.27.124:2222 *:* 3.LoginGraceTime #LoginGraceTime 2m 4.PermitRootLogin [[email?protected] ~]# vim /etc/ssh/sshd_config Port 2222 ListenAddress 172.22.27.12 PermitRootLogin no 重启sshd服务 [[email?protected] ~]# service sshd restart Stopping sshd: [ OK ] Starting sshd: [ OK ] #此时使用root账户已经无法登录 [[email?protected] ~]# ssh -p 2222 [email?protected] The authenticity of host ‘[172.22.27.124]:2222 ([172.22.27.124]:2222)‘ can‘t be established. RSA key fingerprint is 46:d8:67:07:f3:51:87:95:2c:d7:4b:27:ce:85:a2:ed. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘[172.22.27.124]:2222‘ (RSA) to the list of known hosts. [email?protected]‘s password: Permission denied,please try again. [email?protected]‘s password: Permission denied,please try again. [email?protected]‘s password: Permission denied (publickey,password). 5.MaxAuthTries [[email?protected] ~]# vim /etc/ssh/sshd_config #MaxAuthTries 6 6.MaxSessions [[email?protected] ~]# vim /etc/ssh/sshd_config MaxSessions 3 重启服务 [[email?protected] ~]# service sshd restart Stopping sshd: [ OK ] Starting sshd: [ OK ] 此时克隆会话时最多只能克隆3个在多就会拒绝 7.PubkeyAuthentication [[email?protected] ~]# vim /etc/ssh/sshd_config #PubkeyAuthentication yes 8.PermitEmptyPasswords [[email?protected] ~]# vim /etc/ssh/sshd_config #PermitEmptyPasswords no 9.PasswordAuthentication [[email?protected] ~]# vim /etc/ssh/sshd_config PasswordAuthentication yes 10.GatewayPorts [[email?protected] ~]# vim /etc/ssh/sshd_config #ClientAliveInterval 0 #0表示不限制时间 #ClientAliveCountMax 3 11.UseDNS和GSSAPIAuthentication [[email?protected] ~]# vim /etc/ssh/sshd_config MaxStartups 10:30:100 12.Banner [[email?protected] ~]# vim /etc/ssh/sshd_config Banner /data/hello [[email?protected] ~]# echo hello > /data/hello #创建一个hello的文件 #重启服务 [[email?protected] ~]# service sshd restart Stopping sshd: [ OK ] Starting sshd: [ OK ] [[email?protected] ~]# ssh -p 2222 [email?protected] hello #所创建的登录前提示 [email?protected]‘s password: 13、AllowUsers、DenyUsers、AllowGroups、DenyGroups [[email?protected] ~]# vim /etc/ssh/sshd_config DenyUsers wang #将wang用户加入黑名单 #重启服务 [[email?protected] ~]# service sshd restart Stopping sshd: [ OK ] Starting sshd: [ OK ] [[email?protected] ~]# useradd wang [[email?protected] ~]# echo 222221 | passwd --stdin wang Changing password for user wang. passwd: all authentication tokens updated successfully. #此时wang用户无法登陆 [[email?protected] ~]# ssh -p 2222 [email?protected] hello [email?protected]‘s password: Permission denied,please try again. [email?protected]‘s password: Permission denied,please try again. [email?protected]‘s password: Permission denied (publickey,password).
1.建议使用非默认端口 修改port 2.禁止使用protocol version 1 Protocol 2 3.限制可登录用户AllowUsers、DenyUsers、AllowGroups、DenyGroups 4.设定空闲会话超时时长 ClientAliveInterval和ClientAliveCountMax 5.利用防火墙设置ssh访问策略 设置iptables策略 6.仅监听特定的IP地址 修改Listen 7.基于口令认证时,使用强密码策略 使用难以猜测的随机口令,长度越长约好,并定期修改。 8.使用基于密钥的认证 使用密钥登陆,不使用口令 9.禁止使用空密码 PermitEmptyPasswords no 10.禁止root用户直接登录 PermitRootLogin 11.限制ssh的访问频度和并发在线数 MaxStartups 12.经常分析日志 sshd的日志文件为/var/log/secure (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |