Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Log into root from script?

  1. #1

    Default Log into root from script?

    hey! I use pm-suspend [aliased as "ram"] quite a lot on my laptop. It's annoying to have to type
    Code:
    su
    Password: 
    ram
    every time I want to do so. So, is there a way to give su or sudo the password via a parameter? like maybe
    Code:
    su --password=********** -c "ram"
    that would be nice, but it doesn't work. I even tried
    Code:
    echo "********" | su
    but that also doesn't work
    I'd appreciate any help, thanks!
    ~Matt

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Log into root from script?

    Use sudo to give yourself permission to execute that command (and only that commnad) as root. You can then make a one word alias for the command. Look at some previous posts re sudo on this forum for examples. Also the man page for sudoers.

  3. #3

    Default Re: Log into root from script?

    hmm, I'm not sure how to do that, I typed visudo,
    and put in
    %users ALL=/usr/sbin/pm-suspend
    but that doesn't seem to work. So, is there no way to log into root from a script? I mean, it has to be possible! (?)

  4. #4

    Default Re: Log into root from script?

    That should work unless you forgot to add your userid to the %users
    Code:
    # User alias specification
    User_Alias    USERS = your_userid
    #
    # or 
    your_userid ALL = /usr/sbin/pm-suspend  
    #
    # also with caution 
    your_userid ALL =  NOPASSWD: /usr/sbin/pm-suspend  
    #
    # preferred with caution 
    your_userid localhost =  NOPASSWD: /usr/sbin/pm-suspend
    Rambling.
    Box 1: OpenSuse 11.1/Win7 | Linux 2.6.27 Gnome | AMD 64 X2 6000+ | nVidia 8600GT | 2GB RAM
    Box 2: OpenSuse 11.2 | Linux 2.6.31 Gnome | AMD 64 3000+ | ATI X800 Pro | 1GB RAM
    Box 3: Win7 Premium Home | Intel P4 3.0Gz | ATI AIW 2006 | 2GB RAM

  5. #5
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Log into root from script?

    And remember you have to prefix the command with sudo, i.e.

    sudo pm-suspend

    But you can make an alias of that so that you can forget typing sudo.

  6. #6

    Default Re: Log into root from script?

    Quote Originally Posted by tararpharazon View Post
    That should work unless you forgot to add your userid to the %users
    Code:
    # also with caution 
    your_userid ALL =  NOPASSWD: /usr/sbin/pm-suspend
    Rambling.
    Thanks! That works.
    But, ok, so there's no way to switch to root from a script? [Without prompting the user to type the password]? hm.. I thought there would be a way, Somehow..

  7. #7

    Default Re: Log into root from script?

    Quote Originally Posted by mvmacd View Post
    Thanks! That works.
    But, ok, so there's no way to switch to root from a script? [Without prompting the user to type the password]? hm.. I thought there would be a way, Somehow..
    I think most would run script pm_suspend_alias, or sudo runscript.sh as ken_yap suggested.
    Box 1: OpenSuse 11.1/Win7 | Linux 2.6.27 Gnome | AMD 64 X2 6000+ | nVidia 8600GT | 2GB RAM
    Box 2: OpenSuse 11.2 | Linux 2.6.31 Gnome | AMD 64 3000+ | ATI X800 Pro | 1GB RAM
    Box 3: Win7 Premium Home | Intel P4 3.0Gz | ATI AIW 2006 | 2GB RAM

  8. #8
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Log into root from script?

    Quote Originally Posted by mvmacd View Post
    Thanks! That works.
    But, ok, so there's no way to switch to root from a script? [Without prompting the user to type the password]? hm.. I thought there would be a way, Somehow..
    The NOPASSWD should have allowed execution without being asked for a password.

  9. #9

    Default Re: Log into root from script?

    Quote Originally Posted by ken_yap View Post
    The NOPASSWD should have allowed execution without being asked for a password.
    Yes.

    IIUC, OP still wanted to know how to invoke a su command from within his script, which is simply adding that command into his script correct?
    Box 1: OpenSuse 11.1/Win7 | Linux 2.6.27 Gnome | AMD 64 X2 6000+ | nVidia 8600GT | 2GB RAM
    Box 2: OpenSuse 11.2 | Linux 2.6.31 Gnome | AMD 64 3000+ | ATI X800 Pro | 1GB RAM
    Box 3: Win7 Premium Home | Intel P4 3.0Gz | ATI AIW 2006 | 2GB RAM

  10. #10
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Log into root from script?

    If you mean if there is a command that you put in the middle of a script and then all lines after that are executed as root, no it doesn't work that way. Rootness is an attribute of the process. So you have to start another process whose owner id is root. So it would be a different script that you run. (Roughly speaking. One can think up of tricks to fold the commands into one script, but let's not complicate things.)

    But it's not a good idea to give carte blanche to run a whole bunch of commands as root. The best practice is that the minimum of root privileged commands should be used.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •