The Definitive Guide to Linux SSH Command Line Tools
Secure Shell (SSH) is a powerful protocol used for secure remote administration of Linux and Unix-like systems. This guide covers essential SSH command-line tools, their use cases, and advanced features to help you manage remote systems efficiently.
READ ALSO: Advanced SSH Techniques: A Deep Dive Into Secure Remote Access
Basic SSH Commands
Connecting to a Remote Server
ssh user@remote_hostThis command establishes an SSH connection with the specified remote host. Replace
userwith the actual username andremote_hostwith the IP address or hostname of the remote server.Specifying a Port
ssh -p 2222 user@remote_hostIf the SSH server is running on a non-default port (default is 22), use the
-pflag to specify the correct port.Running a Remote Command
ssh user@remote_host "ls -la"Executes
ls -laon the remote server and returns the output.SSH Key-Based Authentication
ssh-keygen -t rsa -b 4096 ssh-copy-id user@remote_hostGenerating and copying SSH keys allows password-less authentication, improving security and convenience.
File Transfer Over SSH
Copying Files with SCP
scp file.txt user@remote_host:/remote/path/ scp -r directory/ user@remote_host:/remote/path/SCP (Secure Copy Protocol) allows secure file transfer between local and remote machines.
Using SFTP
sftp user@remote_host sftp> put localfile.txt sftp> get remotefile.txtSFTP (SSH File Transfer Protocol) provides an interactive mode for transferring files securely.
SSH Tunneling and Port Forwarding
Local Port Forwarding
ssh -L 8080:localhost:80 user@remote_hostForwards local port 8080 to the remote server’s port 80, useful for accessing web services securely.
Remote Port Forwarding
ssh -R 9090:localhost:22 user@remote_hostAllows remote users to connect to the local SSH service via port 9090 on the remote machine.
Dynamic Port Forwarding
ssh -D 1080 user@remote_hostCreates a SOCKS proxy on port 1080, useful for browsing securely through the SSH tunnel.
Managing SSH Sessions
Keeping SSH Sessions Alive
ssh -o ServerAliveInterval=60 user@remote_hostPrevents SSH connections from timing out due to inactivity.
Using Multiplexing to Speed Up Connections
ssh -M -S ~/.ssh/controlmastersocket -o ControlPersist=10m user@remote_hostAllows multiple SSH connections to the same host without re-authenticating.
Backgrounding an SSH Session
ssh -f user@remote_host -NRuns SSH in the background without executing a command, useful for port forwarding.
Advanced SSH Configuration
Configuring
~/.ssh/configCustomize SSH behavior per host:
Host myserver HostName remote_host User user Port 2222 IdentityFile ~/.ssh/id_rsaThis simplifies SSH connections by allowing
ssh myserverinstead of typing full details each time.Enabling X11 Forwarding
ssh -X user@remote_hostAllows GUI applications to run on a remote server and display on the local machine.
SSH Security Best Practices
- Disable Root Login: Edit
/etc/ssh/sshd_configand setPermitRootLogin no. - Change Default Port: Modify
Port 2222in/etc/ssh/sshd_configto reduce automated attacks. - Use Fail2Ban: Install and configure Fail2Ban to block IPs after multiple failed login attempts.
- Disable Password Authentication: Enforce SSH key authentication by setting
PasswordAuthentication noinsshd_config.
- Disable Root Login: Edit
Troubleshooting SSH Issues
Checking SSH Service Status
systemctl status sshdEnsures the SSH daemon is running.
Debugging SSH Connections
ssh -vvv user@remote_hostEnables verbose output to diagnose connection issues.
Restarting the SSH Service
sudo systemctl restart sshdApplies configuration changes and restarts the SSH service.
Conclusion
Mastering Linux SSH command-line tools enhances system administration efficiency, security, and automation. Whether you need basic connectivity, secure file transfers, or advanced tunneling, SSH provides a robust set of features for managing remote systems effectively.
Discover more from Yum Update
Subscribe to get the latest posts sent to your email.
