If you have a lot of Droplets on your account, you probably agree that it’s hard to keep track of all of them—especially if you create new ones and destroy them frequently for one-off workloads. A common pain point is having to look up a Droplet’s IP address when needing to SSH into it.
I created do-ssh-alias to help address that. Let’s look at how it can help.
Let’s assume you have a Droplet named shiny-blog. Usually you would look up its IP address and then SSH into it like so:
What if, instead, you could simply run the following command?
This is where do-ssh-alias comes in. It creates SSH aliases for all your Droplets at once so you can easily SSH in, without having to worry about what user or hostname to use.
It is especially useful if your Droplets’ hostnames are FQDNs (e.g. shiny.example.com) that don’t point directly to the Droplets’ IP addresses. One example is using Cloudflare in front of your website, so your domain name resolves to a Cloudflare server instead of your Droplet.
do-ssh-alias depends on the programs jq and doctl. The first step is installing jq and installing doctl. Linked are the installation instructions for each program. Once you install doctl, log it in to your DigitalOcean account.
With the dependencies taken care of, let’s now install do-ssh-alias. You can either download the script from GitHub or use the command line:
It’s always a good idea to review any scripts you download from the internet before executing them.
Once you have the file on your computer, update its permissions to allow it to be executed:
chmod +x do-ssh-alias
It’s now ready to use. To generate aliases for your Droplets, run:
./do-ssh-alias > ~/.ssh/do_aliases
This will run do-ssh-alias and save the results in the file ~/.ssh/do_aliases.
Finally, update your ssh config to actually use the file with the aliases. Open ~/.ssh/config in a text editor and add the following line at the top:
That’s it. You can now SSH to your Droplets using their hostnames! Any time you create or remove Droplets, simply run it again to update the configuration.
do-ssh-alias only generates SSH aliases, but it accepts a few options for some flexibility:
Below is an example of using all three options.
Let’s assume you have the following Droplets on your account:
do-ssh-alias -u sammy -i droplet1 -s .domain.com
will generate aliases for:
all using the username sammy to log in. The SSH config will look like so:
Host droplet2.domain.com droplet2
Host droplet3.domain.com droplet3
Here are some resources you may find useful:
Note: doctl itself provides similar functionality through the doctl compute ssh command which allows you to SSH into a Droplet using its ID or name. The main difference is that doctl looks up the Droplet’s IP address using the DigitalOcean API every time you run it, while do-ssh-alias generates a static config file that ssh reads. You might prefer do-ssh-alias if:
February 8, 2024•3 min read
February 1, 2024•3 min read
January 18, 2024•3 min read