A ssh server on 192.168.1.45 listens on port 2222. I know I can just add -p2222
or add an alias in ~/.bash_aliases
, but is there a way to force a certain port when I ssh a specified IP?
3 Answers
Sure, you can put these settings in ~/.ssh/config
.
Host 192.168.1.45
Port 2222
You can take this opportunity to give the machine a nickname, and set other options, in particular the remote user name. For example, the following declaration makes ssh foo
equivalent to ssh -p 2222 [email protected]
(Host foo
means that the following options apply when you run ssh foo
, and the HostName
directive makes foo
a nickname for 192.168.1.45 with all the other options):
Host foo
HostName 192.168.1.45
Port 2222
User matt
-
Tried the bottom [with 'foo'] and it didn't work. ["port 22 refused"].. the other one works though– MattNov 18, 2011 at 0:19
-
@Matt Did you run
ssh foo
and notssh 192.168.1.45
? You use the nickname given afterHost
, I should have stated this more clearly in my answer. Nov 18, 2011 at 1:03 -
1
-
Is it possible to add custom flags? I'd like to automatically have -q enabled Jan 25, 2014 at 23:54
-
1@XAleXOwnZX You can't set custom flags, but you can set options.
-q
is equivalent toLogLevel QUIET
. Jan 26, 2014 at 0:03
You could add the host to your ssh configuration file. Run man ssh_config
from a terminal for some nice reading.
In short place
Host <hostname>
Port 2222
in your ~/.ssh/config
For any ec2 instance IP, automatically picks useranme and key file.
Host *.*.*.*
IdentityFile ~/.ssh/key.pem
Port 22
CheckHostIP no
PasswordAuthentication no
User ec2-user
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
-
-
1@MarcVanhoomissen thanks for pointing out. I have updated with port May 18, 2019 at 1:15