Server
SSH
What is SSH?
Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network. The best known example application is for remote login to computer systems by users.
Install OpenSSH Server
Ubuntu
> sudo apt update
> sudo apt install openssh-server
Configuration
> sudo nano /etc/ssh/sshd_config
Service Management
> sudo systemctl start ssh
> sudo systemctl stop ssh
> sudo systemctl restart ssh
> sudo systemctl status ssh
SSH Tunnel
To use an Ubuntu server as a bridge for SSH to access the target server (which accepts SSH only from the office server), you can set up an SSH tunnel or proxy. Here’s how:
Run the following command on your local machine:
> ssh -L <local_port>:<target_server_ip>:22 <bridge_server_username>@<bridge_server_ip> -p <bridge_server_port>
- -L: Create a local port forwarding.
Then, you can SSH to the target server using the following command:
> ssh -p <local_port> <target_server_username>@localhost
If you need to run it in the background:
> ssh -f -L <local_port>:<target_server_ip>:22 <bridge_server_username>@<bridge_server_ip> -p <bridge_server_port>
- -f: Runs in the background.
If you need to run it and prevent executing commands, keeping only the tunnel:
> ssh -N -L <local_port>:<target_server_ip>:22 <bridge_server_username>@<bridge_server_ip> -p <bridge_server_port>
- -N: Prevents executing commands, keeping only the tunnel.
Auto SSH Tunnel
To automatically establish an SSH tunnel, you can install autossh
:
Linux
> sudo apt install autossh
macOS
> brew install autossh
Then, run the following command:
> autossh -M 0 -f -N -L <local_port>:<target_server_ip>:22 <bridge_server_username>@<bridge_server_ip> -p <bridge_server_port>