Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm developing an automatic bootstrapper for some servers as we are using Amazon EC2 for our infrastructure.

What I do is: run a Fabric script which connects to EC2 and initializes a new instance, putting in user-data a cloud-init script. This script checks out a Mercurial project which contains Fabric tasks, then, after the checkout, the script will find out which type of server it's running in and execute a specific local Fabric task.

My problem is that some tasks needs to run sudo commands, as the script was initialized from cloud-init it happens that sudo warns about needing to run over a tty, I tried to modify that to run su --session-command="my commands to restart services" root and it doesn't (and seems like that shouldn't) work at all.

So, how can I run sudo commands in this boot script?

Some code:


su --session-command="\
source /etc/profile; \
cd /home/my_user; \
hg clone ssh://fabric_tasks_repo fabric; \
/usr/local/bin/fab -f /home/my_user/fabric/fabfile.py \`ec2-describe-tags   --filter \"resource-type=instance\"   --filter \"resource-id=$(ec2-metadata -i | cut -d ' ' -f2)\"   --filter \"key=type\" | cut -f5\` > /home/my_user/fabric.log 2>&1" my_user &

The trick in the /usr/local/bin/fab line is that it'll run some ec2 scripts to check which tag-key "type" the server is in.

share|improve this question

2 Answers 2

up vote 3 down vote accepted
sudo: Sorry, you must have a tty to run sudo

Run visudo and add the following line to disable requiretty for your user:

Defaults:username !requiretty
share|improve this answer

your best bet (and probably most reliable) is to give the user correct sudo access to the commands needed and with the NOPASSWD flag

eg if your user running the script is bob, to reload nginx via sudo without a password the syntax of /etc/sudoers would be:

bob ALL= NOPASSWD: /etc/init.d/nginx reload

your script above would effectively be changed to:

sudo -u my_user hg clone ssh://fabric_tasks_repo /home/my_user/fabric
sudo -u my_user /usr/local/bin/fab -f /home/my_user/fabric/fabfile.py `ec2-describe-tags   --filter "resource-type=instance"   --filter "resource-id=$(ec2-metadata -i | cut -d ' ' -f2)"   --filter "key=type" | cut -f5` > /home/my_user/fabric.log 2>&1 &

you may or may not need to escape your quotes but i removed them for clarity

so your sudoers file has the user you run the script as, but it can run those commands as the user my_user

share|improve this answer
I already have my user in a group which sudoers allow NOPASSWD. I don't know if I made it clear in my question, the problem is that this script runs in cloud-init (rc.level-like) and when it tries to run any sudo commands it'll get this message: sudo: Sorry, you must have a tty to run sudo so, there's no tty and my fabric tasks need to run some sudo commands. –  piva Oct 24 '11 at 21:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.