SSH Command Examples - Secure Remote Access Guide

Explore essential SSH command examples for secure remote access, file transfer, and tunneling. Learn to connect, execute commands, and manage SSH connections effectively.

SSH Command Examples

This page provides a comprehensive collection of practical SSH (Secure Shell) command examples for various remote access and management tasks. SSH is a fundamental tool for secure communication over unsecured networks, enabling tasks like remote login, command execution, and secure file transfer.

Securely Connect with SSH

Learn how to establish secure connections to remote servers using different authentication methods and configurations.

# SSH in via PEM file, which normally needs 0600 permissions.
ssh -i /path/to/file.pem user@example.com

# Connect through a non-standard port. It's recommended not to use the default
# port of 22, as it is so often targeted, due to it being so commonplace.
ssh -p 2222 user@example.com

# Connect and forward the authentication agent.
ssh -A user@example.com

# Explicitly specify a key for connection. Useful if you have too many
# authentication failures for a given username.
ssh -i some_id_rsa -o IdentitiesOnly=yes them@there:/path/

# Temporarily disable `pubkey` authentication for this instance.
ssh -o PubkeyAuthentication=no username@hostname.com

Execute Remote Commands and Sessions

Discover how to run commands directly on a remote server or establish interactive shell sessions.

# Execute a command on a remote server.
ssh -t user@example.com 'the-remote-command'

# Tunnel an X session over SSH, via X11 Forwarding.
ssh -X user@example.com

# Launch a specific X application over SSH.
ssh -X -t user@example.com 'chromium-browser'

SSH Tunneling and Port Forwarding

Understand how to create secure tunnels for redirecting network traffic and accessing services securely.

# Redirect traffic with a tunnel between local host (port 8080) and a remote
# host (remote.example.com:5000) through a proxy (personal.server.com).
ssh -f -L 8080:remote.example.com:5000 user@personal.server.com -N

# Create a SOCKS proxy on localhost and port 9999.
ssh -D 9999 user@example.com

Advanced SSH Configurations

Explore more advanced SSH options, including compression, encryption, and file transfer methods.

# Connect to server, but allow for X11 forwarding, while also using Gzip
# compression (can be much faster; YMMV), and using the `blowfish` encryption.
# For more information, see: http://unix.stackexchange.com/q/12755/44856
ssh -XCc blowfish user@example.com

# Copy files and directories, via SSH, from remote host to the current working
# directory, with Gzip compression. An option for when `rsync` isn't available.
#
# This works by creating (not temporary!) a remote Tar archive, then piping its
# output to a local Tar process, which then extracts it to STDOUT.
ssh user@example.com 'tar -C /var/www/Shared/ zcf - asset1 asset2' | tar zxf -

# Mount a remote directory or filesystem, through SSH, to a local mount point.
# Install SSHFS from: https://github.com/libfuse/sshfs
sshfs name@server:/path/to/folder /path/to/mount/point

SSH Escape Sequences and Emacs Integration

Learn about SSH escape sequences for managing sessions and how Emacs can interact with remote files.

# Get help for SSH escape sequences. Useful for terminating unresponsive
# sessions. The default escape character is ~ (tilde), escapes are only
# recognized immediately after a newline.
$ <Enter>~?

# EMACS can read files through SSH. Below, is a link to related documentation.
#
#   http://www.gnu.org/software/emacs/manual/html_node/emacs/Remote-Files.html
#
emacs /ssh:name@server:/path/to/file

Further Resources