【问题标题】:What are the best practices for locking down SSH with Ansible?使用 Ansible 锁定 SSH 的最佳实践是什么?
【发布时间】:2016-08-26 22:50:39
【问题描述】:

假设我有一台服务器,我想做以下事情:

  • 将 SSH 端口更改为 33333
  • 禁用root登录
  • 禁用密码登录,只允许基于密钥的身份验证

我对执行这些任务的说明不感兴趣。网上有很多东西。

我的问题是关于最佳实践的。

这里有几个问题:

  • 当我使用 playbook 更改默认 SSH 端口时,我无法在同一主机上再次运行相同的 playbook。这是因为我需要在 playbook 运行一次后更改清单文件中的 SSH 端口参数。
  • 与上述类似,我使用 root 登录系统,一旦禁用 root 登录,我需要在某处更改“remote_user”参数。
  • 禁用密码登录会导致与上述类似的问题。

如果我的方法有一些基本问题,我也将不胜感激。

【问题讨论】:

  • 更改 SSH 端口一开始就不是什么硬道理。您所说的是“通过默默无闻的安全”,这与其说是实际的安全实践,不如说是一个讽刺的笑话。
  • 是的,你是对的,但这只是我想做的另一件事。

标签: ssh ansible ansible-playbook


【解决方案1】:

我维护一个“引导”剧本,与我的其他剧本和角色分开,它负责安装 python 2.x 和 python-apt 或 python 3.x 和 python3-apt(使用 local_action 和 ssh)的初始工作,创建用户并授予 sudo 访问权限、锁定 root 和 SSH 等。如果我需要引导新主机,我会使用限制(-l--limit)选项运行 playbook。如果我需要修改现有主机,例如添加新用户,我运行 playbook 并传入额外的变量(-e--extra-vars)来覆盖 SSH 用户名和端口。我的任务被参数化,以便它们使用 sudo(或不使用)或成为(或不使用)取决于变量定义,例如:

- hosts: all
  vars:
    ansible_user: root
  become: "{{ ansible_user != 'root' }}"

如果您还有其他问题,请告诉我,我会尽力扩展此答案!

【讨论】:

    【解决方案2】:

    像 @mwp 我有剧本来初始化 ansible。它创建特殊用户(名为“ansible”)来连接服务器,将它们添加到 sudoers。这个剧本尽可能地小。

    ---
    # This playbook for prepare server for ansible.
    
    - name: add role ansible_init
      hosts: '{{ target }}'
      remote_user: root
      roles:
        - ansible_init
    

    角色“ansible_init”是:

    ---
    
    - name: install ansible client library
      yum: name={{ item }} state=latest
      with_items:
        - libselinux-python
        - policycoreutils-python
    
    - name: useradd ansible
      user:
        name={{ ansible_init.ansible_user.login }}
        password={{ ansible_init.ansible_user.password }}
    
    - name: add default ssh key for ansible user
      authorized_key:
        user={{ ansible_init.ansible_user.login }}
        key="{{ lookup('file', '{{ ansible_init.ansible_user.ssh_key_file }}') }}"
        state=present
    
    - name: nopasswd sudo for ansible user
      lineinfile: "dest=/etc/sudoers state=present regexp='^{{ ansible_init.ansible_user.login }}' line='{{ ansible_init.ansible_user.login }} ALL=(ALL) NOPASSWD: ALL'"
    

    作为 ansible 用户执行的其他操作(ansible.cfg 中的remote_user = ansible)。

    用于设置服务器(邮件、数据库、Web 等)的一些剧本包括必要角色、在通用剧本中分开的常见步骤(包括作为第一步)。常用 playbook 设置 sshd、firewalld、fail2ban、安装额外的 repos、服务器的 known_hosts 等。

    典型的剧本如下所示:

    # This playbook deploys 'webserver' server.
    
    - include: init_server.yml
    
    - name: deploy 'webserver'
      hosts: '{{ target | default("webservers") }}'
      become: true
      become_user: root
      roles:
        - git
        - nginx
    ...........
    

    init_server.yml 从未直接调用,仅来自具体的剧本。

    【讨论】:

      【解决方案3】:

      一种方法是在启动之前尽可能多地处理这些问题。也就是说,修改您要启动的映像,以便它已经进行了必要的更改。使用亚马逊,这将构建一个自定义 AMI;与其他供应方法不同,其背后的技术会有所不同,但原理是相同的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-25
        • 2011-06-07
        • 1970-01-01
        相关资源
        最近更新 更多