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 - 286 results
ssh -R 9000:localhost:9000 you@remote-php-web-server.com
2011-05-28 09:39:16
User: nadavkav
Functions: ssh
2

If you need to xdebug a remote php application, which is behind a firewall, and you have an ssh daemon running on that machine. you can redirect port 9000 on that machine over to your local machine from which you run your xdebug client (I am using phpStorm)

So, run this command on your local machine and start your local xdebug client, to start debugging.

more info:

http://code.google.com/p/spectator/wiki/Installing

ssh tomcat-server ls -l webapp-dir | grep -- '->' | awk ' { print $(NF-2) " " $(NF-1) " " $NF; }'
2011-05-23 08:38:28
User: igorfu
Functions: awk grep ls ssh
Tags: tomcat
0

Tomcat webapps are often remote links

ssh -X johndoe@123.456.789
2011-05-19 21:28:25
User: evandrix
Functions: ssh
-5

To start X11 display only on your local machine: xeyes &

Extensions to basic command: ssh -X -f user@remotehost xcalc -bg black -fg green

ssh-keygen ptaduri@c3pusas1
ssh remotehost 'dpkg --get-selections' | dpkg --set-selections && dselect install
2011-05-12 17:26:43
User: Mozai
Functions: ssh
23

(also works on Ubuntu) Copies the 'install,' 'hold,' 'deinstall' and 'purge' states of packages on the remote machine to be matched on the local machine. Note: if packages were installed on the local machine that were never installed on the remote machine, they will not be deinstalled by this operation.

nice -n -20 ssh SOURCE_SERVER "tail -f /var/log/httpd/access.log " | awk '{print $7}' | grep jsp | parallel 'curl TARGET_SERVER{} 2>&1 /dev/null'
2011-05-09 19:32:40
User: dennisfaust
Functions: awk grep nice ssh
1

This takes the stream created by apache requests containing jsp and funnels them to another server. I'm using this for simulating real time traffic. The nice command gives ssh maximum CPU cycles, awk & grep strip out everything served by apache. Putting parallel on curl is important because curl is synchronous and waits for the response. Yes, I thought about using wget but it didn't seem any easier. Also, if you figure out how to run this in the background let me know. Every time I background it it stops. If you have multiple front end servers just run multiple instances of this.

ssh -l <username> <server>
ssh vm-user@`VBoxManage guestproperty get "vm-name" "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print $2 }'`
2011-05-04 18:01:36
User: lucasrangit
Functions: awk get ssh
9

Booting the VM headless via VBoxHeadless requires knowledge of the VM's network in order to connect. Using VBoxManage in this way and you can SSH to the VM without first looking up the current IP, which changes depending on how you have your VM configured.

Diff files over SSH: ssh [login]@[host] "cat [remote file]" | diff - "[local file]"
2011-04-28 16:59:56
User: dennisfaust
Functions: diff ssh
3

Sometimes you need to compare two config files on different servers. Put the file names into the above script and let 'er rip.

pbpaste | ssh user@hostname pbcopy
2011-04-24 16:30:48
User: DavidLudwig
Functions: ssh
Tags: ssh osx clipboard
1

This uses ssh to transfer the contents of one Mac's clipboard to another's. This only works with plain text, sadly. Trying to transfer images will just clear out the remote machine's clipboard, and rich text will be converted to plain text. Using the "Remote Login" must be enabled on the remote machine (via System Preferences' Sharing panel) for this to work.

ssh user@remote-host "DISPLAY=:0.0 import -window root -format png -"|display -format png -
2011-04-08 01:07:41
User: splante
Functions: ssh
11

Admittedly, I'd never have thought of this without the earlier examples, but here's one that you can execute from your workstation to just display the image from another, without separately doing a file transfer, etc. By the way, I hear a loud beep coming from the other room, so I guess it's not too stealthy :-D

cat /dev/zero | pv | ssh 192.168.1.2 "cat > /dev/null"
ssh username@remotehost 'mysqldump -u <dbusername> -p<dbpassword> <dbname> tbl_name_1 tbl_name_2 tbl_name_3 | gzip -c -' | gzip -dc - | mysql -u <localusername> -p<localdbpassword> <localdbname>
pdsh -R ssh -w se00[1-5] # a list of host names
2011-03-10 18:29:24
User: dexterhu
Functions: host ssh
1

parrallel execution of a command on remote host by ssh or rsh or ...

very useful for cluster management (software update)

ssh username@remotehost 'mysqldump -u <dbusername> -p<dbpassword> <dbname> tbl_name_1 tbl_name_2 tbl_name_3' | mysql -u <localusername> -p<localdbpassword> <localdbname> < /dev/stdin
2011-03-09 18:35:07
User: tur_ki_sh
Functions: ssh
1

In the example above 3 tables are copied. You can change the number of tables. You should be able to come up with variants of the command by modifying the mysqldump part easily, to copy some part of remote mysql DB.

until (ssh root@10.1.1.39 2> /dev/null); do date; sleep 15; done
2011-03-08 08:42:12
User: greggster
Functions: sleep ssh
8

until (ssh root@10.1.1.39 2> /dev/null); do date; sleep 15; done

In this case will execute "date" then "sleep 15" until we are able to ssh into server, such as after a reboot

Could also be like:

until ( ping 10.1.1.39 1> /dev/null); do echo "server 10.1.1.39 is down"; sleep 15; done

ssh -tq -o "BatchMode yes" $HOST <some_command> >> to_a_file
2011-03-02 20:33:59
User: jp
Functions: ssh
1

Need to query hundreds of hosts with an ssh command ?

Of course you'll have setup keys on all your remote HOSTs. But in the case a key is not present this command will skip that node, proceeding on to the next.

-t:

Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine. Also prevents unwanted stty messages being sent to console

-q:

Quiet mode.

-o "BatchMode yes"

If set to yes, passphrase/password querying will be disabled. This option is useful in scripts and other batch jobs where no user is present

ssh -NR 0.0.0.0:2222:127.0.0.1:22 user@jump.host.com
2011-03-01 23:49:28
User: mweed
Functions: ssh
11

Useful to get network access to a machine behind shared IP NAT. Assumes you have an accessible jump host and physical console or drac/ilo/lom etc access to run the command.

Run the command on the host behind NAT then ssh connect to your jump host on port 2222. That connection to the jump host will be forwarded to the hidden machine.

Note: Some older versions of ssh do not acknowledge the bind address (0.0.0.0 in the example) and will only listen on the loopback address.

ssh -t -X -A user@sshproxy ssh -X -A user@sshhost
2011-02-16 15:01:18
User: FuFighter
Functions: ssh
1

Simply makes it possible to launch any X application residing on sshhost through sshproxy and display it on your screen where ever you are.

cat tarfile.tar.gz | ssh server.com " cd /tmp; tar xvzf - directory/i/want"
2011-02-11 17:10:01
User: alf
Functions: cat ssh
Tags: ssh tar
0

This may be listed already but this command is useful to untar a specific directory to a different server.

cat LOCALFILE | ssh HOST "cat > REMOTEFILE"
2011-02-03 05:29:06
User: eataudio
Functions: cat ssh
0

Nice command for when you don't have scp available for whatever reason.

Works with binaries.

ssh-keygen -R hostname
ssh-copy-id -i .ssh/id_rsa.pub username:password@remotehost.com
ssh USER@REMOTESYSTEM arecord - | aplay -
2011-01-31 16:49:45
User: mu_mind
Functions: aplay arecord ssh
4

Record off the microphone on a remote computer and listen to it live through your speakers locally.

mysqldump -u user -p --all-databases | ssh user@host dd of=/opt/all-databases.dump