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 - 287 results
ssh -X -l user 192.168.1.25
2013-04-30 13:48:28
User: xmuda
Functions: ssh
-6

One time you logged in, you can prove these method just writing "firefox".

ssh user@remote "tar cfp - /path/to/log/* | gzip" > local.tar.gz
ssh user@host.com "DISPLAY=:0.0 import -window root png:-" | open -a /Applications/Preview.app -f
2013-04-14 23:52:53
User: yuumei
Functions: ssh
0

Takes a screenshot of x11 and pipes it over ssh to the preview application on a mac. Requires imagemagick on the server side.

parallel -j 50 ssh {} "ls" ::: host1 host2 hostn | sort | uniq -c
2013-04-12 11:56:41
User: macoda
Functions: sort ssh uniq
1

parallel can be installed on your central node and can be used to run a command multiple times.

In this example, multiple ssh connections are used to run commands. (-j is the number of jobs to run at the same time). The result can then be piped to commands to perform the "reduce" stage. (sort then uniq in this example).

This example assumes "keyless ssh login" has been set up between the central node and all machines in the cluster.

bashreduce may also do what you want.

sxh () { for i in "${@:2}"; do ssh "$i" "$1"; done ; }
2013-04-12 11:34:02
User: zlemini
Functions: ssh
2

Oneliner to run commands on multiple servers over ssh.

- First parameter "$1" is the command you want to execute remotely.

( It can be multiple commands e.g. "hostname;uptime")

- Second parameter "${@:2}" represents the remote host/s you want to run the command/s on.

ssh -t myserver.org 'sudo ls /etc'
2013-04-09 04:23:37
User: patko
Functions: ls ssh
Tags: ssh sudo
-4

This command will ask for remote sudo password before executing a remote command.

ssh root@172.16.1.99 -i my_openssh_key.ssh -p 9999
2013-03-10 12:37:19
User: motopig
Functions: ssh
-8

use .ssh file to login the server

ssh <remoteuser>@<remoteserver> \ 'mysqldump -u <user> -p<password> <database>' \ | mysql -u <user> -p<password> <database>
2013-03-05 19:20:52
User: kvcrawford
Functions: ssh
Tags: mysql ssh sync
0

There must be no space between -p and the password

ssh user@remote "cat /remote/music/dir/*.mp3" | mpg123 -
2013-02-15 20:03:21
User: erix
Functions: mpg123 ssh
Tags: ssh Linux mpg123
-1

This is just a quick and dirty way to play remote audio files *locally* in your PC.

The best way is to mount the remote Music directory into the local FS, say by using sshfs:

sshfs user@remote:/remote/music/dir/ /mnt/other_pc/

rsync -a -v --stats -e ssh /home root@<newserver>:/root/
ssh username1@servername1 -t ssh username2@servername2 uuencode -m testfile1.tar - | uudecode > testfile1.tar
2013-01-28 17:06:00
User: djt78
Functions: ssh uudecode uuencode
0

cat didn't seem to work with binaries to well for me, the above command seemed to do the trick.

Opps should be pointed out its going through a "hop" box (which is why I was searching here in first place), only need the last bit (after the -t) if doing it directly from one box to another...

ssh -t user@host 'sudo bash -c "ls /var/log && cat /etc/passwd"'
ssh-copy-id [-i [identity_file]] [user@]machine
cat ~/.ssh/id_rsa.pub | ssh user@host 'cat >> ~/.ssh/authorized_keys'
ssh -t HOSTNAME 'tail -f LOGFILE' | while read; do growlnotify -t "TITLE" -m "$REPLY"; done
for host in $HOSTNAMES; do ping -q -c3 $host && ssh $host 'command' & for count in {1..15}; do sleep 1; jobs | wc -l | grep -q ^0\$ && continue; done; kill %1; done &>/dev/null
for host in $MYHOSTS; do ping -q -c3 $H 2>&1 1>/dev/null && ssh -o 'AllowedAuthe ntications publickey' $host 'command1; command2' & for count in 1 2 3 4 5; do sleep 1; jobs | wc -l | grep -q ^0\$ && continue; done; kill %1; done
2012-11-13 23:12:27
User: a8ksh4
Functions: grep host jobs kill ping sleep ssh wc
0

Execute commands serially on a list of hosts. Each ssh connection is made in the background so that if, after five seconds, it hasn't closed, it will be killed and the script will go on to the next system.

Maybe there's an easier way to set a timeout in the ssh options...

diff -u <(ssh -t user@host1 sudo cat /dir1/file1) <(ssh -t user@host2 sudo cat /dir2/file2)
ssh remotehosts;date
2012-11-09 01:14:24
User: kiiwii
Functions: ssh
-3

Run this within a steady screen session.

You can get the approximate time when the remote server went down or other abnormal behavior.

rsync -ayz -e ssh --exclude=/proc --exclude=/sys --exclude=/dev / root@NEWHOST:/MNTDIR
2012-11-06 09:43:42
User: bones
Functions: rsync ssh
7

Copies the complete root-dir of a linux server to another one, where the new harddisks formated and mountet. Very useful to migrate a root-server to another one.

knife ssh name:* -x ec2-user -i ~/.ssh/aws-west.pem "hostname"
2012-11-06 00:14:01
User: rwilson04
Functions: ssh
0

runs the specified ssh command on all chef nodes

ssh-add ~/.ssh/KEY_PAIR_NAME.pem
2012-11-03 02:59:52
User: brockangelo
Functions: ssh ssh-add
Tags: ssh EC2
-1

This command adds your pem key to SSH so that you no longer have to manually specify it when connecting to EC2 instances.

# you can do this:

ssh ec2-instance.amazonaws.com

# instead of this:

ssh -i ~/.ssh/KEY_PAIR_NAME.pem ec2-instance.amazonaws.com

ssh -XfC -c blowfish user@host Xephyr dpms -fullscreen -query localhost :5
2012-11-01 18:59:57
User: hute37
Functions: ssh
Tags: ssh X Xephyr xdmcp
-1

ssh compresion -C option ...

on slow connection VNC performs better but in local LAN native secure X protocol is an option

ssh -t user@remote_host tmux attach
2012-10-12 17:55:50
User: shadow_id
Functions: ssh
Tags: ssh tmux
0

if you use tmux and wish to automatically reattach you previously detached sessions when logging in.