Skip to main content

Create

Add an authorized SSH key to a user.

Adds the key line to the end of ~/.ssh/authorized_keys of the user, and makes the directory and the file if they are missing, with modes 700 and 600, owned by the user. A key that the file has already, by its fingerprint, does not change it. A line with no public key is refused. A user that the host does not have is not found. It runs on the Debian family only, and elsewhere reports skipped.

osapi client node user sshkey create [flags]

Examples​

# Add a key
osapi client node user sshkey create --target web-01 --name osapitest --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl85dcLw7lIdX+IyXEXT00CTCezv/2E1jvolleleo6Z osapitest@example"

# Add a second key
osapi client node user sshkey create --target web-01 --name osapitest --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGLPeEZQDbhOrSIx6gISjxXIcREV3rwL517pX6m9G5jc other@example"

# A key after a last line with no line break
osapi client node user sshkey create --target web-01 --name osapitest --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGLPeEZQDbhOrSIx6gISjxXIcREV3rwL517pX6m9G5jc other@example"

# A key the user has
osapi client node user sshkey create --target web-01 --name osapitest --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl85dcLw7lIdX+IyXEXT00CTCezv/2E1jvolleleo6Z osapitest@example"

Result​

Add a key, on one host:

$ osapi client node user sshkey create --target web-01 --name osapitest --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl85dcLw7lIdX+IyXEXT00CTCezv/2E1jvolleleo6Z osapitest@example"
Results:
  HOSTNAME  STATUS   FINGERPRINT
  web-01    changed  SHA256:mnpIO1El26FsWnOeLzppADcNXgXRNHh9MPNPLPvSCwk

  1 host: 1 changed   job 550e8400-e29b-41d4-a716-446655440000   trace 26e448c9dbe1c4a8d216a98c675079c9

On each host, where one cannot run it:

$ osapi client node user sshkey create --target _all --name osapitest --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl85dcLw7lIdX+IyXEXT00CTCezv/2E1jvolleleo6Z osapitest@example"
Results:
  HOSTNAME  STATUS   FINGERPRINT
  web-01    changed  SHA256:mnpIO1El26FsWnOeLzppADcNXgXRNHh9MPNPLPvSCwk
  web-02    changed  SHA256:mnpIO1El26FsWnOeLzppADcNXgXRNHh9MPNPLPvSCwk

  3 hosts: 2 changed, 1 skipped   job 550e8400-e29b-41d4-a716-446655440000   trace 26e448c9dbe1c4a8d216a98c675079c9

Details:
  mac-01  skipped  operation not supported on this OS family

With --json, the API's response:

{
"job_id": "550e8400-e29b-41d4-a716-446655440000",
"results": [
{
"changed": true,
"fingerprint": "SHA256:mnpIO1El26FsWnOeLzppADcNXgXRNHh9MPNPLPvSCwk",
"hostname": "web-01",
"status": "ok"
}
]
}

Add a second key, on one host:

$ osapi client node user sshkey create --target web-01 --name osapitest --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGLPeEZQDbhOrSIx6gISjxXIcREV3rwL517pX6m9G5jc other@example"
Results:
  HOSTNAME  STATUS   FINGERPRINT
  web-01    changed  SHA256:a5CeY6Lh3ZXkJOqGyuwb24kQeVjgag1gc9ujzv++nhE

  1 host: 1 changed   job 550e8400-e29b-41d4-a716-446655440000   trace 26e448c9dbe1c4a8d216a98c675079c9

On each host, where one cannot run it:

$ osapi client node user sshkey create --target _all --name osapitest --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGLPeEZQDbhOrSIx6gISjxXIcREV3rwL517pX6m9G5jc other@example"
Results:
  HOSTNAME  STATUS   FINGERPRINT
  web-01    changed  SHA256:a5CeY6Lh3ZXkJOqGyuwb24kQeVjgag1gc9ujzv++nhE
  web-02    changed  SHA256:a5CeY6Lh3ZXkJOqGyuwb24kQeVjgag1gc9ujzv++nhE

  3 hosts: 2 changed, 1 skipped   job 550e8400-e29b-41d4-a716-446655440000   trace 26e448c9dbe1c4a8d216a98c675079c9

Details:
  mac-01  skipped  operation not supported on this OS family

With --json, the API's response:

{
"job_id": "550e8400-e29b-41d4-a716-446655440000",
"results": [
{
"changed": true,
"fingerprint": "SHA256:a5CeY6Lh3ZXkJOqGyuwb24kQeVjgag1gc9ujzv++nhE",
"hostname": "web-01",
"status": "ok"
}
]
}

A key after a last line with no line break, on one host:

$ osapi client node user sshkey create --target web-01 --name osapitest --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGLPeEZQDbhOrSIx6gISjxXIcREV3rwL517pX6m9G5jc other@example"
Results:
  HOSTNAME  STATUS   FINGERPRINT
  web-01    changed  SHA256:a5CeY6Lh3ZXkJOqGyuwb24kQeVjgag1gc9ujzv++nhE

  1 host: 1 changed   job 550e8400-e29b-41d4-a716-446655440000   trace 26e448c9dbe1c4a8d216a98c675079c9

On each host, where one cannot run it:

$ osapi client node user sshkey create --target _all --name osapitest --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGLPeEZQDbhOrSIx6gISjxXIcREV3rwL517pX6m9G5jc other@example"
Results:
  HOSTNAME  STATUS   FINGERPRINT
  web-01    changed  SHA256:a5CeY6Lh3ZXkJOqGyuwb24kQeVjgag1gc9ujzv++nhE
  web-02    changed  SHA256:a5CeY6Lh3ZXkJOqGyuwb24kQeVjgag1gc9ujzv++nhE

  3 hosts: 2 changed, 1 skipped   job 550e8400-e29b-41d4-a716-446655440000   trace 26e448c9dbe1c4a8d216a98c675079c9

Details:
  mac-01  skipped  operation not supported on this OS family

With --json, the API's response:

{
"job_id": "550e8400-e29b-41d4-a716-446655440000",
"results": [
{
"changed": true,
"fingerprint": "SHA256:a5CeY6Lh3ZXkJOqGyuwb24kQeVjgag1gc9ujzv++nhE",
"hostname": "web-01",
"status": "ok"
}
]
}

A key the user has, on one host:

$ osapi client node user sshkey create --target web-01 --name osapitest --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl85dcLw7lIdX+IyXEXT00CTCezv/2E1jvolleleo6Z osapitest@example"
Results:
  HOSTNAME  STATUS  FINGERPRINT
  web-01    ok      SHA256:mnpIO1El26FsWnOeLzppADcNXgXRNHh9MPNPLPvSCwk

  1 host: 1 ok   job 550e8400-e29b-41d4-a716-446655440000   trace 26e448c9dbe1c4a8d216a98c675079c9

On each host, where one cannot run it:

$ osapi client node user sshkey create --target _all --name osapitest --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl85dcLw7lIdX+IyXEXT00CTCezv/2E1jvolleleo6Z osapitest@example"
Results:
  HOSTNAME  STATUS  FINGERPRINT
  web-01    ok      SHA256:mnpIO1El26FsWnOeLzppADcNXgXRNHh9MPNPLPvSCwk
  web-02    ok      SHA256:mnpIO1El26FsWnOeLzppADcNXgXRNHh9MPNPLPvSCwk

  3 hosts: 2 ok, 1 skipped   job 550e8400-e29b-41d4-a716-446655440000   trace 26e448c9dbe1c4a8d216a98c675079c9

Details:
  mac-01  skipped  operation not supported on this OS family

With --json, the API's response:

{
"job_id": "550e8400-e29b-41d4-a716-446655440000",
"results": [
{
"changed": false,
"fingerprint": "SHA256:mnpIO1El26FsWnOeLzppADcNXgXRNHh9MPNPLPvSCwk",
"hostname": "web-01",
"status": "ok"
}
]
}

With the default settings, each example does this on the host:

ExampleOn the host
Add a keywrites /home/osapitest/.ssh/authorized_keys: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl85dcLw7lIdX+IyXEXT00CTCezv/2E1jvolleleo6Z osapitest@example; runs chown -R 1001:1001 /home/osapitest/.ssh
Add a second keywrites /home/osapitest/.ssh/authorized_keys: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl85dcLw7lIdX+IyXEXT00CTCezv/2E1jvolleleo6Z osapitest@example\nssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGLPeEZQDbhOrSIx6gISjxXIcREV3rwL517pX6m9G5jc other@example; runs chown -R 1001:1001 /home/osapitest/.ssh
A key after a last line with no line breakwrites /home/osapitest/.ssh/authorized_keys: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl85dcLw7lIdX+IyXEXT00CTCezv/2E1jvolleleo6Z osapitest@example\nssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGLPeEZQDbhOrSIx6gISjxXIcREV3rwL517pX6m9G5jc other@example; runs chown -R 1001:1001 /home/osapitest/.ssh

The command fails in these cases:

CallFlagsFails with
A line with no public key--name osapitest --key "not a key"not an SSH public key
A key that is not base64--name osapitest --key "ssh-ed25519 !!!"not an SSH public key
A user that the host does not have--name nobody --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl85dcLw7lIdX+IyXEXT00CTCezv/2E1jvolleleo6Z osapitest@example"not found
A name that is not a user name--name "Bad;name" --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl85dcLw7lIdX+IyXEXT00CTCezv/2E1jvolleleo6Z osapitest@example"Name
A name over 32 characters--name aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl85dcLw7lIdX+IyXEXT00CTCezv/2E1jvolleleo6Z osapitest@example"max
A key with a line break--name osapitest --key "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl85dcLw7lIdX+IyXEXT00CTCezv/2E1jvolleleo6Z osapitest@example\nssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGLPeEZQDbhOrSIx6gISjxXIcREV3rwL517pX6m9G5jc other@example"no_linebreak

Permission​

It needs the user:write permission. osapi controller role get --name <role> shows what a role grants.

Flags​

FlagDescriptionDefault
--keyThe public key line, such as ssh-ed25519 AAAA... alice@laptop, with options before it if any (required)
--nameThe user, such as alice (required)
-T, --targetTarget: _all, _any, hostname, or label (group:web.dev)_all

Global flags​

FlagDescriptionDefault
-d, --debugEnable or disable debug mode
-j, --jsonEnable JSON output
-f, --osapi-filePath to config file; without one at the default path, osapi runs on the defaults/etc/osapi/osapi.yaml
--urlBase URL the CLI client connects to (controller.client.url)https://localhost:8080

Output tells how the result is printed, and what --json gives.