How to configure SSH port forwarding

Source: Internet
Author: User
Ssh's three powerful port forwarding commands: QUOTE: ssh-C-f-N-g-Llisten_port: DST_Host: DST_portuser @ Tunnel_Hostssh-C-f-N-g-Rlisten_port: DST_Host: DST_portuser @ Tunnel_Hostssh-C-f-N-g-Dlisten_po ssh's three powerful port forwarding commands: QUOTE:
Ssh-C-f-N-g-L listen_port: DST_Host: DST_portuser @ Tunnel_Host
Ssh-C-f-N-g-R listen_port: DST_Host: DST_portuser @ Tunnel_Host
Ssh-C-f-N-g-D listen_port user @ Tunnel_Host
-F Fork into background after authentication.
The user/password for background authentication, usually used with-N. you do not need to log on to the remote host. -P port Connect to this port. Server must be on the sameport.
The sshd service port of the logged-on ssd server.
-L port: host: hostport
Forward a port of the local machine (client) to the specified port of the remote specified machine. the working principle is as follows: a socket listening port is allocated on the local machine. Once a connection is established on the port, the connection is forwarded through a secure channel, at the same time, the remote host establishes a connection with the host's hostport. you can specify port forwarding in the configuration file. only the root user can forward the privileged port. description of IPv6 address in another format: port/host/hostport
-R port: host: hostport
Forward a port of the remote host (server) to the specified port of the local host. the working principle is as follows: a socket listening port is assigned to the remote host. Once a connection is established on the port, the connection goes through the secure channel, establish a connection between the local host and the host's hostport. you can specify port forwarding in the configuration file. only the root user can log on to the remote host to forward the privileged port. description of IPv6 address in another format: port/host/hostport
-D port
Specifies a local machine "dynamic'' application port forwarding. the working principle is as follows: a socket listening port is allocated on the local machine. Once a connection is established on the port, the connection is forwarded through a secure channel, based on the application protocol, you can determine where the remote host will be connected. currently, the SOCKS4 protocol is supported and will act as the SOCKS4 server. only the root user can forward the privileged port. you can specify dynamic port forwarding in the configuration file.
-C Enable compression.
Compressed data transmission.
-N Do not execute a shell or command.
Do not execute scripts or commands, usually used with-f.
-G Allow remote hosts to connect to forwarded ports.
In the-L/-R/-D parameter, the remote host is allowed to connect to the forwarding port established. If this parameter is not added, only the local host is allowed to establish a connection. Note: this parameter does not seem to work in practice, see III)
Instance description:
One server provides the ftp service because ftp transmission uses a plaintext password. if you do not use the ssh port, you can use TcpdumpThe command can easily capture plaintext information. Therefore, we need to forward port 21:
(Ftp-server) # ssh-CNfg-R 2121: localhost: 21 root@10.4.2.50
Then log on to the 10.4.2.50 host. we can use netstat-an | grep: 2121 to check whether the port has been listened on.
(10.4.2.50) # ftp localhost21 can log on to the ftp-server, and tcpdump cannot capture valid information.
Port 2121 can be selected as long as it is a port not used on the machine
Here is an example of a gateway that is a little more complex:
Assume that there is A machine on the intranet that provides ftp (linux, port is 2121, known as machine A) and enters through the gateway server (linux, port is8888, known as machine B, currently, a C machine on the Internet needs to access a port (port is21) of the gateway server to access the intranet ftp server. As you can see, this is actually like an ssh-based firewall program. well, let's perform the following operations:
1. Login
# Ssh-CNfg-R 8888: localhost: 2121 root @ B host IP
In this way, we have enabled a 8888-> 2121 Port conversion on Machine B, but since Port 8888 can only listen on the localhost, although we can already use it on Machine B
# Ftp localhost 8888 is used to access the real ftp server, but it still cannot be provided to external machines for access
2. Login machine B
# Ssh-CNfg-L 21: localhost: 8888 root @ localhost
In this way, Port 21-> 8888 on the local machine can be switched to listen for requests from any address.
2 (1 ).
If the C machine is also a linux machine, you can set it as follows:
# Ssh-CNfg-R 21: localhost: 8888 root @ C machine IP
3. Run the ftp command in linux on the C machine, you can also use windows client software to access port 21 of Machine B to connect to the real ftp server in the background (the real port is 2121)
If it is set in 2 (1), the access address is the local IP address.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.