Jump to content


Recommended Posts

How many times have I seen people using SSH aliases to shorthand the long statements they need to type to login:

alias ssh-home="ssh -p 12345 znx@somelong.hostname.com"


Now whilst this eases the pain, you are missing some real magic that SSH and BASH can provide you! So lets get started:


Edit/Make the ~/.ssh/config

Host some
  HostKeyAlias somelong.hostname.com
  HostName somelong.hostname.com
  User znx
  Port 12345


Now what's that all about you say, well now the original long top line can be replaced with:

ssh some




But that's not all, you can infact setup BASH to provide you with a tab completion on those new short hostnames!


Edit/Add to your ~/.bashrc


function _ssh() {
 local cur
 if [ "${cur:0:1}" != "-" ]; then
COMPREPLY=( $(awk '/^Host '$2'/{print $2}' $HOME/.ssh/config) )
 return 0

complete -F _ssh ssh sftp scp
complete -A hostname ssh sftp scp


Edit/Add to a ~/.hosts  some.long.host hostname other.long.host host


To update the current shell you are running do:

source ~/.bashrc


Now here is the magic:



At which point you will be given the option of "some" or "some.long.host". Better than all that is this, when you edit your ~/.ssh/config or your ~/.hosts to update or add a new host, it is INSTANTLY in your tab complete.




ssh home
ssh work
ssh somefriend
ssh here
ssh there


;) Love Linux ;)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...