Find the answer to your Linux question:
Results 1 to 6 of 6
So I just got into Bash and I'm having some fun with it. So I'm making a script that just simply shutsdown the computer. The problem I'm having, with the ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Posts
    4

    Question Bash: how to enter password for sudo

    So I just got into Bash and I'm having some fun with it. So I'm making a script that just simply shutsdown the computer. The problem I'm having, with the code presented below, is when the script is ran it ask for a password. So what am I doing wrong here?

    Code:
    #!/bin/sh
    echo "shutting down now!"
    sudo PASSWORD
    shutdown -h now

  2. #2
    Linux Guru reed9's Avatar
    Join Date
    Feb 2009
    Location
    Boston, MA
    Posts
    4,651
    I know almost nothing about bash scripts, but I can say with fair confidence that sudo PASSWORD isn't going to work. Just like if you typed that into a terminal, the next command wouldn't run with elevated privileges.

    It should be
    Code:
    sudo shutdown -h now
    or just
    Code:
    sudo halt
    If you want a user to be able to run a command with elevated privileges and without being asked for a password, you need to edit the /etc/sudoers file and specify it there.

    Sudoers Manual

  3. #3
    Just Joined!
    Join Date
    Aug 2009
    Posts
    4
    how would I go about editing the /etc/sudoers file?

  4. #4
    Linux Guru reed9's Avatar
    Join Date
    Feb 2009
    Location
    Boston, MA
    Posts
    4,651
    Code:
    su
    visudo
    Usually this defaults to using vi as the editor. Some distros have changed this default behavior to an easier editor like nano.

    If it uses vi, and you prefer nano, do
    Code:
    su
    EDITOR=nano visudo

  5. #5
    Just Joined!
    Join Date
    Aug 2009
    Posts
    4
    I'm sorry I didn't ask that right. I know how to edit the file I'm just not quite sure what code to use to get this going.

  6. #6
    Linux Guru reed9's Avatar
    Join Date
    Feb 2009
    Location
    Boston, MA
    Posts
    4,651
    Here is a pretty detailed example sudoers file to give you an idea of how to do things.
    http://www.gratisoft.us/sudo/sample.sudoers

    Basically, you'll need something like
    Code:
    username        ALL = NOPASSWD: /sbin/halt
    username could also be a group the user belongs to.

Posting Permissions

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