Skip to main content

VentureCake » Blog Archive » 10 Linux Shell Tricks You Don't ...

Popularity Report

Total Popularity Score: 0

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Rank

Groups (1)

  • caravat

    caravat

    3 members,26 bookmarks

    no description

Bookmark History

Saved by 22 people (7 private), first by anonymouse user on 2007-06-18


Public Sticky notes

6. Add Your Public Key to Remote Machines the Easy Way
In order to perform key based logins to a new machine, you need to get a copy of a public key to the remote machine yourself. Sure, you could do this manually – which gets a bit boring after a while (why doesn’t SSH have an authorized_keys.d anyway?), but why waste time when SSH comes with it the tool to do it?
Just run:

ssh-copy-id -i .ssh/id_rsa.pub hostname

After being prompted to enter your password for the last time, SSH will say:
Now try logging into the machine, with “ssh ‘hostname’”, and check in:

.ssh/authorized_keys

to make sure we haven’t added extra keys that you weren’t expecting.

Try it. No more passwords!

Highlighted by ejwettstein

2. Parallelize Your Loops
Almost every Linux network administrator knows the power of the for loop: the way to do something for one, one hundred or one thousand users, files, machines, processes, or whatever else. Most people set their loops in sequence – so that each jobs is finished before moving onto the next.
But the jobs command can be used to background each loop, so you don’t have to wait for it to complete before continuing with the next.

Here’s an example running an apt-get update:

for HOST in $(< ListOfHosts); do ssh $HOST ’sudo apt-get update’ & done

Maybe you need a bunch of SSH tunnels running simultaneously:

for HOST in $(< ListOfHosts); do ssh -C -N -R 80:localhost:80 $HOST & done

Sometimes you probably don’t want to see all the output as it happens – in that case, save a file on each machine and use another loop to collect it later.

Benefit: Saving a metric shitload (2/3rd of an imperial shitload) of time waiting on stuff to finish
Works in: any currently supported Linux
Drawbacks: Bash probably has some limits of the amount of concurrent jobs. But I’ve yet to run into them.

Highlighted by ejwettstein