-2
#!/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.

1
  • Yep, "sudo /bin/su -c" solved it. Many thanks MadHatter.
    – Sandfrog
    Jan 21, 2016 at 19:41

1 Answer 1

0

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.

1
  • The problem is even when I log in on the server and sudo the command, I STILL get the error. But for some odd reason when I first do a sudo su(which puts me in root) I can then pass that command. I'm not sure why it requires sudo su. But I need to somehow do that in my bash script with ssh.
    – Sandfrog
    Jan 21, 2016 at 19:29

Not the answer you're looking for? Browse other questions tagged .