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 am trying to run a script on remote server as a root user. I have this line in my shell script.

cat /home/myuser/create_user_updated.sh |   ssh myuser@$myip "sudo sh"

I get the following error.

sudo: sorry, you must have a tty to run sudo

The script "create_user_updated.sh" works ok if copy the file to remote server and execute it there locally. But it does not work from remote server.

share|improve this question

2 Answers 2

up vote 8 down vote accepted

You have two options:

  1. You can disable tty requirements by sudo: run visudo and either remove "requiretty" option globally if it is set, or on per-user basis:

    Defaults:username !requiretty

  2. You can force ssh to allocate pseudo-tty:

    ssh -t -t myuser@$myip "sudo sh"

    Note the double -t options, both are needed. Moreover, you need to add exit as a last line of your create_user_updated.sh script. Otherwise, ssh session might not end properly (because of the forced pseudo-tty creation).

Please use option #2 only if you don't have root rights on the target machine, and cannot change sudo config. #1 is much more clean solution. In my opinion, enforcing tty via sudo does very little from security standpoint.

share|improve this answer
    
After running the script, I get some obscure prompt like sh3.22>> I have to hit ctrl+C to get the normal prompt. How do I avoid this since it must be because of -t -t option. –  shantanuo Oct 13 '11 at 4:16
    
I updated my answer with the solution to this. Yes, this is because of -t -t option. –  haimg Oct 13 '11 at 15:05
    
Don't forget that you can constrain the defaults declaration to not only a single username but to any spec, including command specs. This allows you to restrict the changes to only specific commands, groups of users or other spec. –  Mark Carey Apr 25 '13 at 19:48

Use visudo to edit the /etc/sudoers file and insert the below line:

Defaults:myuser !requiretty
share|improve this answer

Your Answer

 
discard

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.