- Shell 100%
Updated script to handle multiple GitHub usernames for SSH key import, improved logging, and ensured secure permissions for SSH configuration. |
||
|---|---|---|
| ghkeys.sh | ||
| LICENSE | ||
| README.md | ||
GitHub SSH Keys Importer
This Bash script retrieves SSH public keys from a specified GitHub account and configures your SSH daemon to authenticate using these keys. It saves the keys in a dedicated file within your home directory and creates a separate SSH daemon configuration file that references this file. This approach ensures that automatically generated keys by other system services (for example, ProxmoxVE) remain unaffected.
Features
- Automatic GitHub Key Retrieval: Fetches the public SSH keys from a specified GitHub account.
- User-Friendly: Prompts for a GitHub username if none is provided as an argument.
- Dependency Management: Checks for the presence of
curland attempts to install it if missing. - System & User Separation:
- The keys are stored in the current user’s
~/.ssh/authorized_keys_github. - The SSH daemon configuration file is created under
/etc/ssh/sshd_config.dusing sudo (if available).
- The keys are stored in the current user’s
- Security: Sets proper permissions for both the authorized keys file and the SSH configuration file.
- Service Restart: Automatically restarts the SSH service after applying changes.
Use Cases
- Quick Setup for SSH Authentication: Easily import GitHub SSH keys to streamline the login process without manually managing keys.
- Centralized Key Management: Manage authorized keys centrally by leveraging GitHub as your key repository.
- Multi-User Environments: Use the provided SSH daemon configuration to ensure each user's home directory is correctly referenced for their authorized keys.
Requirements
- Operating System: Linux (with systemd-based service management)
- Tools:
- Bash
curl(the script will attempt to install it if missing)sudo(for creating system-level configuration files)
- Privileges: Sudo privileges are required for system-wide configuration changes and restarting the SSH daemon.
Running localy
Clone the Repository, Make the Script Executable, and run the script
git clone https://github.com/Micinek/ghkeys.git
cd ghkeys
chmod +x ghkeys.sh
./ghkeys.sh
Or run the script directly with parameter of your Github Username
./ghkeys.sh Micinek
Usage
Run the script by providing a GitHub username as an argument or by entering it when prompted:
./ghkeys.sh your_github_username
The script will:
-
Check for
sudoandcurl, installing curl if necessary. -
Create your local
~/.sshdirectory if it doesn't exist. -
Fetch the public SSH keys from
https://github.com/"your_github_username".keys. -
Save these keys into
~/.ssh/authorized_keys_githubwith secure permissions. -
Create an SSH daemon configuration file at
/etc/ssh/sshd_config.d/github_authorized_keys.confwith the following content:AuthorizedKeysFile .ssh/authorized_keys_githubNote: Because the file path is relative (does not start with a
/), the SSH daemon automatically prepends each user’s home directory when looking up the authorized keys file. -
Restart the SSH service so that the new configuration takes effect.
How It Works
-
Authorized Keys File:
The script downloads the GitHub SSH keys and writes them to~/.ssh/authorized_keys_github. Since the SSH daemon interprets relative paths in theAuthorizedKeysFiledirective as relative to the user’s home directory, each user will have their keys read from~/.ssh/authorized_keys_github. -
SSH Daemon Configuration:
The configuration file placed in/etc/ssh/sshd_config.dtells the SSH daemon to use the.ssh/authorized_keys_githubfile from each user’s home directory. This ensures that the keys imported from GitHub are recognized during authentication.
Troubleshooting
-
SSH Connection Refused:
If SSH connections fail after running the script, verify:- The configuration file is in the correct directory (
/etc/ssh/sshd_config.d). - The SSH service restarted properly. You may try restarting it manually:
sudo systemctl restart sshd - The authorized keys file exists in your home directory and has proper permissions (600).
- The configuration file is in the correct directory (
-
Missing Dependencies:
Make surecurlis installed. The script will attempt to install it, but you may need to run the installation command manually if your package manager requires additional confirmation.
License
This project is licensed under the MIT License. See the LICENSE file for details.