I want to login to my server that has custom ssh port like -
ssh -p <my server port number> root@example.com
Now I want such short command (something like alias) that I don't need to input that port number again and again. Like -
ssh my-short-code
Is it possible? If yes any hint are welcome.
Go to your ssh
config file - mine is in $HOME/.ssh/config
, and add the following:
Host example
HostName example.com
User root
Port 2229
Then you can just do:
ssh example
If you want to be able to run X11 applications (e.g. xclock
or xterm
or any graphical software development tools) on the remote machine, but display the graphics locally, you can forward X11 by adding the following 2 lines after the ones shown above:
ForwardX11 yes
ForwardX11Trusted yes
In response to your further question in the comments. Yes, you can just keep adding as many additional servers as you like, with different ports, usernames, keep-alive intervals for each:
Host example
HostName example.com
User root
Port 2229
Host sercon
HostName sercon.onlinehome-server.info
User u8566723
Port 378
StrictHostKeyChecking no
You can also put a generic entry at the start with settings you want for most servers, and then override them later on individual servers:
Host *
Username mark
ServerAliveInterval 10
Host XYZ
<specific stuff for this host>
Host <ABC>
<specific stuff for this host>
To get further information, use this in Terminal:
man ssh_config
If you happen to be on a Mac under macOS/OSX
and you want X11 forwarding to work with XQuartz, put these 3 lines at the top of the file:
Host *
# https://serverfault.com/a/859370/235272
XAuthLocation /opt/X11/bin/xauth
You can't do exactly that, but you can do something very similar. In your .bashrc
add this line:
export myshortcode='-p <my server port number> root@example.com'
Now you execute the command loke this:
ssh $myshortcode
Or you add an alias like
alias ssh_port='ssh -p <my server port number> root@example.com'