Linux 采用 SSH CA 登陆验证

发布时间: 更新时间: 总字数:728 阅读时间:2m 作者: IP上海 分享 网址

当配置大量的服务器有很多用户,保持SSH访问在线的基础设施可以变得复杂,SSH实际上具有使用证书颁发机构来验证服务器和客户端的功能。

步骤

  • 生成 CA 密钥对
  • 为目标客户机配置 SSH CA 公钥信任
  • 生产客户端 SSH 密钥对
  • 采用 CA 私钥客户端 SSH 公钥进行签名
  • 客户端采用客户端 SSH 私钥和公钥签名登陆目标客户机

生成 CA 密钥对

目的:配置客户端认证服务器的证书,该证书允许我们的客户端连接到我们的服务器,而不需要质疑服务器的真实性。

ssh-keygen -C "server_ca" -b 2048 -f server_ca

说明:

  • -C comment 注释
  • -b bits 位数
  • -f keyfile 证书文件

执行中可以输入密码,提高安全性。生产的证书如下:

[root@xiexianbin_cn CA]# ls
server_ca  server_ca.pub

配置 SSH CA 公钥信任

修改目标机的/etc/ssh/sshd_config文件,添加刚生产的server_ca.pub信任:

sudo echo "TrustedUserCAKeys /etc/ssh/server_ca.pub" >>/etc/ssh/sshd_config
sudo systemctl restart sshd

生产客户端密钥对

ssh-keygen -t dsa -f dsa/id_dsa

ssh-keygen -t rsa -f rsa/id_rsa

采用 CA 客户端 SSH 公钥进行签名

ssh-keygen -s /root/ssh/CA/server_ca -I xiexianbin -n root -V +52w id_rsa.pub

说明:

  • -s:CA 证书私钥
  • -I:识别证书的名称。 当证书用于认证时,它用于日志记录
  • -n:识别与此证书关联的名称(用户或主机)
  • -V:指定证书的有效期为。 在这种情况下,我们指定证书将在一年(52周)过期
  • -O option:source-address=address_list :允许用户证书使用的客户端的地址,多个地址用逗号分隔,可以时候CIDR, 我们将设置这个来限制用户证书的使用范围。

然后生成id_rsa-cert.pub公钥签名文件。

登陆说明

ssh -i id_dsa root@ip  #即可登陆,默认会加载:id_dsa + id_dsa-cert.pub登陆测试机器

采用dsa目录下的id_dsa + id_dsa-cert.pub 或rsa目录下的id_rsa + id_rsa-cert.pub 即可登陆。

Python 实现

依赖 paramiko>=2.3.0 版本

cat test_paramiko_cert_key.py
# -*- coding: utf-8 -*-
'''
依赖:
    paramiko==2.3.3
'''

import paramiko

from StringIO import StringIO


def test_ssh_ca_key():
    key_file = """<PRIVATE KEY>"""
    cert_pub_file = """<*-cert.pub>"""
    ip = "<ip>"
    port = 22
    pkey = paramiko.DSSKey.from_private_key(StringIO(key_file))
    pkey.load_certificate(cert_pub_file)

    # Client setup
    ssh = paramiko.SSHClient()
    ssh.load_system_host_keys()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=ip, port=port, username='root', pkey=pkey)

    stdin, stdout, stderr = ssh.exec_command("pwd")
    print stdin, stdout, stderr


if __name__ == '__main__':
    test_ssh_ca_key()

参考

  1. https://www.digitalocean.com/community/tutorials/how-to-create-an-ssh-ca-to-validate-hosts-and-clients-with-ubuntu
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数