<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://brokenco.de//feed/by_tag/bsd.xml" rel="self" type="application/atom+xml" /><link href="https://brokenco.de//" rel="alternate" type="text/html" /><updated>2026-10-31T17:58:32+00:00</updated><id>https://brokenco.de//feed/by_tag/bsd.xml</id><title type="html">rtyler</title><subtitle>a moderately technical blog</subtitle><author><name>R. Tyler Croy</name></author><entry><title type="html">Dynamically forwarding SSH ports with “commandline disabled”</title><link href="https://brokenco.de//2023/07/10/dynamically-forwarding-ports-again.html" rel="alternate" type="text/html" title="Dynamically forwarding SSH ports with “commandline disabled”" /><published>2023-07-10T00:00:00+00:00</published><updated>2023-07-10T00:00:00+00:00</updated><id>https://brokenco.de//2023/07/10/dynamically-forwarding-ports-again</id><content type="html" xml:base="https://brokenco.de//2023/07/10/dynamically-forwarding-ports-again.html"><![CDATA[<p>I frequently use SSH for accessing one of the many development workstations I
use for work, which includes developing network services among other things. A
couple of years ago I wrote about this hidden gem in <code class="language-plaintext highlighter-rouge">ssh</code> which allows
<a href="/2021/05/16/dynamically-forward-ssh-ports.html">dynamocaily forwarding ports</a>. 
This handy little feature allows dynamocailly adding local port forwards from within an already running SSH session. Recently however this feature has stopped working properly, emitting <code class="language-plaintext highlighter-rouge">commandline disabled</code>.</p>

<p>It turns out that this is due to a backwards incompatible change which <a href="https://www.openssh.com/txt/release-9.2">OpenSSH released in 9.2</a> earlier this year:</p>

<blockquote>
  <p>ssh(1): add a new EnableEscapeCommandline ssh_config(5) option that controls
whether the client-side ~C escape sequence that provides a command-line is
available. Among other things, the ~C command-line could be used to add
additional port-forwards at runtime.</p>
</blockquote>

<p>The reason for this change is to support some sandboxing use-case which I don’t entirely understand but also don’t need, so I needed to add the following option to my host entries in <code class="language-plaintext highlighter-rouge">~/.ssh/config</code>:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Host foobar
    Hostname 172.16.1.1
    EnableEscapeCommandLine yes
</code></pre></div></div>

<p>This can also be configured on the command line with <code class="language-plaintext highlighter-rouge">-o EnableEscapeCommandline=yes</code>. Happy port forwarding!</p>]]></content><author><name>R. Tyler Croy</name></author><category term="ssh" /><category term="bsd" /><category term="linux" /><summary type="html"><![CDATA[I frequently use SSH for accessing one of the many development workstations I use for work, which includes developing network services among other things. A couple of years ago I wrote about this hidden gem in ssh which allows dynamocaily forwarding ports. This handy little feature allows dynamocailly adding local port forwards from within an already running SSH session. Recently however this feature has stopped working properly, emitting commandline disabled.]]></summary></entry><entry><title type="html">Dynamically forwarding SSH ports</title><link href="https://brokenco.de//2021/05/16/dynamically-forward-ssh-ports.html" rel="alternate" type="text/html" title="Dynamically forwarding SSH ports" /><published>2021-05-16T00:00:00+00:00</published><updated>2021-05-16T00:00:00+00:00</updated><id>https://brokenco.de//2021/05/16/dynamically-forward-ssh-ports</id><content type="html" xml:base="https://brokenco.de//2021/05/16/dynamically-forward-ssh-ports.html"><![CDATA[<p>Working over SSH on a big beefy remote machine is a great way to extend the
life of any laptop, but presents challenges when developing network-based
services. Fortunately OpenSSH has a “port forwarding” feature which has been around for a number of
years. Port forwarding allows the user to tunnel a port from the remote machine back to
their local machine, in essence allowing you to access a remote service bound to port 8000 on your own <code class="language-plaintext highlighter-rouge">localhost:8000</code>. When I first learned about this, I would fiddle around
with my <code class="language-plaintext highlighter-rouge">ssh </code> invocations or hardcode a list of potential ports forwarded in
my <code class="language-plaintext highlighter-rouge">~/.ssh/config</code>.  If I was working on a new service that needed a port not yet forwarded, I would disconnect, add it to the list of ports in my <code class="language-plaintext highlighter-rouge">config</code> file, and then reconnect. That was until my pal <a href="https://github.com/nibalizer">nibz
(nibalizer)</a> showed me how to <em>dynamically</em> add
port forwards to an already existing session.</p>

<p><a href="https://www.openssh.com/">OpenSSH</a> provides a number of <strong>escape characters</strong>
that can be used on a running connection, one of these is <code class="language-plaintext highlighter-rouge">~C</code> which will open
up a little command line interface:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>❯
ssh&gt; ?
Commands:
      -L[bind_address:]port:host:hostport    Request local forward
      -R[bind_address:]port:host:hostport    Request remote forward
      -D[bind_address:]port                  Request dynamic forward
      -KL[bind_address:]port                 Cancel local forward
      -KR[bind_address:]port                 Cancel remote forward
      -KD[bind_address:]port                 Cancel dynamic forward
</code></pre></div></div>

<p>From this simple command line you can add or remove local, remote, and dynamic
port forwards! Pretty snazzy! When I was first learning how to use these escape
characters I had a difficult time getting the key-combination correct,
frequently I would somehow screw it up and just send <code class="language-plaintext highlighter-rouge">~C</code> to the shell:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>❯ ~C
zsh: no such user or named directory: 
</code></pre></div></div>

<p>The approach that I have found most reliable is to hold my left shift key down
while I hit <code class="language-plaintext highlighter-rouge">~</code> and <code class="language-plaintext highlighter-rouge">C</code> in sequence. In essence, my left pinky finger keeps
shift depressed while my middle finger and then my pointer finger consecutively
hit the two keys. I share this because when I first started to use these escape
sequences I could never get the timing/cadence correct the first time and it
felt like I was wasting more time than it would take to just disconnect and
reconnect.</p>

<p>Another tip is to make sure you have detached from any active <code class="language-plaintext highlighter-rouge">tmux</code> sessions, as <code class="language-plaintext highlighter-rouge">tmux</code> has it’s own escape sequences and key bindings that you likely want to avoid triggering.</p>

<p>Once you get the hang of it, you can dynamically allocate new ports for whatever service you’re developing and then easily use your local browser/client to access the service under development. Much better!</p>

<p>For more escape characters, refer to the <code class="language-plaintext highlighter-rouge">ssh(1)</code> manpage via <code class="language-plaintext highlighter-rouge">man ssh</code> which has a section dedicated to these magic key combos.</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ESCAPE CHARACTERS
     When a pseudo-terminal has been requested, ssh supports a number of functions through the use of an escape character.

     A single tilde character can be sent as ~~ or by following the tilde by a character other than those described below.  The escape
     character must always follow a newline to be interpreted as special.  The escape character can be changed in configuration files us-
     ing the EscapeChar configuration directive or on the command line by the -e option.

     The supported escapes (assuming the default ‘~’) are:

     ~.      Disconnect.

     ~^Z     Background ssh.

     ~#      List forwarded connections.

     ~&amp;      Background ssh at logout when waiting for forwarded connection / X11 sessions to terminate.

     ~?      Display a list of escape characters.

     ~B      Send a BREAK to the remote system (only useful if the peer supports it).

     ~C      Open command line.  Currently this allows the addition of port forwardings using the -L, -R and -D options (see above).  It
             also allows the cancellation of existing port-forwardings with -KL[bind_address:]port for local, -KR[bind_address:]port for
             remote and -KD[bind_address:]port for dynamic port-forwardings.  !command allows the user to execute a local command if the
             PermitLocalCommand option is enabled in ssh_config(5).  Basic help is available, using the -h option.

     ~R      Request rekeying of the connection (only useful if the peer supports it).

     ~V      Decrease the verbosity (LogLevel) when errors are being written to stderr.

     ~v      Increase the verbosity (LogLevel) when errors are being written to stderr.
</code></pre></div></div>]]></content><author><name>R. Tyler Croy</name></author><category term="ssh" /><category term="bsd" /><category term="linux" /><summary type="html"><![CDATA[Working over SSH on a big beefy remote machine is a great way to extend the life of any laptop, but presents challenges when developing network-based services. Fortunately OpenSSH has a “port forwarding” feature which has been around for a number of years. Port forwarding allows the user to tunnel a port from the remote machine back to their local machine, in essence allowing you to access a remote service bound to port 8000 on your own localhost:8000. When I first learned about this, I would fiddle around with my ssh invocations or hardcode a list of potential ports forwarded in my ~/.ssh/config. If I was working on a new service that needed a port not yet forwarded, I would disconnect, add it to the list of ports in my config file, and then reconnect. That was until my pal nibz (nibalizer) showed me how to dynamically add port forwards to an already existing session.]]></summary></entry></feed>