How to automate SSH login with password?
SSH key authentication allows you to connect to a Linux server without entering your account password every time. It is also a more secure alternative to storing a server password in scripts, shortcuts, or command-line arguments.
SSH authentication uses a pair of cryptographic keys: a private key stored on your computer and a public key added to the server.
Attention:
Never share your private SSH key or store your server password in shortcuts, scripts, or command-line arguments.
Generate an SSH key
On Windows, macOS, or Linux with OpenSSH installed, open Terminal, Windows Terminal, or PowerShell and run:
ssh-keygen -t ed25519
Follow the prompts to select where the key should be stored. You can also protect the private key with a passphrase.
By default, the command creates a private key and a corresponding public key in your SSH directory.
Add the public key to your server
Connect to your Linux server using your existing SSH credentials.
Add the contents of your public key to the following file for the user you want to authenticate as:
~/.ssh/authorized_keys
Make sure the SSH directory and authorized_keys file have appropriate permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Connect using your SSH key
After the public key has been added to the server, connect using:
ssh username@server_ip -p ssh_port
Replace username, server_ip, and ssh_port with your server connection details. The SSH port provided for your server can be found in the Welcome Email.
If your private key is stored in a non-default location, specify it with the -i option:
ssh -i /path/to/private_key username@server_ip -p ssh_port