{"id":494,"date":"2012-07-05T11:34:33","date_gmt":"2012-07-05T03:34:33","guid":{"rendered":"http:\/\/rmohan.com\/?p=494"},"modified":"2012-07-05T11:34:33","modified_gmt":"2012-07-05T03:34:33","slug":"best-ssh-commands-tricks","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=494","title":{"rendered":"Best SSH Commands \/ Tricks"},"content":{"rendered":"<p><strong>\u00a0Best SSH Commands \/ Tricks<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>OpenSSH is a FREE version of the SSH <a id=\"AdBriteInlineAd_connectivity\" name=\"AdBriteInlineAd_connectivity\" target=\"_top\"><\/a>connectivity tools that technical users of the Internet rely on. Users of telnet, rlogin, and ftp may not realize that their <a id=\"AdBriteInlineAd_password\" name=\"AdBriteInlineAd_password\" target=\"_top\"><\/a>password is transmitted across the Internet unencrypted, but it is. OpenSSH encrypts all traffic (including passwords) to effectively eliminate eavesdropping, <a id=\"AdBriteInlineAd_connection\" name=\"AdBriteInlineAd_connection\" target=\"_top\"><\/a>connection hijacking, and other attacks. The encryption that OpenSSH provides has been strong enough to earn the trust of Trend Micro and other providers of cloud computing.Additionally, OpenSSH provides secure tunneling capabilities and several authentication methods, and supports all SSH protocol versions.<\/p>\n<p>1) Copy ssh keys to user@host to enable password-less ssh logins.<\/p>\n<p>ssh-copy-id user@host<\/p>\n<p>To generate the keys use the command ssh-keygen<\/p>\n<p>2) Start a tunnel from some machine\u2019s port 80 to your local post 2001<\/p>\n<p>ssh -N -L2001:localhost:80 somemachine<\/p>\n<p>Now you can acces the website by going to http:\/\/localhost:2001\/<\/p>\n<p>3) Output your microphone to a remote computer\u2019s speaker<\/p>\n<p>dd if=\/dev\/dsp | ssh -c arcfour -C username@host dd of=\/dev\/dsp<\/p>\n<p>This will output the sound from your microphone port to the ssh target computer\u2019s speaker port. The sound quality is very bad, so you will hear a lot of hissing.<\/p>\n<p>4) Compare a remote file with a local file<\/p>\n<p>ssh user@host cat \/path\/to\/remotefile | diff \/path\/to\/localfile &#8211;<\/p>\n<p>Useful for checking if there are differences between local and remote files.<\/p>\n<p>5) Mount folder\/filesystem through SSH<\/p>\n<p>sshfs name@server:\/path\/to\/folder \/path\/to\/mount\/point<\/p>\n<p><a id=\"AdBriteInlineAd_Install\" name=\"AdBriteInlineAd_Install\" target=\"_top\"><\/a>Install SSHFS from http:\/\/fuse.sourceforge.net\/sshfs.html<br \/>\nWill allow you to mount a folder security over a network.<\/p>\n<p>6) SSH connection through host in the middle<\/p>\n<p>ssh -t reachable_host ssh unreachable_host<\/p>\n<p>Unreachable_host is unavailable from local network, but it\u2019s available from reachable_host\u2019s network. This command creates a connection to unreachable_host through \u201chidden\u201d connection to reachable_host.<\/p>\n<p>7) Copy from host1 to host2, through your host<br \/>\nssh root@host1 \u201ccd \/somedir\/tocopy\/ &amp;&amp; tar -cf \u2013 .\u201d | ssh root@host2 \u201ccd \/samedir\/tocopyto\/ &amp;&amp; tar -xf -\u201d<\/p>\n<p>Good if only you have access to host1 and host2, but they have no access to your host (so ncat won\u2019t work) and they have no direct access to each other.<\/p>\n<p>8) Run any GUI program remotely<\/p>\n<p>ssh -fX @<\/p>\n<p>The SSH server configuration requires:<\/p>\n<p>X11Forwarding yes # this is default in Debian<\/p>\n<p>And it\u2019s convenient too:<\/p>\n<p>Compression delayed<\/p>\n<p>9) Create a persistent connection to a machine<\/p>\n<p>ssh -MNf @<\/p>\n<p>Create a persistent SSH connection to the host in the background. Combine this with <a id=\"AdBriteInlineAd_settings\" name=\"AdBriteInlineAd_settings\" target=\"_top\"><\/a>settings in your ~\/.ssh\/config:<br \/>\nHost host<br \/>\nControlPath ~\/.ssh\/master-%r@%h:%p<br \/>\nControlMaster no<br \/>\nAll the SSH <a id=\"AdBriteInlineAd_connections\" name=\"AdBriteInlineAd_connections\" target=\"_top\"><\/a>connections to the machine will then go through the persisten SSH socket. This is very useful if you are using SSH to synchronize files (using rsync\/sftp\/cvs\/svn) on a regular basis because it won\u2019t create a new socket each time to open an ssh connection.<\/p>\n<p>10) Attach screen over ssh<\/p>\n<p>ssh -t remote_host screen -r<\/p>\n<p>Directly attach a remote screen session (saves a useless parent bash process)<\/p>\n<p>11) Port Knocking!<\/p>\n<p>knock 3000 4000 5000 &amp;&amp; ssh -p user@host &amp;&amp; knock 5000 4000 3000<\/p>\n<p>Knock on ports to open a port to a service (ssh for example) and knock again to close the port. You have to <a id=\"AdBriteInlineAd_install\" name=\"AdBriteInlineAd_install\" target=\"_top\"><\/a>install knockd.<br \/>\nSee example config file below.<br \/>\n[options]<br \/>\nlogfile = \/var\/log\/knockd.log<br \/>\n[openSSH]<br \/>\nsequence = 3000,4000,5000<br \/>\nseq_timeout = 5<br \/>\ncommand = \/sbin\/iptables -A INPUT -i eth0 -s %IP% -p tcp \u2013dport 22 -j ACCEPT<br \/>\ntcpflags = syn<br \/>\n[closeSSH]<br \/>\nsequence = 5000,4000,3000<br \/>\nseq_timeout = 5<br \/>\ncommand = \/sbin\/iptables -D INPUT -i eth0 -s %IP% -p tcp \u2013dport 22 -j ACCEPT<br \/>\ntcpflags = syn<\/p>\n<p>12) Remove a line in a text file. Useful to fix<\/p>\n<p>ssh-keygen -R<\/p>\n<p>In this case it\u2019s better do to use the dedicated tool<\/p>\n<p>13) Run complex remote shell cmds over ssh, without escaping quotes<\/p>\n<p>ssh host -l user $(<\/p>\n<p>Much simpler method. More portable version: ssh host -l user \u201c<code>cat cmd.txt<\/code>\u201d<\/p>\n<p>14) Copy a MySQL Database to a new Server via SSH with one command<\/p>\n<p>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<p>Dumps a MySQL database over a compressed SSH tunnel and uses it as input to mysql \u2013 i think that is the fastest and best way to migrate a DB to a new server!<\/p>\n<p>15) Remove a line in a text file. Useful to fix \u201cssh host key change\u201d warnings<\/p>\n<p>sed -i 8d ~\/.ssh\/known_hosts<\/p>\n<p>16) Copy your ssh public key to a server from a machine that doesn\u2019t have ssh-copy-id<\/p>\n<p>cat ~\/.ssh\/id_rsa.pub | ssh user@machine \u201cmkdir ~\/.ssh; cat &gt;&gt; ~\/.ssh\/authorized_keys\u201d<\/p>\n<p>If you use Mac OS X or some other *nix variant that doesn\u2019t come with ssh-copy-id, this one-liner will allow you to add your public key to a remote machine so you can subsequently ssh to that machine without a password.<\/p>\n<p>17) Live ssh network throughput test<\/p>\n<p>yes | pv | ssh $host \u201ccat &gt; \/dev\/null\u201d<\/p>\n<p>connects to host via ssh and displays the live transfer speed, directing all transferred data to \/dev\/null<br \/>\nneeds pv <a id=\"AdBriteInlineAd_installed\" name=\"AdBriteInlineAd_installed\" target=\"_top\"><\/a>installed<br \/>\nDebian: \u2018apt-get install pv\u2019<br \/>\nFedora: \u2018yum install pv\u2019 (may need the \u2018extras\u2019 repository enabled)<\/p>\n<p>18) How to establish a remote Gnu screen session that you can re-connect to<\/p>\n<p>ssh -t user@some.domain.com \/usr\/bin\/screen -xRR<\/p>\n<p>Long before tabbed terminals existed, people have been using Gnu screen to open many shells in a single text terminal. Combined with ssh, it gives you the ability to have many open shells with a single remote connection using the above options. If you detach with \u201cCtrl-a d\u201d or if the ssh session is accidentally terminated, all processes running in your remote shells remain undisturbed, ready for you to reconnect. Other useful screen commands are \u201cCtrl-a c\u201d (open new shell) and \u201cCtrl-a a\u201d (alternate between shells). Read this quick reference for more screen commands: http:\/\/aperiodic.net\/screen\/quick_reference<\/p>\n<p>19) Resume scp of a big file<\/p>\n<p>rsync \u2013partial \u2013progress \u2013rsh=ssh $file_source $user@$host:$destination_file<\/p>\n<p>It can resume a failed secure copy ( usefull when you transfer big files like db dumps through vpn ) using rsync.<br \/>\nIt requires rsync installed in both hosts.<br \/>\nrsync \u2013partial \u2013progress \u2013rsh=ssh $file_source $user@$host:$destination_file local -&gt; remote<br \/>\nor<br \/>\nrsync \u2013partial \u2013progress \u2013rsh=ssh $user@$host:$remote_file $destination_file remote -&gt; local<\/p>\n<p>20) Analyze traffic remotely over ssh w\/ wireshark<\/p>\n<p>ssh root@server.com \u2018tshark -f \u201cport !22? -w -\u2019 | wireshark -k -i &#8211;<\/p>\n<p>This captures traffic on a remote machine with tshark, sends the raw pcap data over the ssh link, and displays it in wireshark. Hitting ctrl+C will stop the capture and unfortunately close your wireshark window. This can be worked-around by passing -c # to tshark to only capture a certain # of packets, or redirecting the data through a named pipe rather than piping directly from ssh to wireshark. I recommend filtering as much as you can in the tshark command to conserve bandwidth. tshark can be replaced with tcpdump thusly:<br \/>\nssh root@example.com tcpdump -w \u2013 \u2018port !22? | wireshark -k -i &#8211;<\/p>\n<p>21) Have an ssh session open forever<\/p>\n<p>autossh -M50000 -t server.example.com \u2018screen -raAd mysession\u2019<\/p>\n<p>Open a ssh session opened forever, great on laptops losing Internet connectivity when switching WIFI spots.<\/p>\n<p>22) Harder, Faster, Stronger SSH clients<\/p>\n<p>ssh -4 -C -c blowfish-cbc<\/p>\n<p>We force IPv4, compress the stream, specify the cypher stream to be Blowfish. I suppose you could use aes256-ctr as well for cypher spec. I\u2019m of course leaving out things like master control sessions and such as that may not be available on your shell although that would speed things up as well.<\/p>\n<p>23) Throttle bandwidth with cstream<\/p>\n<p>tar -cj \/backup | cstream -t 777k | ssh host \u2018tar -xj -C \/backup\u2019<\/p>\n<p>this bzips a folder and transfers it over the network to \u201chost\u201d at 777k bit\/s.<br \/>\ncstream can do a lot more, have a look http:\/\/www.cons.org\/cracauer\/cstream.html#usage<br \/>\nfor example:<br \/>\necho w00t, i\u2019m 733+ | cstream -b1 -t2<\/p>\n<p>24) Transfer SSH public key to another machine in one step<\/p>\n<p>ssh-keygen; ssh-copy-id user@host; ssh user@host<\/p>\n<p>This command sequence allows simple setup of (gasp!) password-less SSH logins. Be careful, as if you already have an SSH keypair in your ~\/.ssh directory on the local machine, there is a possibility ssh-keygen may overwrite them. ssh-copy-id copies the public key to the remote host and appends it to the remote account\u2019s ~\/.ssh\/authorized_keys file. When trying ssh, if you used no passphrase for your key, the remote shell appears soon after invoking ssh user@host.<\/p>\n<p>25) Copy stdin to your X11 buffer<\/p>\n<p>ssh user@host cat \/path\/to\/some\/file | xclip<\/p>\n<p>Have you ever had to scp a file to your work machine in order to copy its contents to a mail? xclip can help you with that. It copies its stdin to the X11 buffer, so all you have to do is middle-click to paste the content of that looong file <img decoding=\"async\" src=\"http:\/\/www.newitperson.com\/wp-includes\/images\/smilies\/icon_smile.gif\" alt=\":)\" \/><\/p>\n<p>Have Fun<\/p>\n<p>Please comment if you have any other good SSH Commands OR Tricks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p> Best SSH Commands \/ Tricks<\/p>\n<p>&nbsp;<\/p>\n<p>OpenSSH is a FREE version of the SSH connectivity tools that technical users of the Internet rely on. Users of telnet, rlogin, and ftp may not realize that their password is transmitted across the Internet unencrypted, but it is. OpenSSH encrypts all traffic (including passwords) to effectively eliminate [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/494"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=494"}],"version-history":[{"count":2,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/494\/revisions"}],"predecessor-version":[{"id":496,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/494\/revisions\/496"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}