Advanced SSH Settings

When setting up SSH access to Kubernetes nodes, you can configure additional options for easier connection, such as SSH keys or connection shortcuts.

Setting Up SSH Keys

To make the SSH connection easier and more secure without entering a password, you can generate a public/private SSH key pair and upload the public key to each node.

On your local machine, check whether the $HOME\.ssh directory contains the files id_ed25519 and id_ed25519.pub. If you do not have the key pair, generate it by running the following command on your machine:

ssh-keygen -t ed25519

You will be prompted for a passphrase, which you can leave empty if you do not need the extra layer of security.

When completed, the id_ed25519 and id_ed25519.pub files are added to the .ssh directory ($HOME\.ssh on Windows or ~/.shh on Linux and Mac).

Then copy the public key to each node, using one of the following commands.

For Windows (Powershell):

type $HOME\.ssh\id_ed25519.pub | ssh <username>@<host> "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

For Linux or macOS (Bash/Zsh):

ssh-copy-id -i ~/.ssh/id_ed25519 <username>@<host>

If you do this for each node, you will not be prompted for the password anymore. You can now disable the SSH access to the nodes through a username and password for security reasons.

Creating SSH Connection Shortcuts

You can simplify the SSH access by defining aliases for each node. You need to create a configuration file named $HOME\.ssh\config on Windows or ~/.ssh/config on Linux and MAC and enter the aliases there. The following example shows three nodes with aliases n1, n2, and n3.

(#) $HOME\.ssh\config Host n1 HostName <hostname1> User <username1> IdentityFile ~/.ssh/id_ed25519 Host n2 HostName <hostname2> User <username2> IdentityFile ~/.ssh/id_ed25519 Host n3 HostName <hostname3> User <username3> IdentityFile ~/.ssh/id_ed25519

When successfully completed, you can access the nodes using the following shortcuts:

  • ssh n1

  • ssh n2

  • ssh n3

What's Next?

Installing a K3s Cluster on Premises Using Command Line