DEV Community

Andrés Villarreal
Andrés Villarreal

Posted on • Originally published at kaeruct.github.io on

6

fzf + SSH Config Hosts

SSH has a nice feature in which you can store aliases for frequently accessed hosts.

Combining this with fzf, you can have a nice quick shortcut to quickly pick a server to connect to into.

This comes in very handy if you need to ssh into different servers and forget their IP or hostname often.

Here’s a sample ssh config file (normally located at ~/.ssh/config):

# see https://man.openbsd.org/ssh_config.5 for all the available configuration settings
Host runner-staging
    HostName 10.0.0.8
    User alpha

Host runner-production
    HostName 10.0.0.9
    User beta

Host mainframe
    HostName mainframe.computer.world
    User hackerman

Enter fullscreen mode Exit fullscreen mode

Here’s a small shell function which calls fzf with the hostnames configured and allows you to pick one to connect to:

s () {
  local server
  server=$(grep -E '^Host ' ~/.ssh/config | awk '{print $2}' | fzf)
  if [[ -n $server ]]; then
    ssh $server
  fi
}

Enter fullscreen mode Exit fullscreen mode

Add this function to your .bashrc (or .zshrc, or whichever config file for your shell) and reload the configuration.

Now, you can quickly ssh into mainframe by typing s:

$ s

# fzf will allow to quickly search and pick your server
> runner-staging
  runner-production
  mainframe
  3/3 ──────────

# press enter and you will be connected!
[hackerman@mainframe.computer.world ~]$ 

Enter fullscreen mode Exit fullscreen mode

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (3)

Collapse
 
dservnh profile image
dserv-nh

Your snippet contains a syntax error. The fourth line should be
if [[ -n $server ]]; then

Collapse
 
kaeruct profile image
Andrés Villarreal

Thank you so much for the correction. Somehow dev.to messed it up when I imported this post from my blog.

Collapse
 
nunesfd profile image
Flávio Nunes

Very good, this is great, especially when you have a lot of Hosts to manage!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay