16

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 3

29

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
5
  • Tried the bottom [with 'foo'] and it didn't work. ["port 22 refused"].. the other one works though
    – Matt
    Nov 18, 2011 at 0:19
  • @Matt Did you run ssh foo and not ssh 192.168.1.45? You use the nickname given after Host, I should have stated this more clearly in my answer. Nov 18, 2011 at 1:03
  • 1
    ohh.. no, I guess I misunderstood.
    – Matt
    Nov 18, 2011 at 1:23
  • Is it possible to add custom flags? I'd like to automatically have -q enabled
    – Alexander
    Jan 25, 2014 at 23:54
  • 1
    @XAleXOwnZX You can't set custom flags, but you can set options. -q is equivalent to LogLevel QUIET. Jan 26, 2014 at 0:03
3

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

0

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

2
  • Thank you but does it answer the question (port number)? May 17, 2019 at 9:10
  • 1
    @MarcVanhoomissen thanks for pointing out. I have updated with port May 18, 2019 at 1:15

You must log in to answer this question.

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