6

I changed my SSH port in the /etc/ssh/sshd_config file and then restarted the ssh service. I implemented fail2ban and updated the port to my SSH under that config. I also then implemented the UFW firewall and allowed incoming connections to my new SSH port.

However, when I try and login with my SSH key using ssh -i /Users/myuser/.ssh/vpsssh [email protected] it's trying to connect to port 22 instead of the defined port I have.

user1048676
  • 217
  • 1
  • 4
  • 9
  • 2
    Server and client do not about each other automatically. They both use the default port 22, unles is changed, on `sshd_config` for server, or specified on command for cliente like [here](https://askubuntu.com/a/660556/260379) – bistoco Jan 22 '19 at 15:35
  • 1
    They could be trying to login to an Ubuntu server from a Windows PC, which I think is still on topic. – Arronical Jan 22 '19 at 17:13
  • Is there a reason you're using a different key for each host you connect to? – R.. GitHub STOP HELPING ICE Jan 23 '19 at 17:41

2 Answers2

27

You can specify a non-default port on the ssh client command line using the -p option. From man ssh:

 -p port
         Port to connect to on the remote host.  This can be specified on
         a per-host basis in the configuration file.

You may wish to put both the port number and the identity file location for the host in a ~/.ssh/config file so that they don't need to be specified every time on the command line.

Ex.

Host myremotehost
  Hostname      555.555.555.555
  User          user
  Port          20002
  IdentityFile  /Users/myuser/.ssh/vpsssh

Then you will be able to use:

ssh myremotehost
pa4080
  • 29,351
  • 10
  • 85
  • 161
steeldriver
  • 131,985
  • 21
  • 239
  • 326
  • Probably useful to many when you start defining that kind of configuration: [How do I configure SSH so it dosen't try all the identity files automatically?](https://superuser.com/questions/268776/how-do-i-configure-ssh-so-it-dosent-try-all-the-identity-files-automatically). – Giacomo Alzetta Jan 23 '19 at 14:46
5

Note that ssh accepts commands in the URI form, such as ssh://[email protected]:<port>. Based on that, what I do when logging in to a remote server with a private key is the following:

ssh -i ~/.ssh/id_rsa ssh://myuser@domain_name.com:2222
Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
  • This answer over at "Unix Linux S.O." https://unix.stackexchange.com/questions/75668/why-doesnt-the-ssh-command-follow-rfc-on-uri explains that URI is actually not accepted by SSH. I have tried this in my updated Ubuntu 20.04 and it didn't work so I don't think this answer is valid. – badc0de Sep 22 '21 at 12:53