OpenSSH 证书登录提供了一种增强安全性的方法来验证用户或主机的身份,避免了传统密码认证可能带来的安全隐患。它包括单向(通常是客户端到服务器)和双向(客户端与服务器互相认证)两种认证方式。

这里记录一下如何使用 OpenSSH 的证书功能来进行多服务器的秘钥管理。
给服务器设置信任客户端证书
设置完成之后,可以实现:使用 user_ca 签发出的证书登录任意服务器。
使用到的命令:
 ssh-keygen -t rsa -b 4096 -f user_ca -C user_ca
 
 
  ssh-keygen -t rsa -b 4096 -f ssh_user_rsa_key
 
 
  ssh-keygen -s user_ca -I dev@example.com -n user1,user2 -V +30d ssh_user_rsa_key.pub
 
  ssh-keygen -L -f ssh_user_rsa_key-cert.pub
 
 
 
 
 
 
 
  TrustedUserCAKeys /etc/ssh/user_ca.pub
 
  systemctl restart sshd
 
 
  | 
 
给客户端设置信任服务端证书
设置完成之后可以实现:服务器会向连接的所有人提供自己的证书。
使用到的命令:
 ssh-keygen -t rsa -b 4096 -f host_ca -C host_ca
 
 
  ssh-keygen -t rsa -b 4096 -f ssh_host_rsa_key
 
 
  ssh-keygen -s host_ca -I host.example.com -h -n host.example.com -V +30d ssh_host_rsa_key.pub
  ssh-keygen -L -f ssh_host_rsa_key-cert.pub
 
 
 
 
 
 
 
 
  HostCertificate /etc/ssh/ssh_host_rsa_key-cert.pub
 
 
  TrustedUserCAKeys /etc/ssh/host_ca.pub
  @cert-authority host1,host2,*.example.com ssh-rsa xxxx这里是 host_ca.pub 的内容xxxxx
  systemctl restart sshd
 
 
  | 
 
通过 Ansible 脚本来进行批量部署
 --- - name: Manage SSH keys and certificates using OpenSSH   hosts: all   gather_facts: no   vars:     user_ca_path: "{{ playbook_dir }}/ssh/ansible_user_ca_key"     user_key_path: "{{ playbook_dir }}/ssh/ansible_ssh_user_rsa_key"     user_pub_path: "{{ playbook_dir }}/ssh/ansible_ssh_user_rsa_key.pub"     cert_validity_days: +30d     
    tasks:     - name: Generate the Ansible User CA and cert       delegate_to: localhost       openssh_keypair:         path: "{{ user_ca_path }}"         type: rsa         size: 4096         state: present         force: no
      - name: Ensure the Ansible User key exists       delegate_to: localhost       openssh_keypair:         path: "{{ user_key_path }}"         type: rsa         size: 4096         state: present         force: no
  - name: Configure SSHD on target servers   hosts: coreos   become: yes   vars:     user_ca_pub_path: "{{ playbook_dir }}/ssh/ansible_user_ca_key.pub"     user_key_path: "{{ playbook_dir }}/ssh/ansible_ssh_user_rsa_key"
    tasks:     - name: Copy the CA certificate to the target server       copy:         src: "{{ user_ca_pub_path }}"         dest: "/etc/ssh/trusted_ansible_user_ca_key.pub"         owner: root         group: root         mode: "0644"
      - name: Ensure correct configuration in sshd_config       lineinfile:         path: /etc/ssh/sshd_config.d/62-ansible-user-ca.conf         create: yes          line: "TrustedUserCAKeys /etc/ssh/trusted_ansible_user_ca_key.pub"         state: present
      - name: Restart SSHD service to apply changes       systemd:         name: sshd         state: restarted         enabled: yes
 
  | 
 
参考文章:https://goteleport.com/blog/how-to-configure-ssh-certificate-based-authentication/