Okay. We have already set up port forwarding on LdapClientHost. Can this port forwarding be used by other machines? For example, can I add LdapClientHost2 to directly connect to port 7001 of LdapClientHost? The answer is no. In mainstream SSH implementations, local port forwarding is bound to the lookback interface, which means that only localhost or 127.0.0.1 can use local port forwarding, the connection initiated by other machines will only get "connection refused. ". Fortunately, SSH also provides the GatewayPorts keyword. We can share this local port forwarding with other machines by specifying it.
Ssh-g-L : : Remote forwarding instance analysis Let's look at the second example. This time, we assume that we cannot use SSH to directly connect to the LDAP server (LdapServertHost) from LdapClientHost due to network or firewall reasons, but reverse connections are allowed. At this time, we naturally choose remote port forwarding.
Its command format is:
ssh -R
:
:
For example, run the following command on the LDAP server (LdapServertHost:
$ ssh -R 7001:localhost:389 LdapClientHost
Figure 3. Remote port forwarding Compared with the local port forwarding, this time the hosts, SSH Server, and SSH Client locations are reversed, but the data streams are still the same. Our applications on LdapClientHost send data to port 7001 on the local machine, while the local SSH Server encrypts the data received by port 7001 and forwards it to the SSH Client of LdapServertHost. The SSH Client decrypts the received data and forwards it to the listening LDAP 389 port. Then, it returns the data returned from LDAP to complete the entire process.
Are you confused? Why is it local forwarding and sometimes remote forwarding? What is the difference between the two?
Comparison and Analysis of local and remote forwarding Good. SSH Server, SSH Client, LdapServertHost, LdapClientHost, local forwarding, and remote forwarding are confusing. Let's analyze the structure. First, SSH port forwarding naturally requires SSH connections, while SSH connections are directed from the SSH Client to the SSH Server. Our applications are also oriented. For example, when we need to connect to the LDAP Server, the LDAP Server is the Server, and the application connection direction is also from the application Client to the application Server. If the two connections have the same direction, we will say that they are local forwarding. If the two directions are inconsistent, we will say that it is remote forwarding.
We can recall the two examples above for comparison.
Local forwarding:
LdapClientHost is both the application Client and the SSH Client. Both connections direct to LdapServertHost (both LDAP Server and SSH Server ).
Remote forwarding:
LdapClientHost is the application Client, but it is an SSH Server. LdapServertHost is the LDAP Server, but it is an SSH Client. In this way, the two connections are in the opposite direction.
Another easy-to-remember method is that the ports on the Server are predefined fixed ports (SSH Server port 22 and LDAP port 389 ), the Client ports are all dynamic ports available for us (for example, port 7001 selected in the preceding example ). If the two ports on the Server are on the same machine and the two ports on the Client are on the other machine, this is the local connection. If the four ports are distributed across the two machines, each machine has a Server port and a Client port, that is, remote connection.
After clarifying the differences between the two, let's take a look at the similarities between the two. In your environment, you can allow LdapClientHost to initiate an SSH connection to LdapServerHost or initiate an SSH connection to LdapClientHost. In this case, we can select local or remote forwarding to achieve the same function.
Next let's take a look at port forwarding in the advanced version. All the connections and forwards we have previously involved involve only two machines. Do you still remember the problem we mentioned in local forwarding? In the local forwarding command And Can it be a different machine?
ssh -L
:
:
The answer is yes! Let's look at an example involving four machines (A, B, C, D.
Figure 4. multi-host forwarding Application Run the following commands on the SSH Client (C) to establish an SSH connection and port forwarding:
$ ssh -g -L 7001::389
Then Configure port 7001 of the Connection Machine (C) on our application client (. Note that the "-g" parameter is specified in the command to ensure that machine (A) can use the local port forwarding established by machine (C. In the preceding connection, the connections between (A) <-> (C) and (B) <-> (D) are not secure connections, they are not encrypted and decrypted through SSH. If the network between them is not a trusted network connection, we need to use this connection method with caution.
Back to Top
Part 3 Analysis of other types of dynamic forwarding instances Well, dynamic forwarding sounds cool. When you see this, have you ever thought about local forwarding and remote forwarding, but the premise is that there is a fixed application server port number, for example, port 389 of the LDAP server in the preceding example. What should I do without this port number? Wait, what kind of application will not have this port number? Well, for example, using a browser for Web browsing, such as MSN.
When we access the Internet in an insecure WiFi environment, it is undoubtedly necessary to use SSH dynamic forwarding to protect our Webpage Browsing and MSN information. Let's take a look at the Command Format of dynamic forwarding:
$ ssh -D
For example:
$ ssh -D 7001
Figure 5. Dynamic port forwarding
It seems very simple. We still chose 7001 as the local port number. In fact, here SSH creates a SOCKS proxy service. Let's take a look at the description of the-D parameter in the help document: -D port This works by allocating a socket to listen to port on the local side, and whenever a connection is made to this port, the con- nection is forwarded over the secure channel, and the applica- tion protocol is then used to determine where to connect to from the remote machine. Currently the SOCKS4 and SOCKS5 protocols are supported, and ssh will act as a SOCKS server. Only root can forward privileged ports. Dynamic port forwardings can also be specified in the configuration file.
The subsequent use is simple. We can directly use localhost: 7001 as a normal SOCKS proxy and directly set it on the browser or MSN. Websites that cannot be accessed on the SSH Client can be browsed normally now. However, it is worth noting that the scope of SSH protection only covers connections from the browser (SSH Client) to the SSH Server, does not include connections from the SSH Server to the target website. If the security of the last half of the connection cannot be fully guaranteed, this method is still not a suitable solution.
Analysis of X protocol forwarding instances Let's take a look at the last example-X protocol forwarding.
In our daily work, we may often remotely log on to Linux, Unix, Solaris, HP, and other machines for development or maintenance, and often need to run some programs in GUI mode, for example, a graphical interface is required to install DB2/WebSphere. At this time, there are usually two options for implementation: VNC or X Window. Let's take a look at the latter.
To use the X Window, you must install X Client and X Server respectively. In this example, X Client is the accessed remote Linux/Unix/Solaris/HP, our X Server is the local machine that initiates access (such as the laptop or desktop computer in front of you ). To display the X Window of X Client on X Server, you must first specify the position of X Server on X Client. The command format is as follows:
export DISPLAY=
:
.
For example:
export DISPLAY=myDesktop:1.0
Then run the X application directly, and the X Window will automatically open on our local end.
Everything works normally, but the IT department suddenly adds a firewall in front of remote Linux/Unix/Solaris/HP. Unfortunately, the X protocol is not in the allowed list. What should I do? Can I only use VNC? No, in fact, you only need to use SSH port forwarding and encrypt the X communication data. (Of course, before using this method, IT is best to consult the relevant IT department to check whether IT complies with the relevant security regulations to avoid illegal operations .)
It is also easy to create a command. You can directly initiate an SSH connection from the Local Machine (X Server) as follows:
$ ssh -X
Figure 5. X forwarding After the connection is established, you can directly run the remote X application. Note that the DISPLAY environment variable will be automatically set after the X-forward is established. Usually it will be set to localhost: 10.0. We do not need or need to modify this environment variable after the connection.
A common scenario is that our local machine is a Windows operating system. In this case, we can select open-source XMing as our XServer, while the SSH Client can choose any one, such as PuTTY, cygwin can both configure access to SSH and create X forwarding.
Summary So far, we have completed the introduction of local port forwarding, remote port forwarding, dynamic port forwarding, and X forwarding. In retrospect, the general idea is to forward TCP connections to the SSH channel to solve data encryption and break through firewall restrictions. For some applications with known port numbers, such as Telnet/LDAP/SMTP, we can use local port forwarding or remote port forwarding to achieve this goal. Dynamic port forwarding can implement SOCKS proxy to encrypt and break the firewall's restrictions on Web browsing. For X applications, X Forwarding is undoubtedly the most suitable. Although each part is just a brief introduction, if we can use these skills flexibly, we believe it will be helpful for our daily life/work.