S
(ecure)SH
(hell) is widely used for server connection. Beside singe connection, here are many details we need to know:
- port forwarding
- client options
- server options
Port forwarding allows you to redirect network traffic from one port on a local machine to another port on a remote machine or vice versa.
1. Port Forwarding
Port forwarding is the most common funciton we use. Basically there are three:
- Local port forwarding
- Remote port forwarding
- Dynamic port forwarding
1.1 Local Port Forwarding
Local port forwarding is a feature of SSH that allows you to redirect network traffic from a port on your local machine to a specific destination port on a remote server. It enables you to access services or resources on the remote server as if they were running on your local machine.
ssh -L <local_port>:<destination_host>:<destination_port> <username>@<SSH_server>
In the above command:
Example:
Let's say you want to forward local traffic from port 8080 to a web server running on a remote machine with the IP address 192.168.1.100 on port 80. You can use the following command:
ssh -L 8080:192.168.1.100:80 [email protected]
This command will establish an SSH connection to ssh-server.example.com and forward any traffic received on your local port 8080 to the remote machine's port 80.
1.2 Remote Port Forwarding
Remote port forwarding is a feature of SSH that allows you to redirect network traffic from a port on a remote server to a specific destination port on your local machine or another machine on your local network. It enables you to expose services or resources running on your local machine to the remote server's network.
ssh -R <remote_port>:<destination_host>:<destination_port> <username>@<SSH_server>
In the above command:
Make sure you have SSH access to the server and the necessary permissions to establish port forwarding.
1.3 Dynamic Port Forwarding
Dynamic port forwarding allows you to create a SOCKS proxy on your local machine that routes traffic through an SSH tunnel and exits from the remote server.
ssh -D <local_port> [email protected]
Replace
Once the SSH connection is established, configure your applications (e.g., web browser) to use the SOCKS proxy with the following settings:
SOCKS Host: localhost
SOCKS Port:
1.4 Serer Configuration
In order to use port forwarding, we need to modify the sshd_config file in /etc/ssh/sshd_config
.
AllowAgentForwarding yes
AllowTcpForwarding yes
GatewayPorts yes
2. Client Options
2.1 Options in command line
Here are some additional SSH options that you can use to customize and enhance your SSH connections:
- -C: Enables compression of data during the SSH connection to improve performance over slow network connections. It compresses the data before sending it over the network.
- -F: Specifies an alternative SSH configuration file instead of the default
~/.ssh/config
. This allows you to use a custom configuration file for specific SSH connections. - -N: Prevents executing a remote command when establishing the SSH connection. This option is useful when you only need to set up port forwarding or establish a secure tunnel without running a remote command.
- -T: Disables pseudo-terminal allocation on the remote server. This is useful when you want to run SSH without a terminal, such as for executing SSH commands in scripts.
- -q: Quiet mode. Suppresses most warning and diagnostic messages, making the SSH connection quieter and suitable for scripting purposes.
- -4 and -6: These options force the SSH client to use IPv4 (
-4
) or IPv6 (-6
) addresses, respectively. By default, SSH uses both IPv4 and IPv6. - -L: Specifies local port forwarding, allowing you to forward traffic from a local port on your machine to a remote server.
- -R: Specifies remote port forwarding, allowing you to forward traffic from a remote port on the server to a local machine.
- -D: Specifies dynamic port forwarding, also known as SOCKS proxy. It allows you to create a local SOCKS proxy on your machine, enabling you to route traffic through the SSH tunnel.
- -o: Allows you to specify configuration options directly on the command line. For example, you can use
-o "User=username"
to specify the username to use for the SSH connection. - -v, -vv, -vvv: Increases the verbosity level of the SSH client. Specifying
-v
increases the level of detail in the debug output, while-vv
and-vvv
provide even more detailed debugging information.
These options offer additional control and flexibility when using the SSH client (ssh
) to connect to remote servers. Remember to refer to the SSH manual (man ssh
) for a comprehensive list of options and their descriptions, as well as their specific usage details.
2.2 Client Configure File
These configures could be wrote down to a file.
3. Server Configure
The sshd_config
file is the main configuration file for the OpenSSH server (sshd
). It is typically located in the /etc/ssh/
directory on most Linux-based systems. The sshd_config
file contains various settings that control the behavior and functionality of the SSH server. Here are some key configurations you may find in the sshd_config
file:
- Port: Specifies the port number on which the SSH server listens for incoming connections. The default is port 22, but it can be changed to enhance security.
- PermitRootLogin: Determines whether the root user is allowed to log in directly via SSH. It can be set to values such as
yes
,no
, orprohibit-password
. - PasswordAuthentication: Specifies whether password-based authentication is allowed for SSH connections. It can be set to
yes
orno
. Disabling password authentication and using key-based authentication is generally recommended for improved security. - PubkeyAuthentication: Enables or disables public key authentication. It should be set to
yes
to allow users to authenticate using SSH keys. - AllowUsers/AllowGroups/DenyUsers/DenyGroups: These directives control which users or groups are allowed or denied access to the SSH server. You can specify individual usernames or group names.
- PermitEmptyPasswords: Determines whether empty passwords are allowed for authentication. It should be set to
no
to disallow empty passwords. - ChallengeResponseAuthentication: Enables or disables challenge-response authentication. It should be set to
no
if you want to disable this authentication method. - AllowTcpForwarding/AllowStreamLocalForwarding/AllowAgentForwarding: These options control whether specific types of port forwarding are allowed. They can be set to
yes
orno
to enable or disable forwarding capabilities. - LoginGraceTime: Specifies the time in seconds during which the SSH server allows authentication before closing the connection if the user hasn't successfully logged in. It can help prevent brute-force attacks.
- TCPKeepAlive: Determines whether TCP keep-alive messages are sent to keep SSH connections alive. It should be set to
yes
for most cases.
These are just a few examples of the many configuration options available in the sshd_config
file. It's important to note that modifying the sshd_config
file requires root or administrative privileges. After making changes to the file, you need to restart the SSH server (sshd
) for the new configuration to take effect.
Summary
In this article, we discussed about:
- SSH port forwarding
- Client Options
- Server Configurations