Is it possible to “sudo su” using ssh in bash? [duplicate]

#!/bin/bash
USERNAME=ksmith
HOSTS="linux1"
YUPDATE="sudo yum -y update"
FIXDATE="sudo -u echo -e 'ZONE="America/New_York"\nUTC=true' > /etc/sysconfig/clock"

for HOSTNAME in ${HOSTS} ; do
ssh -tt -l ${USERNAME} ${HOSTNAME} "${YUPDATE}; ${FIXDATE}"

done

I get the error:

sudo -u echo -e 'ZONE="America/New_York"\nUTC=true' > /etc/sysconfig/clock
-bash: /etc/sysconfig/clock: Permission denied

I manually went in and tried a sudo and it won’t take. It requires a sudo su first, then it works. But I can’t get it working in bash. I understand Fab / Python can do this, but I’m hoping to keep this in bash.

It’s going to be a script that updates all our servers and then applies the “FIXDATE” fix(which requires sudo su). The yum update works fine.

Answer

Your problem is that sudo only is accepted for the first part, the redirection in the file is not done with higher privileges.

Also your “-u” is a little bit confusing. Do you missing the user there?
And i don’t know if the ” and ‘ are used right, you might have to experoiment with that..

This might work for you:

sudo bash -c 'echo -e "Zone=\"America/New_York\"\nUTC=true" > /etc/sysconfig/clock'

Another way might be to write a seperate script and call that with sudo, so all instructions in the script are called with sudo.

Attribution
Source : Link , Question Author : Sandfrog , Answer Author : moonhawk

Leave a Comment