Chef:部署一个禁用root ssh访问的服务器?

时间:2014-01-01 20:09:13

标签: ssh root chef knife-solo

当我们配置新的Linux服务器时,我们

  • 使用sudo权限创建一个新用户,然后......
  • 阻止来自ssh访问的根

然后我们继续通过新用户帐户配置系统。当然我们这样做是因为我们的日志充满了恶意的ssh root登录尝试。

但是现在我们正在尝试使用Chef(厨师独奏与刀 - 独奏和图书管理员 - 厨师)来构建服务器。一旦我们创建了一个阻止从root登录ssh的配方,我们就无法再次运行配方。

我见过这个: How to run chef-client vagrant provisioner from custom non-root user? 但似乎“root”在整个地方都是硬编码的,并且所接受的解决方案需要分支所有基本的烹饪书。

有关如何创建包含禁用root ssh访问权限的可重新运行的Chef设置的任何想法?

2 个答案:

答案 0 :(得分:2)

使用非root帐户引导服务器并不罕见。正如@StephenKing指出的那样,刀支持这一点。以下是我的正常程序:

ssh-copy-id me@hostname
knife bootstrap hostname --ssh-user me --sudo --run-list role[desired_server_role]

但是,这个答案假设您正在使用厨师服务器(强烈推荐)....

您正在使用非标准刀插件来模拟此引导行为。 documentation表示sudo将被“knife solo prepare”命令使用:

  

它将从〜/ .ssh / config或-F指定的文件中查找SSH信息。您还可以传递端口信息(-p),身份信息(-i)或密码(-P)。它将使用sudo来运行其中一些命令,并在命令行中未提供密码时提示您输入密码。

“knife solo bootstrap”命令似乎支持与标准引导程序类似的选项。

$ knife solo bootstrap -h 
knife solo bootstrap [USER@]HOSTNAME [JSON] (options)
        --no-berkshelf               Skip berks install
        --bootstrap-version VERSION  The version of Chef to install
    -N, --node-name NAME             The Chef node name for your new node
        --server-url URL             Chef Server URL
        --chef-zero-port PORT        Port to start chef-zero on
    -k, --key KEY                    API Client Key
        --[no-]color                 Use colored output, defaults to false on Windows, true otherwise
    -c, --config CONFIG              The configuration file to use
        --defaults                   Accept default values for all questions
    -d, --disable-editing            Do not open EDITOR, just accept the data as is
    -e, --editor EDITOR              Set the editor to use for interactive commands
    -E, --environment ENVIRONMENT    The Chef environment for your node
        --format FORMAT              Which format to use for output
        --[no-]host-key-verify       Verify host key, enabled by default.
    -i, --identity-file FILE         The ssh identity file
    -j JSON_ATTRIBS,                 A JSON string to be added to node config (if it does not exist)
        --json-attributes
        --no-librarian               Skip librarian-chef install
    -z, --local-mode                 Point knife commands at local repository instead of server
    -u, --user USER                  API Client Username
        --omnibus-options "OPTIONS"  Pass options to the install.sh script
        --omnibus-url URL            URL to download install.sh from
        --omnibus-version VERSION    Deprecated. Replaced with --bootstrap-version.
        --prerelease                 Install the pre-release Chef version
        --print-after                Show the data after a destructive operation
    -r, --run-list RUN_LIST          Comma separated list of roles/recipes to put to node config (if it does not exist)
    -F CONFIG_FILE,                  Alternate location for ssh config file
        --ssh-config-file
        --ssh-identity FILE          Deprecated. Replaced with --identity-file.
    -P, --ssh-password PASSWORD      The ssh password
    -p, --ssh-port PORT              The ssh port
    -x, --ssh-user USERNAME          The ssh username
    -s, --startup-script FILE        The startup script on the remote server containing variable definitions
        --sudo-command SUDO_COMMAND  The command to use instead of sudo for admin privileges
        --sync-only                  Only sync the cookbook - do not run Chef
    -V, --verbose                    More verbose output. Use twice for max verbosity
    -v, --version                    Show chef version
    -W, --why-run                    Enable whyrun mode
    -y, --yes                        Say yes to all prompts for confirmation
    -h, --help                       Show this message

我注意到各种各样的时髦东西,包括使用厨师零的选项(现在chef-client支持的功能)。

希望这会有所帮助,但我建议考虑使用chef-server。它很容易站起来,然后您将遵循使用厨师的标准方式。

更新

一个有趣的读物:

答案 1 :(得分:1)

它也适用于sudoknife bootstrap,当然还有sudo chef-client)。