SSH is the bedrock of remote Linux administration, but simply logging in and typing commands is like using a hammer to build a skyscraper.
Let’s see SSH in action, not with abstract descriptions, but with a real-world scenario. Imagine you’re deploying a new web server. You’ve got your code ready, but you need to get it onto the remote machine, start a service, and monitor its logs.
First, the SSH connection itself. It’s not just a pipe; it’s an encrypted tunnel.
ssh -i ~/.ssh/my_aws_key.pem ubuntu@ec2-12-34-56-78.compute-1.amazonaws.com
Here, -i specifies your private key for authentication. ubuntu is the username, and the long string is the public IP address of your EC2 instance. Once connected, you’re dropped into a shell. But what if your connection drops? All your work is lost. This is where tmux and screen come in. They are terminal multiplexers, allowing you to run multiple virtual terminals within a single SSH session and detach from them, preserving your work even if the connection breaks.
Consider tmux. You start a new session:
tmux new-session -s deploy_project
This creates a session named deploy_project. Now, you can split your terminal into panes and run different commands in each.
# Split vertically
Ctrl+b %
# Split horizontally
Ctrl+b "
You can navigate between panes using Ctrl+b followed by an arrow key. To detach from the session without killing it, you press Ctrl+b d. The session keeps running on the server. Later, you can reattach to it:
tmux attach-session -t deploy_project
screen offers similar functionality. You’d start a session with screen -S deploy_project, detach with Ctrl+a d, and reattach with screen -r deploy_project.
The real power comes from combining these. You SSH into a server, start a tmux session, begin a long-running compilation or deployment process in one pane, and then tail -f a log file in another. If your laptop battery dies, or you need to switch networks, the processes on the server continue uninterrupted. You simply SSH back in and tmux attach.
This isn’t just about convenience; it’s about resilience and efficiency. You can have multiple projects open, each in its own tmux window, all managed within a single SSH connection.
The difference in how tmux and screen handle input and output is subtle but can be significant for advanced scripting. For instance, tmux has a more granular control over pane resizing and window management, allowing for more complex layouts. screen’s command key, Ctrl+a, is often easier to remember for beginners, but tmux’s Ctrl+b is also widely adopted.
A common, often overlooked, benefit is the ability to share sessions. With tmux or screen, you can allow another user to attach to your session, enabling real-time pair programming or collaborative troubleshooting without needing to grant direct shell access or rely on screen-sharing software. You simply invite them to attach to your session.
The next step in mastering remote administration is understanding how to automate these SSH and multiplexer interactions, perhaps with tools like Ansible or Fabric.