Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

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 matt@192.168.1.45 (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
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 192.168.1.45? 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 –  XAleXOwnZX Jan 25 at 23:54
1  
@XAleXOwnZX You can't set custom flags, but you can set options. -q is equivalent to LogLevel QUIET. –  Gilles Jan 26 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

 
discard

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.