4

I'm working a lot with SSH connections to servers, by for example forwarding localports or just establishing SSH connection to manage some servers.

For this I would like to be able to create some terminal shortcuts for example:

connect ssh server1
-->should run command: ssh xyx@xy
connect ssh server2 forward
-->should run command: ssh -NC user@xy -L 9999:localhost:3306

Is there a way to specify custom terminal commands?

5

Simply add aliases in ~/.bash_profile:

If the file .bash_profile doesn't exist:

touch ~/.bash_profile

Then add aliases with nano ~/.bash_profile. Examples:

alias ssh01='ssh xyx@xy'
alias ssh02='ssh -NC user@xy -L 9999:localhost:3306'

Then source the file or restart Terminal.app:

source ~/.bash_profile

Entering ssh01 in the shell will then execute ssh xyx@xy or ssh02 the second command.

The alias (i.e. ssh01) mustn't be another valid command in your path (e.g ssh-add)

  • And if you want to define custom commands, not just aliases, just chmod +x an sh file (without extension) and put it in your ~/bin. – Majora320 Apr 17 '16 at 4:54
  • I already had stuff in my bash_profile file, and when I appended aliases to the end of the file, they didn't work, so I had to move them to the top of the file. – WallTearer Jan 19 '18 at 10:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .