AKLWEB HOST provides a feature that allows you to pre-install SSH keys when creating a new instance, so you can SSH to the instance as root with the key. However, the key doesn’t work for non-root users. This tutorial describes three methods to use SSH keys with non-root users.
# sudo -u example_user ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/example_user/.ssh/id_rsa): Created directory '/home/example_user/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/example_user/.ssh/id_rsa Your public key has been saved in /home/example_user/.ssh/id_rsa.pub
/home/example_user/.ssh/id_rsa
, to your computer. For example, you might copy it to your local .ssh folder as ~/.ssh/example_user_id_rsa
.# rm /home/example_user/.ssh/id_rsa
authorized_keys
.# mv /home/example_user/.ssh/id_rsa.pub /home/example_user/.ssh/authorized_keys
If you saved the private key as ~/.ssh/example_user_id_rsa
, you can SSH to the server as your non-root example_user:
$ ssh -i ~/.ssh/example_user_id_rsa example_user@192.0.2.123
In this case, we’ll move the root key to the example_user, which also disables the root user’s SSH key access.
.ssh
directory for example_user.# mkdir /home/example_user/.ssh
# mv /root/.ssh/authorized_keys /home/example_user/.ssh/
.ssh
directory from root to example_user so OpenSSH can read it.# chown -R example_user:example_user /home/example_user/.ssh
If you are deploying many instances, you may use the AKLWEB HOST Startup Scripts feature to create a non-root user and move the SSH key automatically.
#!/bin/sh useradd -m -s /bin/bash example_user mv /root/.ssh /home/example_user/ chown -R example_user:example_user /home/example_user/.ssh
You can deploy instances with this script and one or more SSH keys. When the instance deploys, the script creates example_user, then moves the public SSH keys from root to example_user. Now you can SSH to the new instance as example_user with the keys you provided.