Send a command to a group of computers in easy way

If we have to manage a group of computes, we sometime need to do a simple task in all computes. How do you do that? Log in to each computer and type the command? It’s very tiring and boring isn’t it? I have a tips that can make your life easier :)

This is the summary:

  1. Create password-less root account in the cluster. No.. not that kind of password-less account. I’ll tell you later.
  2. Make a simple script that will send a command to all computers.

Create a password-less root account

We will use a public/private key authentication scheme to login in a computer. To do this, we have to create a key pair and the private key should not be passworded. Why? If we create a password, we will have to type it again..

# ssh-keygen -t rsa

The command will create a key pair. Just press enter key several times until you found the command prompt again. You private and public key will be stored in your ~/.ssh directory. id_rsa contains your private key and id_rsa.pub is your public key.

Copy your public key to authorized_users file in the same directory. So the sshd will look that file for the authentication process.

# cd ~/.ssh
# cp id_rsa.pub authorized_keys

Copy that 3 files into all computers in your cluster. Yes.. at this time, you have to type your password. But don’t worry, you will not do this anymore.

After that, you can test the password-less authentication by logging in to your another computer.

# ssh another.computer

Create a simple script to send a command to all computers

The script is very simple. Just a looping script that will iterate your computer addresses and give it to the ssh command.

Ssh can be used to run a command in a remote computer.

ssh some.address hostname

If we enter that command, we will make an ssh connection to some.address and run the hostname command in that machine. The standard input will be forwarded to remote machine and standard output/error will be forwarded to our local machine.

To send a command to a group of computers, we can use the following script.

# for addr in 192.168.1.2 192.168.1.3 192.168.1.4
> do
>     ssh $addr halt
> done

The halt command will be sent to three computers (192.168.1.2, 192.168.1.3, and 192.168.1.4). So we can turn off more than one computers at a time.

If there is a pattern in your machine’s address, you can use the simpler variable.

# for addr in 2 3 4
> do
>     ssh 192.168.1.$addr halt
> done

Tired of typing numbers? You can use seq command.

# for addr in `seq 2 4`
> do
>     ssh 192.168.1.$addr halt
> done

All those three commands will do the same!

Here is one more example.

# for addr in `seq 11 26`
> do
>     ssh 192.168.0.$addr mount -a
>     ssh 192.168.0.$addr /etc/init.d/sgeexecd start
> done

Comments

mas minta ijin untuk

mas minta ijin untuk mengambil artikel public keynya

silakan saja :)

silakan saja :)

mas saya juga minta ijin

mas saya juga minta ijin copy tutorialnya ya?

mas fajran, saya udah ngopi

mas fajran, saya udah ngopi key ke mesin tujuan / server. tapi kok tetap gak bisa, apa diserver harus diset apa gitu? pernah sekali bisa, gak tau kenapa, setelah dc terus gak bisa lagi

@fortmunir:

@fortmunir: silakan..

@sumodirjo: ada pesan error apa gtu yg muncul?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options