Top Ten SSH Commands for Linux

SSH for LinuxSSH is another of the tech industry’s acronyms. It stands for Secure SHell. What is it and why is it important?

SSH was created to enable enhanced security when users were attempting to connect remotely to another computer. It does that by encrypting the session between the two. It also facilitates improved authentication facilities.

Here are ten of the top SSH commands for Linux

1. Let’s say you want to compare files

For example, let’s say you want to compare a local file with a remote file – to determine if there are any variances between the two. You could use: ssh user@host cat /path/to/remotefile | diff /path/to/localfile –

2. Perhaps you need to create a persistent SSH connection in the background to the host.

First create a persistent connection: ssh -MNf <user>@<host>, then:

~/.ssh/config:
Host host
ControlPath ~/.ssh/master-%r@%h:%p
ControlMaster no

Let’s say that your goal is to synchronize files using SSH using rsync/sftp/cvs/scn on a consistent basis. Using this command will cause all the SSH connections to route through the persistent SSH socket.

3. Need to copy a MySQL database to a new server?

Copying a MySQL database to a new server can be accomplished with one command in SSH:

mysqldump –add-drop-table –extended-insert –force –log-error=error.log -uUSER -pPASS OLD_DB_NAME | ssh -C user@newhost “mysql -uUSER -pPASS NEW_DB_NAME”

What this does is dumps a MySQL database via an encrypted SSH tunnel, using it as input to mysql.

4. Just in case you want to keep an SSH session open forever, try:

autossh -M50000 -t server.example.com ‘screen -raAd mysession’

When would you ever need this? Possibly if you lose Internet connectivity on your laptop when switching between WiFi spots?

5. Need to transfer SSH public key elsewhere? Try:

ssh-keygen; ssh-copy-id user@host; ssh user@host

This command sequence allows simple setup of password-less SSH logins.

6. Need to initiate a tunnel from one port to another?

For example, to start a tunnel from some machine’s port 80 to your local port 2001:

ssh -N -L2001:localhost:80 somemachine

This gives you access to the website by going to https://localhost:2001/

7. Port Knocking!

knock <host> 3000 4000 5000 && ssh -p <port> user@host && knock <host> 5000 4000 3000

Knock on ports to open a port to a service (SSH for example) and knock again to close the port. You have to install knockd.

See example config file below.
[options]
logfile = /var/log/knockd.log
[openSSH]
sequence = 3000,4000,5000
seq_timeout = 5
command = /sbin/iptables -A INPUT -i eth0 -s %IP% -p tcp –dport 22 -j ACCEPT
tcpflags = syn
[closeSSH]
sequence = 5000,4000,3000
seq_timeout = 5
command = /sbin/iptables -D INPUT -i eth0 -s %IP% -p tcp –dport 22 -j ACCEPT
tcpflags = syn

8. Mount folder/filesystem through SSH

sshfs name@server:/path/to/folder /path/to/mount/point

Install SSHFS from https://fuse.sourceforge.net/sshfs.html
Will allow you to mount a folder security over a network.

9. Run complex remote shell cmds over ssh, without escaping quotes

ssh host -l user $(<cmd.txt)

10. Resume scp of a big file

rsync –partial –progress –rsh=ssh $file_source $user@$host:$destination_file

It can resume a failed secure copy (Useful when you transfer big files like DB dumps through VPN ) using rsync.
It requires rsync installed in both hosts.
rsync –partial –progress –rsh=ssh $file_source $user@$host:$destination_file local -> remote
or
rsync –partial –progress –rsh=ssh $user@$host:$remote_file $destination_file remote -> local

BROUGHT TO YOU BY PROLIMEHOST

We’ve been in the web hosting industry for over a decade, helping hundreds of clients succeed in what they do best and that’s running their business. We specialize in Virtual Private Servers (VPS) and dedicated servers, with data centers in Los Angeles, Denver & Singapore.

VPS SERVICES: LIGHTNING FAST SSD VIRTUAL SERVERS

Our Virtual Private Servers all feature high performance Xeon processors and SSD storage in a RAID10 configuration to optimize your server’s performance, which dramatically enhances visitor experiences on your site.

That speed is backed by unparalleled 24/7 support, featuring both outstanding response AND resolution times to maximize your uptime.

Now is the time to join the ProlimeHost virtual private server revolution.

DEDICATED SERVERS: BACKED BY A 99.9% SLA NETWORK UPTIME GUARANTEE

We only use enterprise-class hardware in our dedicated servers and offer a four (4) hour hardware replacement. Throw in IPMI for remote management, support for public and private networks, free operating system (OS) re-installs, and SATA, SAS & SSD (including NVMe) storage. Call 1-877-477-9454 or contact us. For everything from gaming servers to cheap dedicated servers, we’re here to help.

Steve
Latest posts by Steve (see all)

Leave a Reply