{"id":1604,"date":"2023-05-18T09:13:00","date_gmt":"2023-05-18T09:13:00","guid":{"rendered":"https:\/\/www.prolimehost.com\/blogs\/?p=1604"},"modified":"2023-05-18T16:15:43","modified_gmt":"2023-05-18T16:15:43","slug":"top-ten-ssh-commands-for-linux","status":"publish","type":"post","link":"https:\/\/www.prolimehost.com\/blogs\/top-ten-ssh-commands-for-linux\/","title":{"rendered":"Top Ten SSH Commands for Linux"},"content":{"rendered":"

\"SSHSSH is another of the tech industry\u2019s acronyms. It stands for Secure SHell. What is it and why is it important?<\/p>\n

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.<\/p>\n

Here are ten of the top SSH commands for Linux<\/strong><\/p>\n

1. Let\u2019s say you want to compare files<\/strong><\/p>\n

For example, let\u2019s say you want to compare a local file with a remote file \u2013 to determine if there are any variances between the two. You could use: ssh user@host cat \/path\/to\/remotefile | diff \/path\/to\/localfile \u2013<\/p>\n

2. Perhaps you need to create a persistent SSH connection in the background to the host. <\/strong><\/p>\n

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

~\/.ssh\/config:
\nHost host
\nControlPath ~\/.ssh\/master-%r@%h:%p
\nControlMaster no<\/p>\n

Let\u2019s 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.<\/p>\n

3. Need to copy a MySQL database to a new server? <\/strong><\/p>\n

Copying a MySQL database to a new server can be accomplished with one command in SSH:<\/p>\n

mysqldump \u2013add-drop-table \u2013extended-insert \u2013force \u2013log-error=error.log -uUSER -pPASS OLD_DB_NAME | ssh -C user@newhost \u201cmysql -uUSER -pPASS NEW_DB_NAME\u201d<\/p>\n

What this does is dumps a MySQL database via an encrypted SSH tunnel, using it as input to mysql.<\/p>\n

4. Just in case you want to keep an SSH session open forever, try:<\/strong><\/p>\n

autossh -M50000 -t server.example.com \u2018screen -raAd mysession\u2019<\/p>\n

When would you ever need this? Possibly if you lose Internet connectivity on your laptop when switching between WiFi spots?<\/p>\n

5. Need to transfer SSH public key elsewhere? Try:<\/strong><\/p>\n

ssh-keygen; ssh-copy-id user@host; ssh user@host<\/p>\n

This command sequence allows simple setup of password-less SSH logins.<\/p>\n

6. Need to initiate a tunnel from one port to another?<\/strong><\/p>\n

For example, to start a tunnel from some machine\u2019s port 80 to your local port 2001:<\/p>\n

ssh -N -L2001:localhost:80 somemachine<\/p>\n

This gives you access to the website by going to https:\/\/localhost:2001\/<\/p>\n

7. Port Knocking!<\/strong><\/p>\n

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

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.<\/p>\n

See example config file below.
\n[options]
\nlogfile = \/var\/log\/knockd.log
\n[openSSH]
\nsequence = 3000,4000,5000
\nseq_timeout = 5
\ncommand = \/sbin\/iptables -A INPUT -i eth0 -s %IP% -p tcp \u2013dport 22 -j ACCEPT
\ntcpflags = syn
\n[closeSSH]
\nsequence = 5000,4000,3000
\nseq_timeout = 5
\ncommand = \/sbin\/iptables -D INPUT -i eth0 -s %IP% -p tcp \u2013dport 22 -j ACCEPT
\ntcpflags = syn<\/p>\n

8. Mount folder\/filesystem through SSH<\/strong><\/p>\n

sshfs name@server:\/path\/to\/folder \/path\/to\/mount\/point<\/p>\n

Install SSHFS from https:\/\/fuse.sourceforge.net\/sshfs.html
\nWill allow you to mount a folder security over a network.<\/p>\n

9. Run complex remote shell cmds over ssh, without escaping quotes<\/strong><\/p>\n

ssh host -l user $(<cmd.txt)<\/p>\n

10. Resume scp of a big file<\/strong><\/p>\n

rsync \u2013partial \u2013progress \u2013rsh=ssh $file_source $user@$host:$destination_file<\/p>\n

It can resume a failed secure copy (Useful when you transfer big files like DB dumps through VPN ) using rsync.
\nIt requires rsync installed in both hosts.
\nrsync \u2013partial \u2013progress \u2013rsh=ssh $file_source $user@$host:$destination_file local -> remote
\nor
\nrsync \u2013partial \u2013progress \u2013rsh=ssh $user@$host:$remote_file $destination_file remote -> local<\/p>\n

\n
\n

Table of Contents<\/p>\nToggle<\/span><\/path><\/svg><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n