Showing posts with label SSH. Show all posts
Showing posts with label SSH. Show all posts

Quick HOWTO: Run SSH sessions in Parallel


 You often used "for loop" in shell script to gather an information from large number of your Linux / Unix infrastructure servers. However if your server count is more, it may take lot of time to finish the task. Do you think it will be good if the ssh sessions to remote servers run in parallel? Since the modern day servers having multi-core CPU's we can utilize the real parallel and multi processing of them. 

xargs is your friend here to accomplish your this.

Here is the example to run the SSH sessions in parallel:

xrags -a -n1 -P50 -I{} sh -c "ssh -q {} 'df -Ph /var' "


In this example,

-a --  Takes the input from the file instead of STDIN

-P -- number to processes to execute in parallel. In our example we are starting 50 processes in parallel.

-I -- Used to specify a name to the variable passing to the command we are executing. In our example, we have used {}. For example, You can specify like -I"server" instead of I{}.

Another example:

xrags -a -n1 -P50 -I"server" sh -c "scp server:/tmp/"



One disadvantage in xargs is the output will not be in a order. you need to make your script writes to a log with clear host identification to gather a report from multiple hosts.

Not only SSH and SCP, you can run any process / task which you want to start and run in parallel. Alternatively you can download and use "parallel" tool which is slightly better than xargs.




See other users SSH sessions in Linux

Are wondering what your users are doing in their SSH sessions? Do you want to see, whats happening there?

Try the following:
# cat /dev/vcs1
This will show you what happenening on the first console. If someone is typing, you’ll be able to get an output of their keystrokes.

Note: You should have "root" access for this.

Replace /dev/vcs1 with /dev/vcs2 or /dev/vcs3 and so on for other consoles.

Enjoy..!

Limit SSH Access for Users

How do you limit the access to the SSH for different users in Linux server?

We can use “Match” in the OpenSSH configuration file. Add the Match directive in your sshd_config followed by a criteria which you want to block (User, Group, Address or Host).

The following example limits TCP and X11 port forwarding for the user "testuser":

AllowTcpForwarding no

X11Forwarding no

Match User jack

AllowTcpForwarding yes

X11Forwarding yes

Hope this helps..!

Copy Public Keys using ssh-copy-id to remote servers

ssh-copy-id is a utility which comes with the OpenSSH package.


ssh-copy-id copies your public identity keys to the remote server, in the correct format, makes sure file permissions and ownership are correct, and ensures a private key is not accidentally copied.



To Generate the keys (If you have not done already) :

# ssh-keygen -t rsa

This is will create your public and private keys and will place them by default in .ssh folder in your home directory.

To setup password less login (key based login), you need to append your public key to the authorized_keys file on the remote server which you want to setup the key based authentication.

You can do conventional copy/paste, etc.. But it takes much time and its repetitive job for multiple hosts. 

Enable SSH in VMware ESXi Server

One of the things that annoyed in VMware ESXi Server is the lack of SSH support.   By default, VMware ESXi Server does not support SSH. But it is there in the operating system, Its just not enabled.   


Here is the steps to make SSH turn on in your VMware ESXi Server :


  1. Get on the console of the ESXi server.
  2. Press ALT-F1 to get to the OS system console
  3. Type “unsupported”
  4. Enter the root password at the password prompt.
  5. Edit /etc/inetd.conf with vi, and uncomment the SSH line
  6. Run:  kill -1 $(cat /var/run/inetd.pid)


Thats all.. Now you can SSH to your ESXi Server..