Linux System AdministrationSSH & Remote Access

Mastering SSH in Linux: Essential Commands and Server Configuration Tips

Secure Shell (SSH) remains the backbone for secure remote management of Linux servers across enterprises and personal projects alike. Whether you’re administering hundreds of production hosts or managing your home lab, understanding and mastering SSH commands and configurations is critical. In this article, I’ll take you through the most common SSH command usages, practical scenarios where they truly shine, and key server-side configurations to harden your SSH access on Linux systems. Drawing from 15+ years managing Debian, Ubuntu, RHEL, CentOS, and Arch servers, I’ll share real-world insights and best practices that enhance both your productivity and your server’s security.

Connecting to Remote Linux Hosts via SSH

When working with remote Linux servers, secure communication is non-negotiable. While there are protocols like telnet or FTP, these transmit data unencrypted, exposing your credentials and commands to potential eavesdroppers. SSH fills this gap by encrypting all traffic, making remote sessions private and secure.

On nearly every Linux distribution you’ll encounter, the ssh command-line client is pre-installed, giving you secure terminal access to remote machines.

ssh -l root 192.168.19.130

root@192.168.19.130's password: 
Welcome to Ubuntu 20.04 LTS (GNU/Linux 5.4.0-42-generic x86_64)

Last login: Thu Apr 4 12:03:21 2024 from 192.168.19.1
root@server:~# hostname
server
root@server:~# exit
logout
Connection to 192.168.19.130 closed.

This command connects to the host at IP 192.168.19.130 with user root. The -l flag specifies the username string (an alternative is ssh root@192.168.19.130). After successful authentication, you get a shell on the remote machine where you can execute commands as if you were locally logged in. I’ve found explicitly specifying the user useful when switching between multiple accounts or roles on shared infrastructure.

Be mindful: password-based authentication leads to repeated prompts for each session. On production servers I manage, I strongly recommend setting up SSH key-based passwordless authentication for smoother and secure access.

Running Commands Remotely Without a Persistent Session

Sometimes, you don’t need a full interactive SSH session — you just want to execute a quick command or a set of commands on the remote host and get the output back immediately. This is particularly useful for scripts or automation workflows.

ssh -l root 192.168.19.130 hostname

server

This runs hostname remotely and returns the output before closing the connection. To run multiple commands, simply enclose them in single quotes and separate with semicolons:

ssh -l root 192.168.19.130 'hostname; pwd'

server
/root

This is excellent for ad-hoc status checks or for invoking maintenance scripts remotely. From experience, wrapping commands in quotes prevents shell misinterpretation and ensures commands execute exactly as intended.

Executing Local Scripts on Remote Linux Machines

In practice, managing multiple remote commands can become cumbersome. A neat trick is to execute a local shell script remotely, piping it over SSH. This lets you maintain complex tasks locally while running them on remote servers.

cat script.sh

hostname
pwd

The simple script above prints the hostname and current directory. To run it on a remote host:

ssh root@192.168.19.130 'bash -s' < ./script.sh

server
/root

The -s tells bash to read the script from standard input. This method is helpful for applying configuration changes or gathering multi-step diagnostics without manually copying files first.

Secure File Transfers: Copying Files with SCP

Frequently, you will want to transfer files between your local machine and remote servers securely. The scp command uses SSH to provide encrypted file copying, which is critical when managing sensitive configurations or scripts.

scp script.sh root@192.168.19.130:/tmp

script.sh                                                                                                100%  102   123.4KB/s   00:00

After transferring, you can verify on the remote host via SSH:

ssh root@192.168.19.130 'ls /tmp/script.sh'

/tmp/script.sh

Use the -r option with scp to recursively copy entire directories. In a production environment where configuration files need to be synced rapidly and securely, scp is a simple yet reliable tool. However, for frequent and incremental copies, consider tools like rsync for efficiency.

Boosting Performance: Enabling SSH Compression

Network latency or bandwidth constraints can severely affect remote operations. SSH can compress the data stream to speed up transfers on slower links using the -C option.

ssh -C -l root 192.168.19.130 hostname

server

This enables gzip compression on the connection, which can be particularly handy when connecting over low-bandwidth links or performing heavy file transfers. From my experience, enabling compression is a quick win to improve responsiveness without additional setup.

Magically Debug SSH Connection Issues with Verbose Mode

SSH connections sometimes fail due to misconfigurations, network issues, or authentication problems. When things go south, adding verbosity can reveal what’s happening behind the scenes.

ssh -v -l root 192.168.19.130 hostname

OpenSSH_8.4p1, OpenSSL 1.1.1h  22 Sep 2021
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to 192.168.19.130 [192.168.19.130] port 22.
debug1: Connection established.
debug1: Authenticating to 192.168.19.130:22 as 'root'
debug1: Authentication succeeded (password).
server

You can increase verbosity with -vv or -vvv for deeper diagnostics. I often use -vvv when troubleshooting stubborn connection failures, key exchange issues, or obscure permission denials in the field.

Useful SSH Client Escape Sequences

Occasionally, especially with flaky networks, you might encounter hung or frozen SSH sessions. SSH clients support escape sequences to manage these sessions.

Suspending an SSH Session

ssh -l root 192.168.19.130

root@server:~# hostname
server

~^Z
[1]+  Stopped                 ssh -l root 192.168.19.130
$ jobs

[1]+  Stopped                 ssh -l root 192.168.19.130
$ fg %1

ssh -l root 192.168.19.130
root@server:~#

Typing the tilde ~ followed immediately by Ctrl+Z suspends the SSH session and drops you back to your local shell, letting you perform local tasks without terminating the remote session. The fg %1 command resumes the session. I use this to juggle between local and remote work without repeated logins.

Terminating a Frozen SSH Connection

ssh -l root 192.168.19.130

~.
Connection to 192.168.19.130 closed.

If your SSH client appears frozen due to network drops or server issues, typing ~. immediately closes the connection gracefully. This saves you from killing terminal windows or waiting indefinitely.

Listing All Escape Sequences

~?

Supported escape sequences:
~.  - terminate connection
~^Z  - suspend ssh
~#  - list forwarded connections
~&  - background ssh
~?  - print this help

This helps you discover handy key combos to control your SSH session, an often overlooked feature that can improve your daily workflow.

Configuring SSH Server for Security and Practicality

As an experienced sysadmin, I can’t stress enough how crucial securing the SSH daemon is to protect your Linux servers from unauthorized access and brute-force attempts. The configuration lives in /etc/ssh/sshd_config, and here are some practical tweaks I deploy after every fresh server setup.

Display a Custom SSH Login Banner

A banner warns unauthorized users that the server is monitored — a small psychological deterrent and often a legal requirement. First, create a banner file with your custom message:

cat /etc/banner.txt

*********************************************************************
Warning !!! You are trying to log in to techmint.com's server.
All the activities on this server are monitored.
Terminate the session immediately if you are not an authorized user.
*********************************************************************

Then, edit /etc/ssh/sshd_config and add:

Banner /etc/banner.txt

Reload SSH to apply the changes:

systemctl restart sshd

From then on, users see this warning before authentication. This has saved me headaches by setting clear policy expectations upfront on production servers.

Disable Root Logins for Better Security

Allowing direct root login over SSH is a common security risk. Instead, you should set up sudo access for your user accounts and disable root login.

/etc/ssh/sshd_config (excerpt)

PermitRootLogin no

After editing, reload the service again:

systemctl restart sshd

This forces attackers to guess usernames before attempting a password, significantly reducing brute force risks. I’ve seen countless intrusion attempts thwarted by this simple setting.

Change the Default SSH Port

Changing the default SSH port (22) to a high-numbered, unused port diminishes automated attack attempts. For example, switch SSH to port 8088 by modifying the configuration:

/etc/ssh/sshd_config (excerpt)

Port 8088

After reloading sshd, remember to specify the custom port while connecting:

ssh -p 8088 -l root 192.168.19.130

root@server:~#

Keep in mind updating firewall rules and SELinux contexts accordingly. This change alone won’t secure your server but acts as a useful layer in defense-in-depth.

Conclusion

Mastering SSH commands and configurations is a foundational skill for any Linux administrator. Understanding when to open an interactive shell, when to execute commands remotely, or how to transfer files securely streamlines your workflow and automation potential. Equally vital is configuring your SSH server securely to minimize risks — applying best practices such as disabling root login, using custom ports, and displaying login banners can save time and headaches in the long term. Over my years managing various Linux distros in production, I can say investing time in mastering SSH is one of the highest ROI tasks a sysadmin can undertake.

Leave a Reply

Your email address will not be published. Required fields are marked *