Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A ssh server on 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?

share|improve this question
up vote 15 down vote accepted

Sure, you can put these settings in ~/.ssh/config.

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 matt@ (Host foo means that the following options apply when you run ssh foo, and the HostName directive makes foo a nickname for with all the other options):

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.