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 - 284 results
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
-2

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.

cat .ssh/id_dsa.pub | ssh <HOST> "mkdir -p .ssh && tee -a .ssh/authorized_keys"
ssh login@server "cat path/filename.mp3" | mplayer -
sshpass -p 'sshpssword' ssh -t <sshuser>@<remotehost> "echo <sudopassword> | sudo -S <command>"
2012-09-13 20:27:13
User: dynaguy
Functions: ssh
Tags: ssh sudo sshpass
2

Example: remote install an application(wine).

sshpass -p 'mypssword' ssh -t mysshloginname@192.168.1.22 "echo 'mypassword' | sudo -S apt-get install wine"

Tested on Ubuntu.

s() { screen -d -RR -m -S "$1" -t "$USER"@"$1" ssh "$1"; }
2012-09-07 23:02:52
User: salamando
Functions: screen ssh
Tags: ssh screen Linux
5

Use as: $ s host1

Will ssh to remote host upon first invocation. Then use C-a d to detatch. Running "s host1" again will resume the shell session on the remote host. Only useful in LAN environment. You'd want to start the screen on the remote host over a WAN.

Adapted from Hack 34 in Linux Server Hacks 2nd Addition.