An open API service indexing awesome lists of open source software.

https://github.com/odanilosalve/ssh-manager

SSH Manager is a powerfull tool write in shell from managers my SSH Keys using simple commands in my terminal.
https://github.com/odanilosalve/ssh-manager

shell-script ubuntu

Last synced: 5 months ago
JSON representation

SSH Manager is a powerfull tool write in shell from managers my SSH Keys using simple commands in my terminal.

Awesome Lists containing this project

README

          

# ssh-manager

`ssh-manager` is a small CLI utility to manage and switch between multiple SSH keys for GitHub on a single machine.

It works by:

* Keeping a registry of SSH keys identified by **aliases**.
* Setting one alias as **active**.
* Pointing a symlink (`~/.ssh/github_current`) to the active key.
* Letting your SSH config use this symlink for all connections to `github.com`.

This way you can jump between personal and work GitHub accounts with a single command.

---

## Requirements

* Linux environment (tested on Ubuntu).
* OpenSSH client (`ssh`).
* `~/.local/bin` on your `PATH` (or another directory on `PATH` where you place the script).
* SSH keys already generated (e.g. `~/.ssh/personal`, `~/.ssh/work`, etc.).

---

## Installation

### Quick Install (Recommended)

1. Clone the repository:

```bash
git clone https://github.com/odanilosalve/ssh-manager.git
cd ssh-manager
```

2. Run the installation script:

```bash
./install.sh
```

This will:
- Copy the project to `~/.local/lib/ssh-manager/`
- Create a symlink `~/.local/bin/ssh-manager` pointing to the script
- Make the script executable
- Warn if `~/.local/bin` is not in your PATH

3. Ensure `~/.local/bin` is on your `PATH`.
If not, add this to your shell config (`~/.bashrc`, `~/.zshrc`, etc.):

```bash
export PATH="$HOME/.local/bin:$PATH"
```

Then reload your shell or run:
```bash
source ~/.bashrc # or source ~/.zshrc
```

### Manual Install

1. Clone or download this repository:

```bash
git clone https://github.com/odanilosalve/ssh-manager.git
cd ssh-manager
```

2. Copy the entire project to a directory on your `PATH`:

```bash
cp -r . ~/.local/bin/ssh-manager
chmod +x ~/.local/bin/ssh-manager/ssh-manager
```

Or create a symlink:

```bash
ln -s "$(pwd)" ~/.local/bin/ssh-manager
```

3. Ensure `~/.local/bin` is on your `PATH` (see step 3 above).

**Note**: The `ssh-manager` script requires the `lib/` directory to be in the same directory as the script.

---

## SSH configuration

Configure SSH to always use the *current* key for GitHub:

```sshconfig
# ~/.ssh/config
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github_current
IdentitiesOnly yes
```

* `ssh-manager key use ` will update the symlink `~/.ssh/github_current`.
* Every `git clone`, `git fetch`, `git push`, etc. that uses `git@github.com:...`
will automatically use the active key.

---

## Quick start

1. Register your keys:

```bash
ssh-manager key add personal ~/.ssh/personal
ssh-manager key add work ~/.ssh/work
```

2. Activate one key (for example, personal):

```bash
ssh-manager key use personal
```

3. Test the connection:

```bash
ssh-manager key test
# or
ssh-manager key test personal
```

4. Use Git as usual:

```bash
git clone git@github.com:odanilosalve/ssh-manager.git
```

5. When you want to switch to the work account:

```bash
ssh-manager key use work
ssh-manager key test
git clone git@github.com:YOUR_WORK_ORG/some-repo.git
```

---

## Command-line reference

### Global usage

```bash
ssh-manager [OPTIONS] [subcommand] [args...]
```

Global options:

* `-h`, `--help` – Show help.
* `-V`, `--version` – Show version.

Main command group:

* `key` – Manage SSH keys known by `ssh-manager`.

---

### `key list`

```bash
ssh-manager key list
ssh-manager key list --system
```

* `ssh-manager key list`
Shows keys registered in the `ssh-manager` registry.
The active key is marked with `*`.

* `ssh-manager key list --system`
Lists physical SSH public keys found in `~/.ssh/*.pub` and their matching private keys if present.

---

### `key add`

```bash
ssh-manager key add
```

Registers a new key in the internal registry.

* ``
Name to identify this key (e.g. `personal`, `work`).
Allowed characters: letters, digits, `-`, `_`.

* ``
Path to the private key file (e.g. `~/.ssh/personal`).

Examples:

```bash
ssh-manager key add personal ~/.ssh/personal
ssh-manager key add work ~/.ssh/work
```

If the alias already exists or the file does not exist, the command exits with an error.

---

### `key use`

```bash
ssh-manager key use
```

Sets the given alias as the **active** key:

* Validates that the alias exists in the registry.

* Validates that the private key file exists.

* Updates the symlink:

```text
~/.ssh/github_current ->
```

* Saves the active alias in:

```text
~/.ssh/github-current-alias
```

* Optionally calls `ssh-add` for the selected key (if `ssh-add` is available).

Example:

```bash
ssh-manager key use personal
```

---

### `key current`

```bash
ssh-manager key current
```

Displays information about the active key:

* Current alias.
* Path to the key.
* Symlink status (`~/.ssh/github_current`).

Example output:

```text
Current alias: personal
Key path: /home/user/.ssh/personal
Symlink: /home/user/.ssh/github_current -> /home/user/.ssh/personal
```

---

### `key test`

```bash
ssh-manager key test
ssh-manager key test
```

Tests the SSH connection to GitHub using:

* The **specified alias**, or
* The **current active key** (if no alias is given).

It runs:

```bash
ssh -T -o IdentitiesOnly=yes -i @
```

Defaults:

* `host`: `github.com` (can be changed via `GITHUB_SSH_HOST`).
* `user`: `git` (can be changed via `GITHUB_SSH_USER`).

Examples:

```bash
ssh-manager key test
ssh-manager key test personal
ssh-manager key test work
```

On success you should see something like:

```text
Running: ssh -T git@github.com
Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.
```

---

## Testing

The project includes comprehensive test coverage using [bats-core](https://github.com/bats-core/bats-core).

To run the tests:

```bash
bats tests/ssh-manager.bats
```

All 34 tests should pass, covering all functions, error cases, and edge cases.

---

## Environment variables

Optional variables:

* `GITHUB_SSH_HOST`
SSH host. Default: `github.com`

* `GITHUB_SSH_USER`
SSH user. Default: `git`

These affect only the `key test` command. Normal Git commands still follow your `~/.ssh/config`.

Example:

```bash
GITHUB_SSH_HOST=github.com GITHUB_SSH_USER=git ssh-manager key test personal
```

---

## Internal files

`ssh-manager` uses the following files in `~/.ssh`:

* `github-keys.conf`
Registry of aliases and key paths.
Format (one entry per line):

```text
alias;/full/path/to/private/key
```

* `github-current-alias`
Contains only the current alias (e.g. `personal`).

* `github_current`
Symlink pointing to the active private key.
This is the file that should be referenced in your `~/.ssh/config`.