I understand that the permitopen="host:port" option limits the use of ssh -L to a single remote port. Is there a way to do the same with ssh -R?
- 81,893
- 31
1 Answers
is there a way to do the same with
ssh -R?
Not really "the same". For the most common usage of ssh -R there is little to no point in having such option, but read this answer to the end.
Analysis
PermitOpen is an option in sshd_config. It affects not only ssh -L, but also ssh -D, ssh -W and ssh -J (at least). I think it's designed as a filter to TCP connections initiated by the SSH server on request of a client.
Imagine you're an admin of the server. The client, for whom SSH may be the only entrance to the system, may be totally untrusted. The system may be a bastion the client should use only to obtain a tunnel to well-defined location(s). You are able to set things up, so the client cannot run arbitrary commands. Then you may want your computer not to initiate connections to random addresses for this client. PermitOpen lets you configure this easily.
An option in sshd_config that would limit the target of client's ssh -R is not useful for you (the admin of the SSH server) because:
In case of
ssh -R, any connection to the targetaddress:portwill originate from the client's system which is not your concern. Your concern is the interface and the port your system will listen on. There is an option to limit this:PermitListen.I don't think
ssh(the client's application) even reports thisaddress:portto the SSH server.Even if it did, a modified
ssh(or anything using the protocol) could lie. The server has no means to tell whatsshdoes with any data on its end.Even if the client's application didn't lie, the client could request
address:portyou allow and locally useiptables,socat,/etc/hostsor its own DNS server to connect the tunnel to another address and another port.
In short: as an admin of the server you cannot control the client's side anyway. Do not try; it's not your concern.
Maybe you meant an option to limit ssh -R on the client's side, in ssh_config. At first glance such option makes some sense; but think about it and you'll notice that:
It's the client who wants a tunnel to some
address:port, so certainly he or she won't be interested in any limiting in~/.ssh/config.An admin of the client's system (where the client may be a regular user) may want to impose some limitations, e.g. in
/etc/ssh/ssh_config. But withsshpersonal config and command line options take precedence, the client can override any global config. This is by design and you shouldn't fight it becauseā¦If you're the admin of the client's system and you don't want the client to connect to some
address:portthen what difference does it make if they try to establish such connection usingssh,ncor anything else? You need a solution with scope broader than the config ofssh.Note there's asymmetry: on the server side, in cases where SSH is the only entrance for the client to the system, limiting in
sshd_configmakes sense. On the client's side the client is already in the system,sshis just a program he or she can use; therefore limiting (if needed) should be more system-wide than inssh_config.
PermitRemoteOpen
There is one scenario in which the client may be interested in limiting address:port his or her ssh can connect to. And it is with ssh -R; but not the ssh -R that mirrors ssh -L (and I guess this is what you had in mind), but the form of ssh -R that mirrors ssh -D.
With ssh -R, if you (as a client) don't specify any address:port to forward to, then the SSH server will become a SOCKS proxy and it will be your computer who will connect to addresses requested by people using the proxy. It's like ssh -D but the other way around.
In case of ssh -D, whoever uses the proxy chooses address:port the SSH server shall connect to on their behalf. The admin of the server may want to limit this, PermitOpen is the way. In case of ssh -R acting as a SOCKS proxy, similarly whoever uses the proxy chooses address:port the SSH client application shall connect to on their behalf. As a client you may want to limit this because in general these address:ports are not necessarily (in practice: never) your choice, but it's your ssh connecting to them. You can use PermitRemoteOpen in your ~/.ssh/config. This is the option to limit ssh -R, but it only works when ssh -R creates a SOCKS proxy on the server.
Summary
PermitOpeninsshd_configon SSH server limitsssh -L(andssh -D,ssh -W,ssh -J).PermitRemoteOpeninssh_config(or~/.ssh/config) on a client's side limitsssh -R.Each option is governed by the person whose application will eventually initiate connections: the admin of the server (owning
sshd) and the client (runningssh) respectively. Limitingssh -Rby the admin of the server would be futile.Each option works in cases where the governing person is not the person who chooses
address:portfor the application to connect to. ForPermitOpenall cases are like this. ForPermitRemoteOpenonlyssh -Rcreating a SOCKS proxy on the server is like this.Limiting
ssh -Rof other types by the client makes no sense (the client would limit himself) and there is no option for it.
- 81,893