-
Generate an SSH Key Pair (if you don't have one yet):
If you don't have an SSH key pair, you can create a new one by running the following command:bashssh-keygen -t rsa -b 4096Follow the prompts to complete the key generation process. You can set a passphrase or leave it empty.
-
Add the Private Key to the SSH Agent:
To have the SSH key automatically loaded at system startup, add it to the ssh-agent. First, ensure that ssh-agent is running:basheval "$(ssh-agent -s)"Then, use the ssh-add command to add the private key:
bashssh-add ~/.ssh/id_rsaThis assumes your private key file is
~/.ssh/id_rsa. If your key file name or path differs, modify the command accordingly. -
Configure the
~/.ssh/configFile:
You can create or edit the~/.ssh/configfile to help manage and automate SSH connection settings. For example, you can specify which private key to use for a specific host:bashHost example.com IdentityFile ~/.ssh/id_rsaThis ensures that the specified private key is used every time you connect to
example.com. -
Make the Key Automatically Load at System Startup:
To have the private key automatically loaded at each system startup, add thessh-addcommand to your.bashrcor.profilefile:bashif [ -z "$(pgrep ssh-agent)" ]; then eval $(ssh-agent -s) ssh-add ~/.ssh/id_rsa fiThis script checks if ssh-agent is running; if not, it starts ssh-agent and adds your private key.
By following these steps, your SSH private key will be automatically loaded at each login or system startup on Ubuntu, simplifying the SSH connection process. This is particularly useful when managing multiple servers or performing automated deployments.