Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands using ssh from sorted by
Terminal - Commands using ssh - 295 results
diff <(ssh user@host cat /path/to/remotefile) /path/to/localfile
ssh user@host "ffmpeg -f x11grab -r 5 -s 1280x720 -i :0 -f avi -" | ffplay - &>/dev/null
2012-05-01 06:26:49
User: buhrietoe
Functions: ssh
Tags: ssh ffmpeg ffplay
1

Play with the framerate option '-r' to scale back bandwidth usage.

The '-s' option is the captured screan area, not the rescaled size. If you want to rescale add a second '-s' option after '-i :0'. Rescaling smaller will also decrease bandwidth.

ssh user@host "ffmpeg -f x11grab -s 1920x1080 -i :0 -r 1 -t 1 -f mjpeg -" | display
2012-05-01 04:45:15
User: buhrietoe
Functions: ssh
0

More stealthy ffmpeg method. The imagemagick 'import' method causes a system beep.

ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no username@host
2012-04-20 01:54:04
User: dmmst19
Functions: ssh
10

This command will bypass checking the host key of the target server against the local known_hosts file.

When you SSH to a server whose host key does not match the one stored in your local machine's known_hosts file, you'll get a error like " WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!" that indicates a key mismatch. If you know the key has legitimately changed (like the server was reinstalled), a permanent solution is to remove the stored key for that server in known_hosts.

However, there are some occasions where you may not want to make the permanent change. For example, you've done some port-forwarding trickery with ssh -R or ssh -L, and are doing ssh user@localhost to connect over the port-forwarding to some other machine (not actually your localhost). Since this is usually temporary, you probably don't want to change the known_hosts file. This command is useful for those situations.

Credit: Command found at http://linuxcommando.blogspot.com/2008/10/how-to-disable-ssh-host-key-checking.html. Further discussion of how it works is there also.

Note this is a bit different than command #5307 - with that one you will still be prompted to store the unrecognized key, whereas this one won't prompt you for the key at all.

ssh user@remote "cat /path/to/archive.tgz" | tar zxvf -
xloadimage <(ssh USER@HOSTNAME DISPLAY=:0.0 import -window root png:-)
2012-03-02 23:01:44
User: brejktru
Functions: ssh
10

standard image viewers do not seem to be able to open a FIFO file. xloadimage was the first one i've stumbled upon that can handle this.

sshpass -p 't@uyM59bQ' ssh username@server.example.com
2012-02-13 09:51:41
User: djyoda
Functions: ssh
-1

You can use sshpass command to provide password for ssh based login. sshpass is a utility designed for running ssh using the mode referred to as "keyboard-interactive" password authentication, but in non-interactive mode.

ssh -f -N -R 8888:localhost:22 user@somedomain.org
2012-02-08 20:24:38
User: 0disse0
Functions: ssh
Tags: ssh
5

this command from the source server and this follow in the destination server:

ssh user@localhost -p 8888

diff -y <(ssh user@host find /boot|sort) <(find /boot|sort)
2012-01-31 15:04:30
User: knoppix5
Functions: diff find ssh
3

You can compare directories on two different remote hosts as well:

diff -y <(ssh user1@host1 find /boot|sort) <(ssh user2@host2 find /boot|sort)

To avoid password-prompt on remote host just generate the rsa key locally and copy it to remote host:

ssh-keygen -t rsa

then

ssh you@server1 "mkdir .ssh"

then

scp .ssh/id_rsa.pub you@server1:; .ssh/authorized_keys2
ssh user@server.com sudo tcpdump -i eth0 -w - 'port 80'| /Applications/Wireshark.app/Contents/Resources/bin/wireshark -k -i -
ssh-keygen -R $(dig +short host.domain.tld)
2012-01-19 15:08:50
User: atoponce
Functions: dig ssh ssh-keygen
2

Quick shortcut if you know the hostname and want to save yourself one step for looking up the IP address separately.

ssh-keygen -R <thehost>
ssh 10.0.0.4 "gzip -c /tmp/backup.sql" |gunzip > backup.sql
2012-01-06 17:44:06
User: ultips
Functions: gunzip ssh
0

If you have servers on Wide Area Network (WAN), you may experience very long transfer rates due to limited bandwidth and latency.

To speed up you transfers you need to compress the data so you will have less to transfer.

So the solution is to use a compression tools like gzip or bzip or compress before and after the data transfer.

Using ssh "-C" option is not compatible with every ssh version (ssh2 for instance).

ssh root@remote.host "rpm -qa" | xargs yum -y install
ssh user@host "tar -zcf - /path/to/dir" > dir.tar.gz
2011-12-16 05:48:38
User: __
Functions: ssh
Tags: ssh tar gzip
16

This improves on #9892 by compressing the directory on the remote machine so that the amount of data transferred over the network is much smaller. The command uses ssh(1) to get to a remote host, uses tar(1) to archive and compress a remote directory, prints the result to STDOUT, which is written to a local file. In other words, we are archiving and compressing a remote directory to our local box.

ssh user@host "tar -czf - /path/to/dir" > dir.tar.gz
ssh user@host "tar -cf - /path/to/dir" | gzip > dir.tar.gz
2011-12-14 15:54:57
User: atoponce
Functions: gzip ssh
Tags: ssh tar gzip
6

The command uses ssh(1) to get to a remote host, uses tar(1) to archive a remote directory, prints the result to STDOUT, which is piped to gzip(1) to compress to a local file. In other words, we are archiving and compressing a remote directory to our local box.

ssh remotebox tail -f /var/log/remotelog
ssh -t remotebox "tail -f /var/log/remote.log"
2011-12-12 20:24:23
User: zlemini
Functions: ssh
12

This is also handy for taking a look at resource usage of a remote box.

ssh -t remotebox top
cat myfile.mp3 | ssh user@remotemachine "mplayer -cache 8912 -"
2011-10-22 23:06:44
User: sidis405
Functions: cat ssh
0

Play local mp3 file on remote machine's speakers through ssh

cat .ssh/id_rsa.pub | ssh user@server "cat >>.ssh/authorized_keys2"
ssh <user>@<host> 'mkdir -m 700 ~/.ssh; echo ' $(< ~/.ssh/id_rsa.pub) ' >> ~/.ssh/authorized_keys ; chmod 600 ~/.ssh/authorized_keys'
2011-10-03 15:59:43
User: Halki
Functions: chmod echo ssh
Tags: ssh ksh
0

Creates the .ssh directory on the remote host with proper permissions, if it doesnt exist. Appends your public key to authorized_keys, and verifies it has proper permissions. (if it didnt exist it may have been created with undesireable permissions).

*Korn shell syntax, may or may not work with bash

ssh -i /root/.ssh/username\@hostname -p 222 username@hostname
cat ~/.ssh/id_rsa.pub | ssh <REMOTE> "(cat > tmp.pubkey ; mkdir -p .ssh ; touch .ssh/authorized_keys ; sed -i.bak -e '/$(awk '{print $NF}' ~/.ssh/id_rsa.pub)/d' .ssh/authorized_keys; cat tmp.pubkey >> .ssh/authorized_keys; rm tmp.pubkey)"
2011-09-30 07:39:24
User: tamouse
Functions: cat ssh
Tags: ssh awk
4

This one is a bit more robust -- the remote machine may not have an .ssh directory, and it may not have an authorized_keys file, but if it does already, and you want to replace your ssh public key for some reason, this will work in that case as well, without duplicating the entry.

cat file.orig | ssh user1@host1 "ssh user2@host2 \"ssh user3@server3 'cat >file.dest'\""
2011-09-23 12:49:19
User: Cell
Functions: cat ssh
0

pub key in ./ssh/authorized_keys needed because ssh-ed ssh can't ask for the password.