]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/ssh-tunnels.html
WIP: toc builder
[ai-pg] / full-docs / src / sgml / html / ssh-tunnels.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>18.11. Secure TCP/IP Connections with SSH Tunnels</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="gssapi-enc.html" title="18.10. Secure TCP/IP Connections with GSSAPI Encryption" /><link rel="next" href="event-log-registration.html" title="18.12. Registering Event Log on Windows" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">18.11. Secure TCP/IP Connections with <span class="application">SSH</span> Tunnels</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="gssapi-enc.html" title="18.10. Secure TCP/IP Connections with GSSAPI Encryption">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="runtime.html" title="Chapter 18. Server Setup and Operation">Up</a></td><th width="60%" align="center">Chapter 18. Server Setup and Operation</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="event-log-registration.html" title="18.12. Registering Event Log on Windows">Next</a></td></tr></table><hr /></div><div class="sect1" id="SSH-TUNNELS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">18.11. Secure TCP/IP Connections with <span class="application">SSH</span> Tunnels <a href="#SSH-TUNNELS" class="id_link">#</a></h2></div></div></div><a id="id-1.6.5.14.2" class="indexterm"></a><p>
3    It is possible to use <span class="application">SSH</span> to encrypt the network
4    connection between clients and a
5    <span class="productname">PostgreSQL</span> server. Done properly, this
6    provides an adequately secure network connection, even for non-SSL-capable
7    clients.
8   </p><p>
9    First make sure that an <span class="application">SSH</span> server is
10    running properly on the same machine as the
11    <span class="productname">PostgreSQL</span> server and that you can log in using
12    <code class="command">ssh</code> as some user;  you then can establish a
13    secure tunnel to the remote server.  A secure tunnel listens on a
14    local port and forwards all traffic to a port on the remote machine.
15    Traffic sent to the remote port can arrive on its
16    <code class="literal">localhost</code> address, or different bind
17    address if desired;  it does not appear as coming from your
18    local machine.  This command creates a secure tunnel from the client
19    machine to the remote machine <code class="literal">foo.com</code>:
20 </p><pre class="programlisting">
21 ssh -L 63333:localhost:5432 joe@foo.com
22 </pre><p>
23    The first number in the <code class="option">-L</code> argument, 63333, is the
24    local port number of the tunnel; it can be any unused port.  (IANA
25    reserves ports 49152 through 65535 for private use.)  The name or IP
26    address after this is the remote bind address you are connecting to,
27    i.e., <code class="literal">localhost</code>, which is the default.  The second
28    number, 5432, is the remote end of the tunnel, e.g., the port number
29    your database server is using.  In order to connect to the database
30    server using this tunnel, you connect to port 63333 on the local
31    machine:
32 </p><pre class="programlisting">
33 psql -h localhost -p 63333 postgres
34 </pre><p>
35    To the database server it will then look as though you are
36    user <code class="literal">joe</code> on host <code class="literal">foo.com</code>
37    connecting to the <code class="literal">localhost</code> bind address, and it
38    will use whatever authentication procedure was configured for
39    connections by that user to that bind address.  Note that the server will not
40    think the connection is SSL-encrypted, since in fact it is not
41    encrypted between the
42    <span class="application">SSH</span> server and the
43    <span class="productname">PostgreSQL</span> server.  This should not pose any
44    extra security risk because they are on the same machine.
45   </p><p>
46    In order for the
47    tunnel setup to succeed you must be allowed to connect via
48    <code class="command">ssh</code> as <code class="literal">joe@foo.com</code>, just
49    as if you had attempted to use <code class="command">ssh</code> to create a
50    terminal session.
51   </p><p>
52    You could also have set up port forwarding as
53 </p><pre class="programlisting">
54 ssh -L 63333:foo.com:5432 joe@foo.com
55 </pre><p>
56    but then the database server will see the connection as coming in
57    on its <code class="literal">foo.com</code> bind address, which is not opened by
58    the default setting <code class="literal">listen_addresses =
59    'localhost'</code>.  This is usually not what you want.
60   </p><p>
61    If you have to <span class="quote">“<span class="quote">hop</span>”</span> to the database server via some
62    login host, one possible setup could look like this:
63 </p><pre class="programlisting">
64 ssh -L 63333:db.foo.com:5432 joe@shell.foo.com
65 </pre><p>
66    Note that this way the connection
67    from <code class="literal">shell.foo.com</code>
68    to <code class="literal">db.foo.com</code> will not be encrypted by the SSH
69    tunnel.
70    SSH offers quite a few configuration possibilities when the network
71    is restricted in various ways.  Please refer to the SSH
72    documentation for details.
73   </p><div class="tip"><h3 class="title">Tip</h3><p>
74     Several other applications exist that can provide secure tunnels using
75     a procedure similar in concept to the one just described.
76    </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="gssapi-enc.html" title="18.10. Secure TCP/IP Connections with GSSAPI Encryption">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="runtime.html" title="Chapter 18. Server Setup and Operation">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="event-log-registration.html" title="18.12. Registering Event Log on Windows">Next</a></td></tr><tr><td width="40%" align="left" valign="top">18.10. Secure TCP/IP Connections with GSSAPI Encryption </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 18.12. Registering <span class="application">Event Log</span> on <span class="systemitem">Windows</span></td></tr></table></div></body></html>