DEV Community

Rob Hoelz
Rob Hoelz

Posted on

SSH Shortcuts

Originally posted at

Let's say that you have a server you SSH into very often. Let's also say that sshd on that server is running on a non-standard port to avoid annoying scanners (we'll using 53718 in this example). To SSH into this server, you run the following:

  $ ssh -p 53718

What a mouthful! There's got to be a way to avoid typing that much! A technique I've often seen people use is to create a shell alias for this:

  $ alias example='ssh -p 53718' # in bash

So now all I'd have to do is run example. Piece of cake, right?

But what if you're using scp? Or sftp? Or rsync? Or git remote add? Or Vim's netrw plugin? Suddenly your simple shell alias doesn't seem so cool!

However, there is a solution to this! Enter your SSH config file.

Your SSH config file, located at ~/.ssh/config, can do a lot of cool things. To replicate our example above, we'd add this to our configuration:

Host example
  User rob
  Port 53718

Now all I need to do is ssh example, and voilà! It works! It's four characters longer than the alias, but the shortcut is applied to the following commands as well:

  scp example:that-important-file.txt .
  sftp example
  rsync -ar example:my-project/ .
  git remote add example ssh://example/~/my-project
  vim scp://example/that-important-file.txt

If you're interested in shortening the Git example even further, see my blog post about it.

Your SSH configuration file is very powerful; I recommend checking out man ssh_config for more options to play with!

Top comments (7)

kapouer profile image
Jérémy Lal

Also with wildcards

Host *
  User joe
chintukarthi profile image
Karthikeyan Dhanapal

Hi. I am having doubts regarding using the ssh login to check the server status with child_process module. Is that something that you can help me with? Thanks.

hoelzro profile image
Rob Hoelz

I can try; I'm not sure which child_process module you're referring to, though. Do you have some code I could look at?

chintukarthi profile image
Karthikeyan Dhanapal

This is a coffescript for hubot.

code :

---> Code Starts here

child_process = require 'child_process'
module.exports = (robot) ->
robot.respond /check status/i, (res) ->

child_process.exec("sshpass -p 'password' ssh username@hostname", (err, stdout, stderr) ->
  if err

       ----> code ends here

Actually the problem is i'm not sure whether the syntax for child_process is correct. I am new to coffeescript and hubot. So i don't know how to resolve this

Thread Thread
hoelzro profile image
Rob Hoelz

I have no familiarity with CoffeeScript or Node's child_process module, so I don't know how much help I'll be, but I'll try! Are you getting an error message or anything?

Thread Thread
chintukarthi profile image
Karthikeyan Dhanapal

yeah. Error : Response not OK: no_text.

I think passing the shell commands within the child_process has some issues. If you come up with any solution or something related to that, please let me know. Thanks.

vlasales profile image
Vlastimil Pospichal • Edited
 $ alias example='ssh example'
 $ example 'command parameters'