Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

The main question:

Is there a way I can run 'completely' one of my script when ubuntu's desktop appears no matter if root , administrator, desktop user or an unprivileged user logged in?

What does the script do?

The script mounts a partition, looks for a file in that partition and finally on the basis of that file a decision of copying a partition to another partition is made. That copying is done via

dd if=/dev/sda2 of=/dev/sda5

When does the script run finely?

Script runs smoothly when I run it from the terminal by

sudo ./my_copying_script

This command asks me for the password of currently logged in user. I enter the password and the script starts working.

When does the script NOT run finely?

I want to run the script at startup. I set it a startup program by using the Startup Applications utility of Ubuntu. Script ran at startup but exited at the dd command returing following error:

dd: opening '/dev/sda2': Permission denied 

On edk's suggestion I set the owner of my_copying_script as root and set the SUID. Now the permissions of my_copying_script are (-rwsr-sr-x). edk's point of view was that once I set the suid, the startup program will run with the permissions of its owner. I did that but the same /dev/sda2 permission denied error came up.

I then prefixed the dd with sudo as mentioned below

 sudo dd if=/dev/sda2 of=/dev/sda5

but this returned following error:

sudo: no tty present and no askpass program specified
share|improve this question
what distribution are you using? what desktop? could you mount the drive at system startup (eg, from /etc/fstab) instead? –  quack quixote Apr 14 '10 at 9:39
I am using Ubuntu 9.10 with GNOME. Yes, I can mount the drive using /etc/fstab but that will make that partition available for all users which I don't want. I want only root to have access to that partition. Any thoughts on this? –  usajbalt Apr 14 '10 at 9:45
@Usman mount it with the right permissions. –  BeowulfOF Apr 14 '10 at 12:18
If I mount /mnt such that give only root all permissions then my startup program cannot access the data in /mnt. –  usajbalt Apr 15 '10 at 8:02
Nice editing. +1 –  Gnoupi Apr 15 '10 at 12:27

5 Answers 5

Usman, I think you have at least two options I already mentioned.

An easiest way

Make a set-UID binary that will(check who started it & allow usage if parent process owner can run it) run prepared script and exit. Put into startup in your desktop manager, that's all. Just be warned it is not the best way and it needs a bit of C coding. Basically you need to make a code like this sample:

int main(){
    system("/bin/sh /root/bin/mounts.sh");

Be aware that this is a hack, not solution, really. And be sure to use root:allowed_group and 4750 rights on binary if you don't need it to be run by everyone. Remember to add all users which may start this up to allowed_group(pick your name).

A good way

Other, much better way is to make a sort of daemon, spawned @ system boot(say, from init-scripts), but I am unsure how exactly are you going to detect when new X session is started, I wasn't into that topic yet, I can't give any clear advices here.

share|improve this answer
Sorry edK but I think I did not understand your first suggestion. I have set the suid of my program using chmod +s command. Now, what will the C code do? –  usajbalt Apr 15 '10 at 12:44
Like the one I gave. You can play with this example and modes 4755 & 6755: codepad.org/zkCULq3q –  edk Apr 15 '10 at 13:04
Clicked the 'this is a great comment' mistakenly. :P Okay here is what I understand. Correct me if I am wrong. You want me to write a C program which will setuid(0) and then run my script via system("/home/namsu/Desktop/my_copying_script") and finally I set the executable of this C code to run at startup. Right? :) –  usajbalt Apr 15 '10 at 13:19
I thought it was stated quite clear in my answer. –  edk Apr 15 '10 at 13:41

Set correct permissions on mounted drive path then and you're done.

share|improve this answer
Sorry but that will not solve the problem. –  usajbalt Apr 14 '10 at 11:47
then I can think of nothing else than creating a daemon-script that will monitor X-related processes spawn OR you may wish to use suid binary that will check who is a caller and then do something you wish, but I really don't see a reason to do so, if you only want to make drive accessible by root. if you just want a script to be run as root when logging in, then suid-binary is the easiest(but dangerous!) way to do so. In fact, it's more of hack than of actual solution. –  edk Apr 14 '10 at 11:58

sudo will only run a program as a different user if one of these 3 conditions has been met (as far as passwords are concerned):

  1. the NOPASSWD option is specified
  2. the user entered the correct target password
  3. the user entered the correct source password

Since options 2 and 3 require a TTY (sudo won't read from a pipe) it won't run if it can't find one. Check your script if at any point you're running a script remotely using ssh, as it's possible that it won't allocate a TTY for a non-interactive remote command.

Quoted from: Here

share|improve this answer
I am not running my script using ssh. –  usajbalt Apr 14 '10 at 9:37
@Usman: ssh is not the only place where sudo will complain about being run noninteractively. it will also complain when run from a startup script, as there is no TTY attached to the startup process. –  quack quixote Apr 15 '10 at 10:32
And attaching TTY to my startup script is not possible, right? –  usajbalt Apr 15 '10 at 11:35

Add the entry to /etc/fstab, use chown and chmod to modify permissions to only give root access to the mounted partition.

sudo chown -R root:root /mnt
sudo chmod -R 770 /mnt
share|improve this answer
I mounted the partition by adding entry to /etc/fstab and set it's permissions as you specified but that did not work because now my startup script cannot access data in /mnt when I log as a normal user (not root). My normal user is not getting access to /mnt which is very fine to me but my program must get access to /mnt. I am still stuck. :( –  usajbalt Apr 15 '10 at 9:24
In your comment you said "I want only root to have access to that partition" If you want others to have read-only access replace the 770 with 774 in the chmod command. –  Mark Robinson Apr 15 '10 at 12:29

A possible solution will be to add your user and the command he is trying to invoke to /etc/sudoers with NOPASSWD parameter. Cut from /etc/sudoers:

# Uncomment to allow members of group sudo to not need a password
# (Note that later entries override this, so you might need to move
# it further down)

So maybe you will need something like:

myuser ALL=NOPASSWD: mount

I'm not very sure about the exact syntaxis, you'd better google for it.

EDIT: If you are trying to mount a network drive, adding a record to /etc/fstab probably will not work, because network services are not loaded at time fstab is executed.

share|improve this answer

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.