注意: 此角色已迁移至我们的 hardening-collection:
请在那里提交任何 issue 和 pull request!
network_ipv6_enable
ssh_listen_to 也必须设置为监听 IPv6 地址(例如 [::])。ssh_server_ports
ssh_client_port
ssh_listen_to
ssh_host_key_files
ssh_host_key_algorithms
ssh_client_alive_interval
ssh_client_alive_count
ssh_permit_tunnel
ssh_remote_hosts
defaults/main.yml 中的示例。ssh_permit_root_login
without-password 或 yes 以启用 root 登录ssh_allow_tcp_forwarding
'no' 禁用 TCP 转发。设置为 'yes' 允许 TCP 转发。如果您使用 OpenSSH >= 6.2 版本,可以指定 'yes'、'no'、'all' 或 'local'。 'yes' 和 'no' 应带引号传递。ssh_gateway_ports
falsefalse 禁用将转发端口绑定到非回环地址。设置为 true 强制绑定到通配符地址。设置为 clientspecified 允许客户端指定要绑定的地址。ssh_allow_agent_forwarding
ssh_x11_forwarding
ssh_pam_support
ssh_use_pam
ssh_gssapi_support
ssh_kerberos_support
ssh_deny_users
ssh_allow_users
ssh_deny_groups
ssh_allow_groups
ssh_authorized_keys_file
ssh_trusted_user_ca_keys_file
ssh_trusted_user_ca_keys
ssh_trusted_user_ca_keys_file 时使用。ssh_authorized_principals_file
ssh_trusted_user_ca_keys_file 时使用。ssh_authorized_principals
default_custom.yml。仅在设置了 ssh_authorized_principals_file 时使用。ssh_print_motd
ssh_print_pam_motd
ssh_print_last_log
sftp_enabled
sftp_umask
sftp_chroot
sftp_chroot_dir
ssh_client_roaming
sshd_moduli_file
sshd_moduli_minimum
ssh_challengeresponseauthentication
ssh_client_password_login
true 允许 ssh 客户端使用基于密码的认证ssh_server_password_login
true 允许 ssh 服务器使用基于密码的认证ssh_banner
falsetrue 在登录时打印横幅ssh_banner_path
ssh_client_hardening
truefalse 停止加固客户端ssh_client_port
'22'ssh_client_compression
falsessh_compression
falsessh_login_grace_time
30sssh_max_auth_retries
2ssh_max_sessions
10ssh_print_debian_banner
falsetrue 打印 Debian 特定的横幅ssh_server_enabled
truefalse 禁用 opensshd 服务器ssh_server_hardening
truefalse 停止加固服务器ssh_server_match_address
ssh_server_match_group
ssh_server_match_user
ssh_server_match_local_port
ssh_server_permit_environment_vars
noyes 指定 sshd 处理 ~/.ssh/environment 和 ~/.ssh/authorized_keys 中的 environment= 选项。使用 openssh 7.8 版本时,除了全局的 "yes" 或 "no" 设置外,还可以指定环境变量名称的白名单。ssh_server_accept_env_vars
ssh_use_dns
falsessh_server_revoked_keys
ssh_max_startups
ssh_macs
defaults/main.ymlssh_kex
defaults/main.ymlssh_ciphers
defaults/main.ymlssh_custom_options
sshd_custom_options
sshd_syslog_facility
sshd_log_level
sshd_strict_modes
sshd_authenticationmethods
publickeydefaults/main.yml如果您想配置上面未列出的 ssh 选项,可以使用 ssh_custom_options(用于 /etc/ssh/ssh_config)或 sshd_custom_options(用于 /etc/ssh/sshd_config)进行设置。这些选项将设置在文件的开头,因此您可以在文件后面覆盖这些选项。
示例 playbook:
- hosts: localhost
roles:
- dev-sec.ssh-hardening
vars:
ssh_custom_options:
- "Include /etc/ssh/ssh_config.d/*"
sshd_custom_options:
- "AcceptEnv LANG"
此角色使用默认端口 22 或清单中配置的端口来连接服务器。如果通过 ssh_server_ports 更改了默认的 ssh 端口,一旦 ssh 服务器重启,它仍将尝试使用之前的端口连接。为了在同一服务器上再次运行此角色,必须更新清单以使用新的 ssh 端口。
如果幂等性很重要,请考虑使用角色 ssh-hardening-fallback,它是此角色的包装器,当配置的端口不可达时会回退到端口 22。
- hosts: localhost
roles:
- dev-sec.ssh-hardening
本地测试此角色的首选方式是使用 Docker。您需要在系统上安装 Docker。请参阅入门获取适合您系统的 Docker 软件包。
您也可以使用 vagrant 和 Virtualbox 或 VMWare 在本地运行测试。您需要在系统上安装 Virtualbox 和 Vagrant。请参阅 Vagrant 下载获取适合您系统的 vagrant 软件包。我们所有的测试都使用 test-kitchen。如果您不熟悉 test-kitchen,请查看他们的指南。
接下来安装 test-kitchen:
# 安装依赖
gem install bundler
bundle install
# 在单台机器上快速测试
bundle exec kitchen test ssh-ubuntu1804-ansible-latest
# 在所有机器上测试
bundle exec kitchen test
# 用于开发
bundle exec kitchen create ssh-ubuntu1804-ansible-latest
bundle exec kitchen converge ssh-ubuntu1804-ansible-latest
bundle exec kitchen verify ssh-ubuntu1804-ansible-latest
# 清理
bundle exec kitchen destroy ssh-ubuntu1804-ansible-latest
# 在单台机器上快速测试
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen test ssh-ubuntu-1804
# 在所有机器上测试
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen test
# 用于开发
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen create ssh-ubuntu-1804
KITCHEN_YAML=".kitchen.vagrant.yml" bundle exec kitchen converge ssh-ubuntu-1804
更多信息请参阅 test-kitchen
我无法登录我的账户。我已经注册了客户端密钥,但它仍然不允许我登录。
如果您已经排除了所有典型问题(防火墙、网络、密钥缺失、密钥错误、账户禁用等),可能是您的账户被锁定了。最快的排查方法是查看您的用户的密码哈希:
sudo grep myuser /etc/shadow
如果哈希中包含 !,则您的账户已被锁定:
myuser:!:16280:7:60:7:::
正确的解决方法是解锁账户(passwd -u myuser)。如果用户没有密码,您可以通过以下方式解锁:
usermod -p "*" myuser
或者,如果您打算使用 PAM,您已通过 ssh_use_pam: true 启用了它。PAM 将允许被锁定的用户使用密钥登录。
为什么我的应用程序无法再通过 SSH 连接?
始终先查看日志文件,如果可能,查看连接时客户端和服务器之间完成的协商。
我们见过一些应用程序(基于 python 和 ruby)的问题,这是由于它们使用了过时的加密套件。这与本加固模块冲突,该模块将加密算法、消息认证码(MAC)和密钥交换(KEX)算法的列表缩减为更安全的选择。
使用此角色后,Ansible 的 template/copy/file 模块不再工作!
此角色默认禁用 SFTP。Ansible 默认使用 SFTP 将文件传输到远程主机。您必须在 ansible.cfg 中设置 scp_if_ssh = True。这样 Ansible 将使用 SCP 复制文件。或者,您可以通过将 sftp_enabled 设置为 true 重新启用 SFTP。
由于权限不足,无法重启 sshd 服务
如果在运行处理程序 "restart sshd" 时收到以下错误
Unable to restart service ssh: Failed to restart ssh.service: Access denied
或
failure 1 running systemctl show for 'ssh': Failed to connect to bus: No such file or directory
请以 root 身份运行 playbook(不要在 playbook 级别使用 become: yes),或者在处理程序中添加 become: yes。
这是 Ansible 的一个 bug:请参阅此处和此处了解更多信息。
请参阅贡献者指南。
根据 Apache 许可证 2.0 版("许可证")授权; 除非遵守许可证,否则您不得使用此文件。 您可以在以下位置获取许可证副本:
http://www.apache.org/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则按许可证分发的软件按"原样"分发, 不附带任何明示或暗示的保证或条件。 请参阅许可证了解特定语言下的权限和限制。