Mayana  Khan
Mayana Khan

Reputation: 23

Unable to switch to root user from shell script

I want to change the user and group of /application directory for list of server.

I have created the below script, when i run the script then it prompt for the password, when i enter the password nothing is happening script is strucking there itself.

#!/bin/bash
file=hosts.txt
for HOST in $(grep -v '^#' $file)
do
ssh -T $HOST <<EOF
sudo su - root
chown root:root /application
echo "Hostname : " \$(hostname)
EOF
sleep 2
done

Below is the prompting output

[mayana@TestHOST test1]$ sh test.sh
Last login: Fri Nov  9 14:06:56 2018 from TestHOST.com
sudo su - root
[mayana@TargetHost1 ~]$ sudo su - root
[sudo] password for mayana: Password
^CKilled by signal 2.
^C
[mayana@TestHOST test1]$

Please help me how can i pass the password for this script.

Upvotes: 1

Views: 1842

Answers (1)

dbush
dbush

Reputation: 223757

When you run this command:

sudo su - root

It starts an interactive root shell. It does not cause the remaining commands in the script to run as root.

You need to give the specific command to run as parameters to sudo:

sudo chown root:root /application

The script will still prompt for a password but after that it will continue running.

Upvotes: 2

Related Questions