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 - 285 results
ssh -L :: -L :: @
2009-06-22 19:12:48
User: starchox
Functions: ssh
10

Thankfully, the ssh command allows you to specify multiple tunnels through the same server in one command.

Remeber if you want a priviliged port on your machine, you must use root or sudo account.

ssh -f -N -L 5432:talk.google.com:5222 user@home.network.com
2009-06-05 23:17:21
User: dcabanis
Functions: ssh
Tags: ssh
12

If your firewall or proxy at your location prevents connection to a particular host or port, you can use ssh to tunnel to your home server and do it there instead.

ssh $HOST -l$USER cat /REMOTE/FILE | sdiff /LOCAL/FILE -
ssh -C -X user@remotehost gui_command
2009-05-21 17:52:24
User: sharfah
Functions: ssh
-2

Run GUI apps on another machine remotely through SSH.

-C is for data compression and -X enables X11 forwarding.

clear; for i in `cat thehosts` ; do ssh $i "cat uname -a ; /etc/redhat-release; cat /proc/cpuinfo | tail -n 25 | egrep '^processor|^model name' "; free ; df -h ;done
ssh -i $PRIVATEKEY $HOST -C 'cd $SOURCE; tar -cz --numeric-owner .' | tee $DESTINATION/backup.tgz | tar -tz
2009-05-18 20:36:45
User: GeckoDH
Functions: ssh tar tee
1
PRIVATEKEY - Of course the full path to the private key \n HOST - The host where to get the backup \n SOURCE - The directory you wish to backup \n DESTINATION - The destination for the backup on your local machine
chkconfig --list | fgrep :on | sed -e 's/\(^.*\)*0:off/\1:/g' -e 's/\(.\):on/\1/g' -e 's/.:off//g' | tr -d [:blank:] | awk -F: '{print$2,$1}' | ssh host 'cat > foo'
2009-05-13 21:17:39
User: catawampus
2

And then to complete the task:

Go to target host;

ssh host

Turn everything off:

for i in `chkconfig --list | fgrep :on | awk '{print $1}'` ; do chkconfig --level 12345 $i off; done

Create duplicate config:

while read line; do chkconfig --level $line on; done < foo
ssh user@server | tee logfilename
2009-04-17 19:17:02
User: bassu
Functions: ssh tee
6

Optionally, you can create a new function to do this with a custom command. Edit $HOME/.bashrc and add:

myssh () { ssh $1 | tee sshlog ; }

Save it.

At command prompt:

myssh user@server
diff <(ssh alice cat /etc/apt/sources.list) <(ssh bob cat /etc/apt/sources.list)
ssh -vN hostname 2>&1 | grep "remote software version"
2009-03-31 18:28:41
User: sud0er
Functions: grep hostname ssh
Tags: ssh
2

I used this to confirm an upgrade to an SSH daemon was successful

ssh user@host "(cd /path/to/remote/top/dir ; tar cvf - ./*)" | tar xvf -
2009-03-31 13:08:45
User: dopeman
Functions: ssh tar
Tags: copy files
1

This command will copy files and directories from a remote machine to the local one.

Ensure you are in the local directory you want to populate with the remote files before running the command.

To copy a directory and it's contents, you could:

ssh user@host "(cd /path/to/a/directory ; tar cvf - ./targetdir)" | tar xvf -

This is especially useful on *nix'es that don't have 'scp' installed by default.

[[ $(COLUMNS=200 ps faux | awk '/grep/ {next} /ssh -N -R 4444/ {i++} END {print i}') ]] || nohup ssh -N -R 4444:localhost:22 user@relay &
2009-03-31 09:39:59
User: j0rn
Functions: awk nohup ps ssh
Tags: ssh cronjob
4

I find it ugly & sexy at the same time isn't it ?

tar -czf - * | ssh example.com "cat > files.tar.gz"
2009-03-24 17:02:02
User: migurski
Functions: ssh tar
5

I recently found myself with a filesystem I couldn't write to and a bunch of files I had to get the hell out of dodge, preferably not one at a time. This command makes it possible to pack a bunch of files into a single archive and write it to a remote server.

ssh-keygen; ssh-copy-id user@host; ssh user@host
2009-03-18 07:59:33
User: bwoodacre
Functions: ssh ssh-keygen
Tags: openssh
18

This command sequence allows simple setup of (gasp!) password-less SSH logins. Be careful, as if you already have an SSH keypair in your ~/.ssh directory on the local machine, there is a possibility ssh-keygen may overwrite them. ssh-copy-id copies the public key to the remote host and appends it to the remote account's ~/.ssh/authorized_keys file. When trying ssh, if you used no passphrase for your key, the remote shell appears soon after invoking ssh user@host.

ssh user@host "cd targetdir; tar cfp - *" | dd of=file.tar
2009-03-18 07:43:22
User: bwoodacre
Functions: dd ssh
6

This invokes tar on the remote machine and pipes the resulting tarfile over the network using ssh and is saved on the local machine. This is useful for making a one-off backup of a directory tree with zero storage overhead on the source. Variations on this include using compression on the source by using 'tar cfvp' or compression at the destination via

ssh user@host "cd dir; tar cfp - *" | gzip - > file.tar.gz
rsync -avz -e ssh --files-from=<(find -mtime +30 -mtime -60) source dest
2009-03-13 12:58:28
User: voyeg3r
Functions: find rsync ssh
6

rsync from source to dest all between >30

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
118

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.