Manage multiple SSH keys and switch between them easily.
Tools like GitHub, GitLab, and Bitbucket use SSH keys to authenticate users and do not allow the same key to be used for different accounts.
If you use your computer for work and also for personal projects, you probably need multiple SSH keys for different accounts (for example, one for work, one for a client, and another for personal projects). Managing these keys manually can be tedious.
This script simplifies the process, allowing you to quickly create and switch between different SSH keys.
- Unix-based operating system (Linux, macOS)
- Basic knowledge of bash scripting and command-line usage
- Git installed for cloning the repository
- Clone the repository:
cd ~ git clone https://github.com/jjpeleato/bash-ssh-key-manager.git ssh-key-manager
- Copy the scripts (
initialize.shandenvironment.sh) into.sshdirectory:cd ~/ssh-key-manager cp *.sh ~/.ssh/
- Give execution permissions to the scripts:
cd ~ chmod +x .ssh/initialize.sh chmod +x .ssh/environment.sh
- Remove the cloned repository:
cd ~ rm -rf ssh-key-manager
- End and happy use!
- Run the
initialize.shscript to create the SSH keys you need. Follow the on-screen instructions, it is very easy. Remember to set a securepassphrasefor each key.cd ~/.ssh sh initialize.sh
- Whenever you need to switch SSH keys, run the
environment.shscript and select the desired key.cd ~/.ssh sh environment.sh
- Done! Now you can easily switch between your SSH keys.
Note: Don’t forget to add the generated public keys to the corresponding platforms (GitHub, GitLab, Bitbucket, etc).
- The
initialize.shscript creates a file calledloki.txtin the~/.sshdirectory, which contains a summary of the keys created. It is important not to delete this file unless you want to restart the process. - SSH keys are stored in the
~/.sshdirectory inside subdirectories named after the account (for example,work,personal). - The keys created use the
ed25519format, which is more secure and efficient than the traditional RSA format. - If you create a
.ppkfile for FileZilla from your key, name the fileid_rsa_filezilla.ppkand save it in the same subdirectory as the original key. Theenvironment.shscript will detect it automatically.
If you already have an existing SSH key and want to integrate it into this system, follow these steps:
- Move your SSH key in
~/.sshinto a subdirectory (for example,~/.ssh/work,~/.ssh/personal). - If it exists, delete the file
~/.ssh/loki.txt. - Run the
initialize.shscript and during the key creation process, use the same name as the subdirectory where you moved your key. The script will detect it and handle it automatically. - Follow the on-screen instructions to complete the process.
Use the initialize.sh script to generate your SSH keys and then create or edit the ~/.ssh/config file with the necessary configurations.
Below is an example configuration for two GitHub accounts (work and personal):
# Work account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/work/id_rsa
IdentitiesOnly yes
# Personal account
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/personal/id_rsa
IdentitiesOnly yes
Nota: Make sure to replace github.com in your remote repository URL with github-work or github-personal, as appropriate.
git remote -v
git remote set-url origin git@github-personal:jjpeleato/bash-ssh-key-manager.gitssh-key-manager/
├── .husky/
│ ├── commit-msg
│ ├── post-merge
│ └── post-rewrite
├── .editorconfig
├── .gitignore
├── commitlint.config.cjs
├── environment.sh # Main script to switch between SSH keys
├── initialize.sh # Main script to create SSH keys
├── LICENSE
├── package.json
└── README.md
Contributions are welcome! Please fork the repository and submit a pull request with your changes.
For issues or feature requests, please open an issue in the repository or contact with me directly.
More information on the following commits. If required.
Grettings @jjpeleato.