03-SSH server config
sshd服務器端配置
[root@husa ssh]# pwd
/etc/ssh
[root@husa ssh]# ls
moduli ssh_host_ecdsa_key ssh_host_ed25519_key.pub
ssh_config ssh_host_ecdsa_key.pub ssh_host_rsa_key
sshd_config ssh_host_ed25519_key ssh_host_rsa_key.pub
其中的公鑰私鑰用於做主機認證
sshd_config文件field explanation
#Port 22 #端口號
#AddressFamily any #監聽的socket類型,ipv4,ipv6
#ListenAddress 0.0.0.0 #監聽的主機的IP地址
#ListenAddress ::
#Protocol 2 #ssh版本
#KeyRegenerationInterval 1h #密鑰每隔多長時間更新一次
#ServerKeyBits 1024 #密鑰長度
SyslogFacility AUTHPRIV #日誌級別
#LoginGraceTime 2m #登陸的寬限期,這裏是2min
#PermitRootLogin yes #是否允許管理員直接登陸
#StrictModes yes #是否使用嚴格模式
#MaxAuthTries 6 #最大認證嘗試次數
#MaxSessions 10 #最大連接數
#HostbasedAuthentication no #要不要對主機進行密鑰認證
#PasswordAuthentication yes #是否允許基於口令認證
#PermitEmptyPasswords no #是否允許空密碼
ChallengeResponseAuthentication no #是否啓用對密鑰認證的密鑰 加密
X11Forwarding yes #是否啓用X11轉發
#PrintMotd yes #登陸時是否打印/etc/motd文件
#PrintLastLog yes #登陸時是否打印上次登錄信息
#UseDNS yes #是否通過DNS把client的IP反解爲主機名
Subsystem sftp /usr/libexec/openssh/sftp-server #sftp子系統
#---------------------------------------------#
#AllowUsers users... #表示允許登陸的用戶
#AllowGroups grps... #表示允許登陸的組
#DenyUsers users... #拒絕
#DenyGroups grps... #
SSH最佳實踐
1、禁止使用默認端口
2、禁止使用protocol version 1
3、顯示可登錄用戶
4、設定空閒會話超時時長
5、利用防火牆設置ssh訪問策略
6、進監聽特定的IP地址
7、基於口令認證時,使用強密碼策略
8、使用基於密鑰的認證
9、禁止使用空密碼
10、禁止root用戶直接登陸
11、限制ssh的訪問頻度和併發在線數
12、做好日誌,經常分析
/var/log/sercure
-
生成隨機密碼的方式
```
[root@husa ~]# openssl rand -base64 20
7lMw+ebwIN1Gabie4rdiOJUfQ6o=
[root@husa ~]# tr -dc A-Za-z0-9_ < /dev/urandom | head -c 30 | xargs
ZT9rALlH1Go4OsghRGwi1hCYi_PTrl
```