17

I need use execute a command inside of a script in a Run Script build phase in Xcode 4 using sudo. However, the compiler complains:

sudo: no tty present and no askpass program specified

Anyone have a clever solution for this problem?

3
  • I recommend stating what you're using sudo access to do. That may open up broader alternatives. Commented Jul 23, 2011 at 21:28
  • Copy a file from one location to another and it needs to be done with root access.
    – ericg
    Commented Jul 23, 2011 at 22:16
  • I have the same issue and I have set ALL=NOPASSWD for that script. In fact if I execute it from console it does not ask for password
    – hithwen
    Commented Aug 28, 2012 at 7:43

7 Answers 7

13

One solution is to place the sudo password in an executable shell script like the following:

#!/bin/bash
echo thesudopassword

This shell script might be called password.sh

Then, setup the environment variable SUDO_ASKPASS=password.sh

Once this is setup, the -A option can be passed to sudo. This option uses the ASKPASS program to obtain the sudo password. The ASKPASS program need only write the password to stdout.

So, for example,

sudo -A ditto -V /tmp/testserver.dst /

This is obviously a rather insecure solution, but it does work.

0
7

Two ideas that haven't been suggested yet, both of which are probably better/safer than the currently accepted answer:

First option would be to put the part of the script that needs to be run as root in a script file (.sh or something), and then make it setuid as root: chmod go-w,+sx scriptfile, sudo chown root scriptfile. This means that script will automatically run as root, which avoids you needing to authenticate to run it (just to change it). As long as its operation isn't subject to user input, this should be quite safe. (Of course, if you make a script that takes an input argument and deletes it or runs it, or does most anything else with it, that would not be safe.)

Second option would be to use applescript (possibly via osascript). Applescript allows you to do shell script "sudo command goes here" with administrator privileges, which will pop up a graphical dialog asking for a password.

The first of these options would be good for an automated environment, though it might not deal well with (for example) being checked into an SCM, or being sent to another user. The second option would work better with that, but requires a password input every time, so doesn't work as well for an automated build script.

5
  • 1
    Setting the uid as root is a good idea. I am not so sure I like the applescript answer. About the last thing I want is to be asked for a password every time I need to build.
    – ericg
    Commented Nov 1, 2013 at 12:24
  • @ericgorr True, though one might hope that you wouldn't generally need root for your run-of-the-mill builds (since most testing should be able to be done in your home directory), but just once in a while for a test that things work when installed in system locations or the like. Of course, I have no idea what you're doing here.
    – arcticmac
    Commented Nov 2, 2013 at 1:19
  • After chowning, when saving my shell file in AppCode the permission is reset to my user. I'm using the ASKPASS approach now.
    – vaughan
    Commented Mar 3, 2014 at 7:31
  • @articmac, e.g. in El Capitan (at least) kextload will not load a kernel extension unless it's chown'd root:wheel, and Xcode cannot modify the build product unless it's not chown'd root:wheel, so it's necessary to run a script as root both pre- and post-build.
    – user2975337
    Commented Aug 8, 2016 at 23:44
  • Unfortunately does not work in El Capitan (no suid shell scripts). Could probably launch the script from a suid binary, if it won't fit into system("...") calls, however this may not preserve the Xcode environment vars. For that a path-restricted sudoers entry works, but is less secure than your answer
    – user2975337
    Commented Aug 8, 2016 at 23:59
2

Another solution to this problem is to modify sudoers file and add your account to it and state that you should never be asked for the sudo password. To accomplish this is fairly straightforward:

run:

sudo visudo

In the User privilege specification section add a line that looks like

youraccountname ALL=(ALL) NOPASSWD: ALL

Of course, this can be a dangerous thing to do, so be careful. I would suggest reading the man page for sudoers and visudo before going this route.

1
  • 2
    Would be better to use youraccountname ALL=(ALL) NOPASSWD:SETENV: /path/to/script, which restricts the no-password allowance to that specific path, and then use sudo -E /the/script in Xcode to transfer the environment vars (e.g. $BUILT_PRODUCTS_DIR, etc.)
    – user2975337
    Commented Aug 9, 2016 at 0:42
1

After much searching I found the following solution.

https://forum.juce.com/t/build-script-for-automatically-moving-built-aus-into-components-folder-xcode/13112

Summary

  1. Create a keychain and store your admin password in the keychain
  2. Create a script which uses /usr/bin/security to access the password In your run script,
  3. Set the ASK_PASS env variable and use the -A option with sudo
1

You can either run commands directly as a administrator with the following (changing echo YourCommandHere > /tmp/hello to your command):

osascript -e 'do shell script "sudo echo YourCommandHere > /tmp/hello " with administrator privileges'

Or run a script in your source directory using:

osascript -e 'do shell script "bash -x $SOURCE_ROOT/MyAdminScript.sh 1>/tmp/build-log 2>/tmp/build-log.err" with administrator privileges'

This runs the script and logs it output to /tmp/build-log and /tmp/build-log.err

For useful variables in the script see https://help.apple.com/xcode/mac/8.0/#/itcaec37c2a6

-1

You can also execute XCode giving it the project as parameter from the Terminal using sudo like this:

sudo /Developer/Applications/Xcode.app/Contents/MacOS/Xcode /path/to/your/project.xcodeproj

This is the easiest solution I could think of, but there may be some drawbacks, since you would be executing XCode as root.

2
  • 2
    Ugh. Technically that works, but I recommend against it, especially considering how buggy Xcode 4 is (based on how often it crashes...imagine the havoc it could wreak with root privileges).
    – Richard
    Commented Sep 7, 2011 at 19:58
  • 1
    This screws over any future changes to the project since you would ALWAYS have to run Xcode as root thereafter.
    – N_A
    Commented May 4, 2012 at 17:14
-4

No need to write your sudo password anywhere. Just open a terminal window and type

$ sudo echo "hello"

Once you've typed your password, it will be good for a while - not sure how long - and the shell spawned by Xcode will inherit this permission.

If you get the "no tty present" message again later, just repeat the procedure

1
  • 2
    Works, but isn't useful in an automated environment.
    – Jasarien
    Commented Jan 7, 2012 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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