57

I know how to connect 'to' a certain port when ssh'ing.

ssh user@remotehostip -p XXX

but is there a way to establish a ssh 'with' a certain port?

I mean, is there a way to specify the port which my local computer will be using?

  • Did you mean changing server port or client receiving port? Edit: Ok, i deleted my suggestion. Favorited. – polym Jun 24 '14 at 1:32
  • I want to specify the client's port – kwagjj Jun 24 '14 at 1:40
  • 3
    @kwagjj you mean the source port? If so no, you cannot control the source port. – Patrick Jun 24 '14 at 1:45
16

That is not easily possible. How that can be done depends on where the source port shall be seen: Locally, too, or is it enough if it's the right port from the perspective of the external network?

You can run the SSH client in an LXC container. I have never done that thus I cannot explain it to you in detail. But you create a virtual network interface and attach it to this container so that ssh uses this interface because it is the only (external) interface it sees.

On the host system it should be possible to detect that a packet comes from this interface. Thus you can use Netfilter's NAT (SNAT) for rewriting the source address with something like:

iptables -t nat -A POSTROUTING -o vnet0 -p tcp --dport 22 -j SNAT --to-source :1234

Of course, this does not work (or becomes more complicated) if you connect to ports different from 22.

  • 1
    thanks... but I think this info is out of my league... but still thanks though :) But if I really really hit a dead end with what I'm doing.. perhaps I'll turn my head over to this answer again :) – kwagjj Jun 24 '14 at 2:54
  • yeah it's really difficult to find information about that issue. I found about sshfs and someone said that it worked with this command, but I don't believe it. If you find the answer, please let us know :)! – polym Jun 24 '14 at 2:57
  • @kwagjj It may be easier (to understand) to install a complete (text mode) Linux as a real VM. If you use this VM (or a certain account in it) for SSH only then you can do this with the VM Netfilter. – Hauke Laging Jun 24 '14 at 3:10
6

The command syntax you're looking for perhaps is this. ssh -p XXX user@remotehostip You have to change option's order. Try it now, it works for me. Best regards.

2

A simple solution for this if you are port forwarding through your router is to set the inbound port to whatever you want and the local port to 22. Your machine will still be taking ssh connection on 22 like normal but to actually connect from outside your network you would use your inbound port. If you're just trying to stop bots from hammering your machine on 22 this should do it.

This might not help for your specific problem but I would imagine a lot of people end up here for this reason.

0

I proposed a -Z portnum option...

Source here: https://github.com/Zibri/openssh-portable

And here is the pull request: https://github.com/openssh/openssh-portable/pull/130

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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