SSH (Secure Shell) is an encrypted network protocol that allows you to securely access and manage your VPS (Virtual Private Server). It provides a secure tunnel for data and commands between your local machine and your VPS, helping prevent snooping or tampering with your connection.
Mastering some key SSH tips and tricks can help make managing your VPS much more efficient. In this guide, we’ll cover some of the most useful SSH tricks for VPS administration, including tips on security, shortcuts, customization, automation, and more.
Top SSH Tricks for VPS Management
Enable Public Key Authentication for Passwordless Logins
Typing your password to login each time quickly becomes tedious. Public key authentication allows passwordless SSH logins for convenience:
- On your local machine, generate an SSH keypair with
ssh-keygen
- Copy your public key to
~/.ssh/authorized_keys
on your VPS - Set proper permissions on
~/.ssh
and its contents - You can now SSH in without entering your password!
Create SSH Aliases and configs for Common Tasks
Manually typing repetitive, long SSH commands gets old fast. Configure your SSH config file (~/.ssh/config
) with aliases, like:
Host myserver HostName 1.2.3.4 User myname IdentityFile ~/.ssh/my_private_key
Now you can simply run ssh myserver
to connect! Create aliases to save typing.
Use SSH Agent for Forwarding Keys Between Servers
Securely hop between servers without copying keys. Start the SSH agent, add your key, and connections will automatically forward keys onwards:
eval $(ssh-agent) ssh-add ~/.ssh/my_private_key ssh server1 # Now you can seamlessly ssh from server1 to server2
No need for insecure key copying!
Also Read: How To Secure And Firewall A VPS Server?
Mount Remote Folders via SSHFS
Locally mount remote folders instead of copying files back and forth. The SSHFS tool uses SSH to provide encrypted transfers, user access rights, and all normal filesystem operations:
sshfs name@server:/path/to/folder /local/mount/point
You can now interact with the remote folder through the local mount point.
Utilize SSH Port Forwarding for Network Access
SSH port forwarding grants secure access to remote LAN resources from your local machine:
- Local port forwarding maps your local ports to ports on the remote server, allowing local access to services running there.
- Remote port forwarding maps remote ports to ports on your local machine, enabling access between the networks.
This eliminates VPN complexity for gaining LAN access.
SSH Productivity Shortcuts
Control Processes with tmux Sessions
Start processes in tmux terminal multiplexer sessions. You can safely detach and reattach later without disrupting long-running tasks.
Common tmux commands:
tmux new -s session_name
: Starts a new sessiontmux attach -t session_name
: Reattach a previous sessiontmux detach
: Safely detaches current session
This persists tasks beyond a single SSH connection.
Quickly Navigate within Command History
Navigate through previously used commands with Ctrl+R and Ctrl+S:
- Ctrl+R: Search backward through history
- Ctrl+S: Search forward through history
Rapidly find and repeat complex commands you’ve already entered!
Jump Between Locations with ssh Menu
The ssh
utility lets you create bookmarks to quickly hop between servers:
ssh -F ~/.ssh/ssh_config server1 # Add bookmarks ssh -F ~/.ssh/ssh_config server2 # Now list and connect through bookmarks ssh -F ~/.ssh/ssh_config menu
No more hunting for IP addresses and usernames!
SSH Customization Tricks
Create Your Own MOTD Banner
Greet yourself with a fun, informative Message of the Day (MOTD) banner when you connect:
- Add messages to
/etc/motd
- Use ANSI escape codes for colors and formatting
- Set update frequency with
/etc/motd.tail
Spruce up your server’s welcome mat!
Enhance Your Shell Prompt
Customize your shell prompt to show useful system info like user, hostname, cwd, time, git repo details, and more:
- For Bash, edit
PS1
andPROMPT_COMMAND
in~/.bashrc
- In Zsh, edit
PS1
and add plugins in~/.zshrc
- Use
\[
and\]
to enclose non-printing escape sequences
Unleash your prompt creativity to boost productivity!
Create Your Own Commands with Aliases
Streamline SSH operations by creating custom terminal command aliases:
- Open
~/.bashrc
or~/.zshrc
- Add alias mappings like
alias update='sudo apt update && sudo apt upgrade'
- Source the file to apply changes in your current session
Now you can run common tasks more easily!
SSH Automation Ideas
Automate Commands with SSH RC Files
RC files (.bashrc, .zshrc) run on every new session – use them to automate tasks:
- Set aliases, functions, variables here to establish your environment
- Change directories, update packages, start services on connect
Reduce repetitive manual operations through automation!
Script Commands with Bash Scripts
Bash scripting allows building complex tools by combining SSH commands:
- Scripts set variables, parse input, allow decision making with conditionals
- Control flow via loops, pipe data between commands
- Ideal for automating workflows requiring decision logic
This takes you beyond simple aliases.
Adopt Configuration Management Tools
Robust configuration management tools like Ansible, Puppet and Chef allow you to completely automate server configuration and management by codifying workflows:
- Automatically install software, configure systems
- Implement entire workflows with templates and modules
- Control fleet of servers at scale
- Built-in idempotence handles errors gracefully
This takes automation to the next level!
Key Takeaways
- Enable public key based passwordless login for better security and convenience
- Create SSH aliases and configs to save on typing for common operations
- Forward SSH keys instead of copying insecurely between servers
- Mount remote folders locally using SSHFS for easy access
- Utilize port forwarding to securely access remote LAN resources
- Boost productivity with tmux terminal multiplexing and command history shortcuts
- Customize your SSH experience with greeting banners, prompts, and aliases
- Automate commands and workflows using RC files, scripts, and configuration management tools
Frequently Asked Questions
What are some good practices for securing SSH?
Some tips: Use key-based authentication instead of passwords, disable root login over SSH, set idle timeout periods before disconnection, configure OS-level firewall rules only allowing from trusted IP ranges.
How can I make working with SSH less tedious?
Leverage SSH Agent to forward keys between servers instead of copying, create aliases/configs for common operations, use SSH port forwarding instead of VPNs for access, mount remote folders locally with SSHFS, utilize tmux and command history shortcuts.
What is the benefit of something like Ansible vs bash scripts?
Ansible and other configuration management tools allow creation of reusable, redistributable modules and templates to implement infrastructure changes and automate workflows across a whole server fleet. They handle errors gracefully and provide reporting/visibility.
How can SSH help improve my productivity?
Customizing your SSH environment with shortcuts, aliases, greeting messages helps you work more efficiently. SSH tools like port forwarding simplify network access. Automating repetitive tasks with scripts, and configuration management frees you to focus on more impactful work.
Conclusion
SSH is a vital tool for securely managing your VPS. Mastering some of the tips covered here – like streamlining access, customizing your environment, and setting up automation – can greatly boost your productivity in administering servers over SSH.
As your VPS infrastructure grows, consider solutions like VpsWala to easily scale up high performing VPS instances. VpsWala offers simple, affordable VPS hosting backed by quality hardware, robust connectivity, and excellent support. Their high-memory VPS plans are ideal for memory intensive applications.
With powerful VPS infrastructure from VpsWala providing the foundation, SSH tools and techniques allow you securely manage your servers with efficiency and ease.