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 - 294 results
sshmysql() { ssh -L 13306:127.0.0.1:3306 -N $* & }
2011-09-01 10:21:55
Functions: ssh
-1

Create a secure tunnelled connection for access to a remote MySQL database.

For example, connect with MySQL Workbench to root@127.0.0.1:13306.

DATE=$(date +%Y-%m-%d_%H-%M-%S)-$(($(date +%N)/10000000)); HOST=ssh_host; DEST=file_dest; URL=url/screenshot_$DATE.png; import -window root png:- | ssh $HOST "cat > $DEST/screenshot_$DATE.png"; echo $URL | xclip; notify-send -u low "Title" "Message"
2011-08-13 00:40:36
User: swordfischer
Functions: date echo ssh
4

Requires you to have password free login to remote host ;)

Requires xclip and notify-send (If you want to put into clipboard and be notified when action is completed).

DATE=$(date +%Y-%m-%d_%H-%M-%S)-$(($(date +%N)/10000000));

HOST="ssh host of your choice";

DEST="destination folder without trailing slash";

URL="URL for file if uploaded to web enabled dir ie.

import -window root png:- | ssh $HOST "cat > $DEST/screenshot_$DATE.png";

echo $URL | xclip; notify-send -u low "Screenshot Taken" "Entire screen.\nCopied to clipboard"

ssh -C USER@HOST tar -c --exclude /proc --exclude /sys / | tar -x
ssh -f -L3389:<RDP_HOST>:3389 <SSH_PROXY> "sleep 10" && rdesktop -T'<WINDOW_TITLE>' -uAdministrator -g800x600 -a8 -rsound:off -rclipboard:PRIMARYCLIPBOARD -5 localhost
2011-07-14 05:48:06
User: johnquail
Functions: ssh
12

This command will:

1. open an SSH tunnel to

2. go to background

3. wait for 10 seconds for the connection

4. during the 10 seconds wait it will localy run 'rdesktop' to connect to the remote host through the created SSH tunnel.

Password-less log in can be achieved (when server allows it) by adding '-p ' to the 'rdesktop' command

ssh-keygen -C hello@world
2011-07-06 18:02:32
User: aykut
Functions: ssh ssh-keygen
0

Now at the end of the rsa.pub file, there is our comment like=

".................peXeuE0ytJgpQcXeR5aHlfLa8dAt0obasd hello@world"

var=host ;while ! nc -zw 1 $var 22;do sleep 1; done ; ssh user@$var
ssh USER@HOST "sudo tshark -i eth0 -f 'tcp port 80 and tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420' -w -" | wireshark -k -i -
while true; do date; ssh <YOUR HOST HERE> "echo" && echo "HOST UP" && break; sleep 60; done
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