can we write a simple bash script to automatically login as root user

15,729

Solution 1

You can configure sudo to require a password only every so many minutes. The default is 5 minutes. Read the sudoers man page and scroll down to this:

    timestamp_timeout
               Number of minutes that can elapse before sudo will ask
               for a passwd again.  The timeout may include a
               fractional component if minute granularity is
               insufficient, for example 2.5.  The default is 5.  Set
               this to 0 to always prompt for a password.  If set to a
               value less than 0 the user's timestamp will never
               expire.  This can be used to allow users to create or
               delete their own timestamps via sudo -v and sudo -k
               respectively.

Solution 2

You can pipe the echo'd password into a command. Try something like this:

echo myPassword | sudo -S 

You can see come more info on this here.

Question is, do you REALLY want your password in a shell script file? (just emphasizing that its a terrible idea)

Solution 3

Is there a reason that you can't sudo su - to just become the root user instead of prepending all of your commands with sudo blah?

Solution 4

just change ownership of the script to root & set SUID-Bit in user the permissions

chmod u=rws g+x o+x script123

the script will run as root for every user

Share:
15,729
forum.test17
Author by

forum.test17

Updated on June 17, 2022

Comments

  • forum.test17
    forum.test17 about 2 years

    I usually have to login in 20 to 50 times daily as a super user, typing the long password again and again..

    i have just created a simple bash script

    #!/bin/bash
    
    sudo -s
    echo password
    

    ./test

    output root@localhost: password when i execute it, it works like charm... but it shows my password on the screen.....

    do some one have any other best solution...... for this small problem.......

    i hope this is not all the solution in security standard...... can we have any other solution with out exposing my password.....

  • Diego Torres Milano
    Diego Torres Milano over 12 years
    Definitely don do this, unless you want everybody could read root password using ps
  • SuperTron
    SuperTron over 12 years
    Agreed, was just answering the question :P