SSH quick and easy login setup

3 June 2023 | Cloud Server, DevOps, EN

When you often login into servers with SSH there’s time to be saved. This article will help you save time and lessen distractions on remembering user- and servernames. There’s three ways of making thing easy: Aliasing, SSH tweaking and TAB completion.

Command aliasing

Aliasing means you use an alias as a command that actually executes the real command. In other words, this means substituting full SSH commands into a simple abbreviation.

You add these aliases to the file .aliases and list all of your favorite server in there. This file is read by bash every time you start a new terminal.

# .aliases
alias srv1='ssh user@server1.example.com'

 

Having done that you can simple login to a server with:

$ srv1

 

Should you have the need for extra options like agent forwarding (with -A) or using a different port (with -p 2222) you can simply add those to your aliased command.

alias srv1='ssh -A -p 2222 user@server1.example.com'

 

Note: open a new terminal when you’ve made changes to your .aliases file, or run source .aliases.

 

SSH tweaks

If you have the need for more complex SSH configuration settings it would be better to put those in your .ssh/config . This keeps your alias list clean and readable. You can simply move (part of) those parameters to said file.

# global settings (for all hosts)
Host *
    TCPKeepAlive yes
    IdentityFile ~/.ssh/id_ed25519

# settings per host (if needed)
Host srv1.example.com   appname.example.com
    Hostname srv1.example.com
    IdentityFile ~/.ssh/id_ed25519_for_srv1
    User username
    Port 2222

 

TAB completion

Then there’s the often used commandline feature called <TAB>-completion. It’s used in bash for a variety of commands. Commands can shows possible options when pressing<TAB>. In SSH’s case it’s a way to show a list of your known hostnames. It shows you this list when you type in ssh srv and press <TAB>.

$ ssh srv<TAB>
srv1 srv2

 

Typing a few more characters of the servername (until unique) and <TAB>-ing again. It automatically adds the full servername to your command. You can then press enter as you normally would and login to the server. This list is based on all the hostnames found in your .ssh/config file.

Create the file below automatically updates this list whenever you add a hostname to your .ssh/config . This requires you to have the package bash-completion setup on your system. In case of Apple’s OSX you need to install brew package manager first (found here https://brew.sh/).

Create the following file /etc/bash_completion.d/ssh (Linux) or /usr/local/etc/bash_completion.d/ssh (OSX) on your system.

# Update hostnames list for bash-completion command
_ssh() 
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts=$(grep '^Host' ~/.ssh/config 2>/dev/null | grep -v '[?*]' | cut -d ' ' -f 2-)

COMPREPLY=( $(compgen -W "$opts" -- ${cur}) )
return 0
}
complete -F _ssh ssh

 

Then add and source said file to your .bashrc like so.

# In case of Linux
source /etc/bash_completion.d/ssh

# or in case of OSX
source /usr/local/etc/bash_completion.d/ssh

 

Now any time you start a terminal your .ssh/config file contents is read and the tab completion host list is updated for you to use. More information on the  provision script is found here: bash – Autocomplete server names for SSH and SCP – Unix & Linux Stack Exchange

 

Time for coffee! … Since you’re saving time already 😉

About Gerard

Gerard Petersen is the founder and owner of CAP5. He has over 35 years of ICT experience and 10+ years of experience in the entrepreneurial landscape. Gerard is driven by the optimal combination of people and technology and aims to make a social impact. Through CAP5, Gerard is active as a consultant for ICT operations and management.

Want to have a talk?

Plan a call to have a conversation about your challenges.

Related articles

Why you should version your software (and not pin it)

Why you should version your software (and not pin it)

Key takeways In this article you’ll learn more about the following main aspects: Importance of Software Versioning - Assigning versions to your software enhances clarity and control, ensuring all stakeholders are aligned and deployments are consistent across different...

Series: WordPress application hacked (and how to recover!)

Series: WordPress application hacked (and how to recover!)

In this article series I describe everything we did to recover a customer's hacked Wordpress application platform. You’ll learn what tasks have priority and how you can recover the hacked installation. You'll get to see examples of malicious code that was injected as...

WordPress application hacked 4/4 – Business processes and security

WordPress application hacked 4/4 – Business processes and security

This is the last article in the series Wordpress application hacked (and how to recover!) on how to recover from a Wordpress hacking attempt. In the previous articles you could read how to prioritize immediate actions and how to recover your Wordpress platform. That’s...

WordPress application hacked 2/4 – How to recover the platform

WordPress application hacked 2/4 – How to recover the platform

In the previous post Wordpress application hacked 1/4 - Immediate damage control, I explained what to do immediately after a security breach is detected. In this article we'll be looking specifically at the wordpress platform and the quickest way to get it healthy and...

WordPress application hacked 1/4 – Immediate damage control

WordPress application hacked 1/4 – Immediate damage control

In this first article of the series Wordpress application hacked (and how to recover!), I help you understand what needs to be done immediately after you notice your wordpress application is hacked.   Introduction I recently had to recover a Wordpress based...

Open chat
1
Hulp nodig?
Scan the code
Hi 👋 ... kan ik je helpen?