Secure Shell (SSH) is a powerful tool for remote administration and secure communication. While basic SSH commands allow remote access and file transfer, advanced techniques can enhance security, automate tasks, and optimize performance. In this guide, we’ll explore some of the most powerful advanced SSH techniques.
READ ALSO: The Definitive Guide to Linux SSH Command Line Tools
SSH keys provide a more secure alternative to password authentication by using cryptographic keys instead of plaintext passwords.
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh-copy-id user@remote_host
/etc/ssh/sshd_config) on the remote server: PasswordAuthentication no
Restart SSH to apply changes:
sudo systemctl restart ssh
Agent forwarding allows you to use your local SSH keys to authenticate on multiple remote systems without storing the key on intermediate servers.
Enable agent forwarding with:
ssh -A user@remote_host
To configure agent forwarding permanently, edit ~/.ssh/config:
Host remote_host
ForwardAgent yes
This prevents needing to store your private key on intermediary servers.
SSH multiplexing allows multiple SSH sessions to reuse a single TCP connection, reducing authentication overhead.
Edit or create ~/.ssh/config and add:
Host *
ControlMaster auto
ControlPath ~/.ssh/sockets/%r@%h-%p
ControlPersist 10m
Create the sockets directory:
mkdir -p ~/.ssh/sockets
Now, subsequent SSH sessions to the same host will connect instantly.
SSH can tunnel traffic securely between local and remote hosts.
Redirects a local port to a remote service:
ssh -L local_port:target_host:target_port user@remote_host
Example: Forward local port 8080 to a remote web server at example.com:80:
ssh -L 8080:example.com:80 user@remote_host
Now, accessing localhost:8080 will reach example.com via the SSH tunnel.
Allows a remote system to access a local service:
ssh -R remote_port:local_host:local_port user@remote_host
Example: Make a local web server available remotely:
ssh -R 9000:localhost:80 user@remote_host
Users on remote_host can now access your local web service via localhost:9000.
Set up a SOCKS proxy over SSH:
ssh -D 8080 user@remote_host
Configure your web browser to use localhost:8080 as a SOCKS5 proxy for encrypted browsing.
When connecting to remote hosts behind a bastion (jump) server, you can simplify access using ProxyJump.
ssh -J user@bastion user@target_host
To make this permanent, add to ~/.ssh/config:
Host target_host
ProxyJump user@bastion
Now, simply run:
ssh target_host
Host target_host
ProxyCommand ssh user@bastion nc %h %p
tmux or screenWhen running long processes over SSH, network disruptions can be frustrating. Tools like tmux and screen allow persistent sessions.
tmuxStart a session:
tmux new -s mysession
Detach from a session:
Ctrl-b d
Reattach later:
tmux attach -t mysession
screenStart a session:
screen -S mysession
Detach:
Ctrl-a d
Reconnect:
screen -r mysession
Edit /etc/ssh/sshd_config:
PermitRootLogin no
Restart SSH:
sudo systemctl restart ssh
Install Fail2Ban:
sudo apt install fail2ban -y
Enable protection for SSH:
sudo systemctl enable fail2ban
Limit SSH to specific IPs by modifying /etc/hosts.allow:
sshd: 192.168.1.100
And deny others in /etc/hosts.deny:
sshd: ALL
SSH can be automated using scripts or tools like sshpass and expect.
sshpass for Password Authentication in Scriptssshpass -p 'password' ssh user@remote_host
expect for Interactive SSH SessionsCreate an expect script:
#!/usr/bin/expect spawn ssh user@remote_host expect "password:" send "your_password\r" interact
Make it executable:
chmod +x ssh_login.exp
Run the script:
./ssh_login.exp
Mount a remote directory locally using SSHFS:
sshfs user@remote_host:/remote/path /local/mountpoint
To unmount:
fusermount -u /local/mountpoint
By leveraging these advanced SSH techniques, you can significantly improve security, optimize workflows, and automate remote administration. Whether setting up SSH multiplexing, tunneling traffic, or using agent forwarding, these tools empower you to make SSH an integral part of your system administration toolkit.
The Definitive Guide to Linux SSH Command Line Tools Secure Shell (SSH) is a powerful…
Introduction The DNS Cluster feature set is flexible to allow for an array of configurations.…
Welcome to WordPress. This is your first post. Edit or delete it, then start writing!
This website uses cookies.