Phil Zona Dot Net

Copy your public SSH key with one command

First off, this is a laziness post. Or to spin things more favorably, some commentary on a convenience-oriented one-liner. I love shortcuts and anything that will save me 3 seconds, regardless of how those seconds add up over time.

The problem I'm solving is this: every so often, I find myself needing to copy my public SSH key to a new service, but I don't have an easy way to grab it. The command to output the key is straightforward:

cat ~/.ssh/id_rsa.pub

...but copying it requires my mouse. I'm not a hardcore mouse-avoider (although I do think the keyboard-only crowd is impressive for their dedication), but I do use tmux with multiple panes, which can make copying problematic. If I split my screen vertically, which I often do, the selection spans multiple panes as you can see here:

tmux_selection_pain

Sure, this is specific to my command line setup, but I came up with a solution that I think everyone will appreciate.

Copy your SSH public key with one command

Okay I'll cut to the chase. The following command will allow you to copy your public SSH key to the clipboard with an alias:

MacOS

echo 'alias pubkey="pbcopy < ~/.ssh/id_rsa.pub"' >> ~/.bashrc && source ~/.bashrc

Linux

echo 'alias pubkey="xsel -b < ~/.ssh/id_rsa.pub"' >> ~/.bashrc && source ~/.bashrc

Shoutout to Ricardo Feliciano for the Linux command - follow him on Twitter for more gems like this 🙂

Alternate shells

The above commands assume you're using bash as your default shell, but you can modify them slightly for alternatives like Zsh or Fish by swapping out ~/.bashrc for ~/.zshrc and .config/fish/config.fish, respectively.

I don't have a Windows machine to test on, although if anyone has an equivalent command they'd like to contribute, let me know and I'll be sure to credit you 😎

A quick note

This command does not apply to SSH sessions. For instance, if you are connected to a server and you run the corresponding command, it will copy the key to that system's clipboard program if one exists. This might be intuitive, but I think it's worth mentioning to be safe.

How it works

If you're new to the command line, or any of the pieces of this don't look familiar, I'll give a brief rundown of what's going on. First, you're adding a new line to your ~/.bashrc file, which sets up different options and environment variables every time you open a new shell instance. The specific line we're adding, assuming you're on MacOS, is:

alias pubkey="pbcopy < ~/.ssh/id_rsa.pub"

The Linux command is almost identical, the difference being pbcopy versus xsel -b. In both cases, this sets up a shell alias, allowing you to run a command or series of commands with one shortcut. The shortcut here is pubkey, which I found fitting given what the alias is doing; if you'd like to rename the alias, however, you can totally do that. You can also create these yourself by manually editing ~/.bashrc or the corresponding shell config on your system.

Back to the alias itself, both pbcopy and xsel -b are tools that copy a given input to the system clipboard. In our case, this is the content of the standard public key file.

The stuff after the ampersands? It lets you start using your alias right away. Your shell loads from its configuration file, ~/.bashrc assuming Bash, each time it starts up a new instance. When you add a new alias, option, or environment variable to your shell configuration, you'll need to close your current session and reopen it for the new settings to take effect. Or alternatively, you can tell it to load a configuration manually - this is what we're doing with source ~/.bashrc, which runs after we add the new alias.

Want a more visual breakdown? Check out the command on explainshell, a tool I use often for understanding one-liners.

That's it!

To wrap up, you can start using this by running pubkey in your terminal. There will not be any output (although you could certainly add something like && echo "Copied!" to the alias if you wanted!), but once you execute the command, you can immediately paste your public key wherever it's needed.

Is this the most innovative, impressive, or useful thing ever? No. But after spinning up several GitLab instances for testing different features, copying my public key was something I needed to do quite often. More important than all that, though, is that modifying your command line can be insanely fun. New commands don't have to be comprised of complex tools or even full scripts. And when you can write a quick alias to make your computer do something it couldn't do before...well, that's the whole point, right?