OpenSSH服务器安装和配置

时间:2020-03-21 11:46:28  来源:igfitidea点击:

今天的计划是在本地Debian VM上安装OpenSSH服务器,为普通用户生成公共和私有SSH密钥,并将SSH服务器配置为使用禁用了密码认证的公共密钥认证。

安装OpenSSH服务器

安装如下所示。

# apt-get update && apt-get install openssh-server

生成公共和私有SSH密钥

我们将与要为其生成密钥的用户(在这种情况下为桑迪)一起做所有事情,而不是与root一起做。

生成公钥和私钥:

$ssh-keygen -b 2048 -t rsa -C "Hyman@theitroad" -f ~/my_key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/sandy/my_key.
Your public key has been saved in /home/sandy/my_key.pub.
The key fingerprint is:
76:18:5f:94:10:d4:7f:64:b8:d3:76:e1:a7:af:c7:0d Hyman@theitroad
The key's randomart image is:
+--[ RSA 2048]----+
| .++.. . S o ++  |
+-----------------+

创建一个目录,我们将使用该目录来存储公钥:

$mkdir ~/.ssh 
$chmod 0700 ~/.ssh

将公钥移到新目录中:

$mv ~/my_key.pub ~/.ssh/my_key

为了方便起见,重命名私钥:

$mv ~/my_key ~/my_key.pem

将公用密钥和专用密钥的权限更改为所有者只读:

$chmod 0400 ~/.ssh/my_key ~/my_key.pem

我们需要将私钥从Debian服务器复制到PC上并保持安全。

配置OpenSSH服务器

我们需要打开OpenSSH配置文件进行编辑:

# vim /etc/ssh/sshd_config

请注意,根据操作系统的不同,下面提供的某些配置选项可能已经设置。

禁用OpenSSH标语

DebianBanner no

更改默认端口并将协议设置为版本2

Port 12 # (change to any unassigned privileged port)
ListenAddress 0.0.0.0 # (listen on IPv4 only)
Protocol 2 # (disable the protocol version 1 since it's been exposed)

使用批准的密码

将密码限制为经过FIPS批准的密码,并且仅在计数器(CTR)模式下使用密码。

Ciphers aes128-ctr,aes192-ctr,aes256-ctr

man sshd_config(5)列出当前SSH守护程序支持的密码列表。

协议版本2的主机密钥

HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key

创建非特权子进程(经过身份验证的用户的特权)

UsePrivilegeSeparation yes

设置服务器密钥参数

KeyRegenerationInterval 3600
ServerKeyBits 2048 # (applies to the protocol version 1)

启用记录

SyslogFacility AUTH # (this goes to /var/log/auth.log)
LogLevel INFO # (info is fine for basic failed login attempts)

如果一分钟内未成功登录,则断开连接

LoginGraceTime 60

用户闲置10分钟后,超时SSH连接

ClientAliveInterval 600
ClientAliveCountMax 3

禁用root登录

PermitRootLogin no # (Jan be set to without-password if in use with a private key)

登录前检查文件模式和用户文件的所有权

StrictModes yes

定义可以通过SSH访问的用户和组

# whitespaces separated users lists
AllowUsers sandy # (only sandy's account is allowed to login via SSH)
DenyUsers root
DenyGroups root

不查找远程主机名

UseDNS no

禁用密码身份验证并禁用空密码

PasswordAuthentication no
PermitEmptyPasswords no

启用公钥身份验证

PubkeyAuthentication yes
AuthorizedKeyFile %h/.ssh/my_key

禁用通过rhosts文件的不安全访问

SSH可以模仿过时的rsh命令的行为,以允许用户通过'.rhosts'文件启用对帐户的不安全访问。

IgnoreRhosts yes

禁用基于主机的身份验证

不建议主机单方面相互信任。

HostBasedAuthentication no

出于安全目的禁用不必要的身份验证机制

RSAAuthentication no
RhostRSAAuthentication no
ChallengeResponseAuthentication no
KerberosAuthentication no
GSSAPIAuthentication no

不允许用户设置环境选项

防止用户潜在地绕过某些访问限制。

PermitUserEnvironment no

成功认证后启用压缩

Compression delayed

限制身份验证尝试次数和最大会话数

MaxAuthTries 2 # (login attempts per connection)
MaxSessions 10

禁用X转发

X11Forwarding no
X11DisplayOffset 15 # (just in case we need to change the above to "yes")

打印上次用户登录的日期和时间

PrintLastLog yes

禁用每日消息-不需要

PrintMotd no

将TCP Keepalive消息发送到另一端

TCPKeepAlive yes

交互式登录会话无登录

UseLogin no

显示横幅(通常是一些令人恐惧的文字)

Banner /etc/issue.net

允许客户端传递区域设置环境变量

AcceptEnv LANG LC_*

实施SFTP文件传输子系统

Subsystem sftp /usr/lib/openssh/sftp-server

禁用可插拔身份验证模块接口

UsePAM no

重新启动SSH服务

# service ssh restart

配置iptables

# iptables -A INPUT -p tcp --dport 12 -j ACCEPT

连接到OpenSSH服务器

$ssh -24x -i /path/to/file/my_key.pem Hyman@theitroad_ip -p12

检查SSH日志中是否有无效用户(尝试入侵)

# cat /var/log/auth.log | grep "Invalid user" | cut -d " " -f 1-3,6-11 | uniq | sort
 Nov 11 19:31:33 Invalid user patrick from 10.131.14.38

OpenSSH配置选项进行复制

DebianBanner no
Port 12 
ListenAddress 0.0.0.0 
Protocol 2
Ciphers aes128-ctr,aes192-ctr,aes256-ctr
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
UsePrivilegeSeparation yes
KeyRegenerationInterval 3600
ServerKeyBits 2048 
SyslogFacility AUTH 
LogLevel INFO 
LoginGraceTime 60
ClientAliveInterval 600
ClientAliveCountMax 3
PermitRootLogin no 
StrictModes yes
AllowUsers sandy 
DenyUsers root
DenyGroups root
UseDNS no
PasswordAuthentication no
PermitEmptyPasswords no
PubkeyAuthentication yes
AuthorizedKeyFile %h/.ssh/my_key
IgnoreRhosts yes
RSAAuthentication no
RhostRSAAuthentication no
HostBasedAuthentication no
ChallengeResponseAuthentication no
KerberosAuthentication no
GSSAPIAuthentication no
PermitUserEnvironment no
Compression delayed
MaxAuthTries 2 
MaxSessions 10
X11Forwarding no
X11DisplayOffset 15 
PrintLastLog yes
PrintMotd no
TCPKeepAlive yes
UseLogin no
Banner /etc/issue.net
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
UsePAM no