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 😉