6

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?

10

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)

4
  • 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. – AbstractVoid Jan 19 '18 at 10:42
  • In case we use zsh , we should add the source ~/.bash_profile line at the end of .zshrc file at the following path: /Users/YOUR USER NAME/.zshrc, then restart your Terminal/iTerm2 app. The source of the answer is stackoverflow.com/questions/34352907/… – awaik Jan 26 at 4:20
  • @awaik This may be a solution for people using both bash and zsh. People using only zsh probably want to keep their shell functions/aliases directly in zsh init files. – nohillside Jan 26 at 6:18

You must log in to answer this question.

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