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 tagged ssh from sorted by
Terminal - Commands tagged ssh - 168 results
ssh -t server 'cd /etc && $SHELL'
2010-04-02 19:34:09
User: dooblem
Functions: ssh
Tags: ssh
4

Useful to create an alias that sends you right in the directory you want :

alias server-etc="ssh -t server 'cd /etc && $SHELL'"

echo 'ServerAliveInterval 60' >> /etc/ssh/ssh_config
2010-03-31 09:22:54
User: rpavlick
Functions: echo
Tags: ssh timeout
6

ssh_config is the system-wide configuration file for ssh.

For per-user configuration, which allows for different settings for each host:

echo 'ServerAliveInterval 60' >> ~/.ssh/ssh_config

On OSX:

echo 'ServerAliveInterval 60' >> ~/.ssh/config

or

echo 'ServerAliveInterval 60' >> ~/etc/ssh_config
ssh user@host "cat /path/to/backup/backupfile.tar.bz2" |tar jpxf -
2010-03-24 01:35:28
User: mack
Functions: ssh tar
Tags: ssh tar
8

Here how to recover the remote backup over ssh

tar jcpf - [sourceDirs] |ssh user@host "cat > /path/to/backup/backupfile.tar.bz2"
2010-03-24 01:29:25
User: mack
Functions: ssh tar
Tags: ssh tar
13

Execute it from the source host, where the source files you wish backup resides. With the minus '-' the tar command deliver the compressed output to the standar output and, trough over the ssh session to the remote host. On the other hand the backup host will be receive the stream and read it from the standar input sending it to the /path/to/backup/backupfile.tar.bz2

for I in $(mysql -e 'show databases' -u root --password=root -s --skip-column-names); do mysqldump -u root --password=root $I | gzip -c | ssh user@server.com "cat > /remote/$I.sql.gz"; done
2010-03-07 15:03:12
User: juliend2
Functions: gzip ssh
6

It grabs all the database names granted for the $MYSQLUSER and gzip them to a remote host via SSH.

startx -- /usr/X11R6/bin/Xnest :5 -geometry 800x600
scp -l10 pippo@serverciccio:/home/zutaniddu/* .
2010-02-19 16:44:24
User: 0disse0
Functions: scp
Tags: ssh scp bandwidth
11

the command is obvious, I know, but maybe not everyone knows that using the parameter "-l" you can limit the use of bandwidth command scp.

In this example fetch all files from the directory zutaniddu and I copy them locally using only 10 Kbs

file='path to file'; tar -cf - "$file" | pv -s $(du -sb "$file" | awk '{print $1}') | gzip -c | ssh -c blowfish user@host tar -zxf - -C /opt/games
2010-01-19 16:02:45
User: starchox
Functions: awk du file gzip ssh tar
3

You set the file/dirname transfer variable, in the end point you set the path destination, this command uses pipe view to show progress, compress the file outut and takes account to change the ssh cipher. Support dirnames with spaces.

Merged ideas and comments by http://www.commandlinefu.com/commands/view/4379/copy-working-directory-and-compress-it-on-the-fly-while-showing-progress and http://www.commandlinefu.com/commands/view/3177/move-a-lot-of-files-over-ssh

pv /dev/zero|ssh $host 'cat > /dev/null'
2010-01-06 20:40:51
User: opertinicy
Functions: ssh
Tags: ssh pv /dev/null
11

connects to host via ssh and displays the live transfer speed, directing all transferred data to /dev/null

needs pv installed

Debian: 'apt-get install pv'

Fedora: 'yum install pv' (may need the 'extras' repository enabled)

mkfifo /tmp/fifo; ssh-keygen; ssh-copyid root@remotehostaddress; sudo ssh root@remotehost "tshark -i eth1 -f 'not tcp port 22' -w -" > /tmp/fifo &; sudo wireshark -k -i /tmp/fifo;
sudo ssh -Y remoteuser@remotehost sudo wireshark
2010-01-05 14:35:20
User: Code_Bleu
Functions: ssh sudo
-8

This allows you to display the wireshark program running on remote pc to your local pc.

yes | pv | ssh $host "cat > /dev/null"
2009-12-27 21:34:23
User: opertinicy
Functions: ssh yes
Tags: ssh yes pv
23

connects to host via ssh and displays the live transfer speed, directing all transferred data to /dev/null

needs pv installed

Debian: 'apt-get install pv'

Fedora: 'yum install pv' (may need the 'extras' repository enabled)

ssh root@server.com 'tshark -f "port !22" -w -' | wireshark -k -i -
2009-12-17 23:03:24
User: markdrago
Functions: ssh
26

This captures traffic on a remote machine with tshark, sends the raw pcap data over the ssh link, and displays it in wireshark. Hitting ctrl+C will stop the capture and unfortunately close your wireshark window. This can be worked-around by passing -c # to tshark to only capture a certain # of packets, or redirecting the data through a named pipe rather than piping directly from ssh to wireshark. I recommend filtering as much as you can in the tshark command to conserve bandwidth. tshark can be replaced with tcpdump thusly:

ssh root@example.com tcpdump -w - 'port !22' | wireshark -k -i -
ssh -4 -C -c blowfish-cbc
2009-12-15 00:30:53
User: vxbinaca
Functions: ssh
Tags: ssh
18

We force IPv4, compress the stream, specify the cypher stream to be Blowfish. I suppose you could use aes256-ctr as well for cypher spec. I'm of course leaving out things like master control sessions and such as that may not be available on your shell although that would speed things up as well.

ssh <host> 'tar -cz /<folder>/<subfolder>' | tar -xvz
2009-11-10 20:06:47
User: polaco
Functions: ssh tar
9

This command will copy a folder tree (keeping the parent folders) through ssh. It will:

- compress the data

- stream the compressed data through ssh

- decompress the data on the local folder

This command will take no additional space on the host machine (no need to create compressed tar files, transfer it and then delete it on the host).

There is some situations (like mirroring a remote machine) where you simply cant wait for a huge time taking scp command or cant compress the data to a tarball on the host because of file system space limitation, so this command can do the job quite well.

This command performs very well mainly when a lot of data is involved in the process. If you copying a low amount of data, use scp instead (easier to type)

ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key.pub && ssh-keygen -l -f /etc/ssh/ssh_host_dsa_key.pub
2009-10-26 17:52:41
Functions: ssh ssh-keygen
Tags: ssh bash
4

Get your server's fingerprints to give to users to verify when they ssh in. Publickey locations may vary by distro. Fingerprints should be provided out-of-band.

ssh user@machine_A dd if=/dev/dvd0 > dvddump.iso
2009-09-11 18:08:36
User: kamathln
Functions: dd ssh
Tags: ssh dvd dump
5

You can use this to directly dump from machine A (with dvd drive) to machine B (without dvd drive) . I used this to copy dvd using my friend's machine to my netbook. Above command is to be issued on machine B.

Advantages :

1) No wasting time dumping first to machine A and then copying to Machine B.

2) You dont need to use space on Machine A. In fact, this will work even when Machine A doesnt have enough hdd space to dump the DVD.

Use -C ssh option on slow networks (enables compression).

you can replace "dd if=/dev/dvd" with any ripping command as long as it spews the iso to stdout.

diff <(ssh server01 'cd config; find . -type f -exec md5sum {} \;| sort -k 2') <(ssh server02 'cd config;find . -type f -exec md5sum {} \;| sort -k 2')
2009-09-11 15:24:59
User: arcege
Functions: diff find md5sum sort ssh
14

This can be much faster than downloading one or both trees to a common servers and comparing the files there. After, only those files could be copied down for deeper comparison if needed.

dsh -M -c -f servers -- "command HERE"
2009-08-31 12:08:38
User: foob4r
Tags: ssh multiple
4

dsh - Distributed shell, or dancer?s shell ;-)

you can put your servers into /etc/dsh/machines.list than you don't have to serperate them by commata or group them in different files and only run commands for this groups

dsh -M -c -a -- "apt-get update"

ssh user@remotehost [anycommand](i.e uptime,w)
2009-08-29 04:27:37
User: unixbhaskar
Functions: ssh
Tags: ssh shell
-3

Once it is connected to the remote server by that ssh protocol,the mentioned command will start working on that server.

ssh middlehost "ssh -a root@securehost '> nicescript'" < nicescript
2009-08-26 20:25:12
User: 0x89
Functions: ssh
Tags: ssh scp
0

Stop tormenting the poor animal cat!

Also you should not that you need a private key not protected by a passphrase on middlehost that grants you access to securehost..

for i in 192.168.1.{61..71};do ping -c 1 $i &> /dev/null && echo $i;fi;done
2009-08-26 06:04:24
User: AlecSchueler
Functions: echo ping
Tags: Network ssh bash
-2

If you need to ssh into a computer on the local network but you're unsure of the ip to use, then ping them and see if you get a response. If you do, print out the address you got it from. Adjust the range to suit your network.

rsync -az /home/user/test user@sshServer:/tmp/
2009-08-25 10:45:15
User: peshay
Functions: rsync
Tags: ssh file move
14

copy files to a ssh server with gzip compression

cat nicescript |ssh middlehost "cat | ssh -a root@securehost 'cat > nicescript'"
2009-08-25 08:11:12
User: syladmin
Functions: cat ssh
Tags: ssh scp
4

middlehost allows ssh access from where you are but not securehost.

Use nice ssh piping to simulate scp through A => B => C

setting up the shell function if left as an exercise for the reader. ;-)

Agent forwarding should avoid password typing.