--- title: New server SSH key params: host: { type: host, required: true, label: Target server } user: { type: name, required: true, default: admin, label: Login user } keyname: { type: name, default: "id_ed25519_{{host}}" } --- # New server SSH key Creates a key pair for **{{user}}** on **{{host}}** and authorizes it. ```sh {check host="{{host}}" title="User exists"} id {{user}} ``` ```sh {check host="{{host}}" title="Password login disabled"} sshd -T | grep -qx 'passwordauthentication no' ``` ```sh {run on=local id=keygen title="Generate key pair" artifacts="{{keyname}},{{keyname}}.pub"} ssh-keygen -q -t ed25519 -N '' -C {{user}}@{{host}} -f {{keyname}} ``` ```sh {run host="{{host}}" id=authorize title="Authorize key" confirm upload="{{keyname}}.pub:/tmp/new_key.pub"} install -d -m 700 -o {{user}} -g {{user}} ~{{user}}/.ssh cat /tmp/new_key.pub >> ~{{user}}/.ssh/authorized_keys rm /tmp/new_key.pub ``` :::after{step=keygen} Download the private key above, then save it: - **Linux:** `~/.ssh/{{keyname}}`, then `chmod 600 ~/.ssh/{{keyname}}` - **Windows:** `C:\Users\\.ssh\{{keyname}}` :::