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
ssh -NL 2000:remotehost:22 remotehost
ssh -fX <user>@<host> <program>
2009-03-10 15:02:35
User: prayer
Functions: ssh
Tags: ssh
15
ssh -X example.org xeyes

The SSH server configuration requires:

X11Forwarding yes # this is default in Debian

And it's convenient too:

Compression delayed

stty -echo; ssh -t HOSTNAME "sudo some_command"; stty echo
2009-03-04 19:44:36
User: jmcantrell
Functions: ssh stty
Tags: ssh sudo
3

The ssh command alone will execute the sudo command remotely, but the password will be visible in the terminal as you type it. The two stty commands disable the terminal from echoing the password back to you, which makes the remote sudo act as it does locally.

ssh -t reachable_host ssh unreachable_host
2009-03-03 23:21:36
User: patko
Functions: ssh
Tags: ssh
122

Unreachable_host is unavailable from local network, but it's available from reachable_host's network. This command creates a connection to unreachable_host through "hidden" connection to reachable_host.

LC_ALL=fr_FR luit ssh root@remote_machine_ip
ssh -T user@host /bin/bash -i
2009-03-02 21:39:30
User: Bender
Functions: ssh
3

opens a "hidden" remote shell (login will not appear in "last" for example).

This is not really hidden, because the login will be shown in auth.log and the process is visible anyways.

ssh -T = Disable pseudo-tty allocation.

bash -i = interactive shell

ssh -MNf <user>@<host>
2009-02-26 14:11:19
User: raphink
Functions: ssh
40

Create a persistent SSH connection to the host in the background. Combine this with settings in your ~/.ssh/config:

Host host

ControlPath ~/.ssh/master-%r@%h:%p

ControlMaster no

All the SSH connections to the machine will then go through the persisten SSH socket. This is very useful if you are using SSH to synchronize files (using rsync/sftp/cvs/svn) on a regular basis because it won't create a new socket each time to open an ssh connection.

tar cpfP - $(find <somedir> -type f -name *.png) | ssh user@host | tar xpfP -
2009-02-26 00:01:30
User: raphink
Functions: find ssh tar
7

Notes:

* Adjust the find command to your own filters.

* The -P flag forces to keep absolute paths in the tarball, so that you can be sure that the exact same file hierarchy will be created on the second machine.

ssh -A user@somehost
2009-02-21 16:01:36
User: f4nt
Functions: ssh
6

the -A argument forwards your ssh private keys to the host you're going to. Useful in some scenarios where you have to hop to one server, and then login to another using a private key.

for i in `ps aux | grep ssh | grep -v grep | awk {'print $2'}` ; do kill $i; done
ssh -t user@some.domain.com /usr/bin/screen -xRR
2009-02-20 23:39:54
User: olifante
Functions: ssh
28

Long before tabbed terminals existed, people have been using Gnu screen to open many shells in a single text terminal. Combined with ssh, it gives you the ability to have many open shells with a single remote connection using the above options. If you detach with "Ctrl-a d" or if the ssh session is accidentally terminated, all processes running in your remote shells remain undisturbed, ready for you to reconnect. Other useful screen commands are "Ctrl-a c" (open new shell) and "Ctrl-a a" (alternate between shells). Read this quick reference for more screen commands: http://aperiodic.net/screen/quick_reference

for i in $(cat listofservers.txt); do konsole --new-tab -e ssh $i; done
2009-02-19 16:32:36
User: nottings
Functions: cat ssh
4

creates a new tab for each of N servers in listofservers.txt and ssh's to said servers

then, try the "send to all sessions" feature of konsole to do the same work on all servers at the same time. BIG time saver, but be careful!

cat .ssh/id_dsa.pub | ssh elsewhere "[ -d .ssh ] || mkdir .ssh ; cat >> .ssh/authorized_keys"
2009-02-18 22:13:04
User: jsiren
Functions: cat ssh
5

Enter your ssh public key in the remote end for future key-based authentication. Just type your password one last time. The next time you should be able to login with the public key. If you don't have a key, generate one with ssh-keygen.

Requires Bourne-compatible shell in the remote end.

rsync -avz -e ssh username@hostname:/path/to/remote/dir/ /path/to/local/dir/
ssh user@host 'mysqldump dbname | gzip' > /path/to/backups/db-backup-`date +%Y-%m-%d`.sql.gz
2009-02-18 21:12:53
User: root
Functions: ssh
Tags: mysql
17

I have this on a daily cronjob to backup the commandlinefu.com database from NearlyFreeSpeech.net (awesome hosts by the way) to my local drive. Note that (on my Ubuntu system at least) you need to escape the % signs on the crontab.

ssh user@host cat /path/to/some/file | xclip
2009-02-18 10:56:38
User: raphink
Functions: cat ssh
Tags: X X11
18

Have you ever had to scp a file to your work machine in order to copy its contents to a mail? xclip can help you with that. It copies its stdin to the X11 buffer, so all you have to do is middle-click to paste the content of that looong file :)

tar cvzf - /directory/ | ssh root@host "cat > /dev/nst0"
2009-02-18 07:12:54
User: Tuxmania
Functions: ssh tar
3

I use this all the time for taking manual backups of stuff i want to keep but not important enough to backup regularly.

mysqldump -uUserName -pPassword tudb | ssh root@rootsvr.com "mysql -uUserName -pPassword -h mysql.rootsvr.com YourDBName"
2009-02-17 22:36:06
User: elecboy
Functions: ssh
5

This option makes a copy of your current db and via ssh it transfer to the server you specify and upload the database to the specific User & Password you specify (Note the db User & Pass, can be different from the one you use in the other server)

If you are going to use "localhost" as your main db remove (-h) and youst add "localhost"

mysqldump --add-drop-table --extended-insert --force --log-error=error.log -uUSER -pPASS OLD_DB_NAME | ssh -C user@newhost "mysql -uUSER -pPASS NEW_DB_NAME"
2009-02-17 17:57:53
User: Tscherno
Functions: ssh
27

Dumps a MySQL database over a compressed SSH tunnel and uses it as input to mysql - i think that is the fastest and best way to migrate a DB to a new server!

ssh -CNL 3306:localhost:3306 user@site.com
ssh -D 8888 user@site.com
2009-02-17 15:34:27
User: wesrog
Functions: ssh
5

Simply change your web browser's proxy settings to point to a SOCKS proxy at port 8888 and you're good to go.

ssh-copy-id username@hostname
cat ~/.ssh/*.pub | ssh user@remote-system 'umask 077; cat >>.ssh/authorized_keys'
ssh -L 5900:localhost:5900 user@exampleserver.com
2009-02-13 04:53:27
User: Jackie
Functions: ssh
1

Foward vnc securely from exampleserver.com

cat .ssh/id_rsa.pub | ssh hostname 'cat >> .ssh/authorized_keys'
2009-02-11 17:40:12
User: bendavis78
Functions: cat hostname ssh
14

Just run the command, type your password, and that's the last time you need to enter your password for that server.

This assumes that the server supports publickey authentication. Also, the permissions on your home dir are 755, and the permissions on your .ssh dir are 700 (local and remote).