2

I have a lot of servers that I changed my port to 2222.

Can I change on my computer the default port for ssh command? Instead of 22 I want to use 2222 as default.

For instance, instead of using ssh root@ip -p 2222

I want to use ssh root@ip only.

And when I need to use port 22 I type ssh root@ip -p 22

Lucas César
  • 73
  • 1
  • 9
  • 1
    You can configure a port number per host in /etc/ssh/ssh_config. – muclux Jan 31 '18 at 19:21
  • . . . or per user via your `~/.ssh/config` file - similar to here [Permanently store addresses when using SSH](https://askubuntu.com/questions/666788/permanently-store-addresses-when-using-ssh/666798#666798) – steeldriver Jan 31 '18 at 19:26
  • 1
    ...or in your `~/.bashrc`: `alias ssh="ssh -p 2222 "` – waltinator Jan 31 '18 at 21:17

1 Answers1

4

Use your favorite editor to modify ~/.ssh/config

nano ~/.ssh/config

and enter

Host *
User root
Port 2222

this would mean, by default, if you tried

ssh remote-host.com

SSH would actually do this

ssh -p 2222 root@remote-host.com

you can, of course, also specify this per host with a special username

Host remote-host1 remote-host2 remote-host3
User jackvanier
Port 2222

these settings can also co-exist

Host remote-host1 remote-host2 remote-host3
User jackvanier
Port 2222

Host *
User root
Port 2222

See also here for some more ideas.

Robert Riedl
  • 4,321
  • 16
  • 37