Shell script to become root

7,293

You can write a script using expect tool.

In Redhat, expect package comes by default. But in Ubuntu you need to install it separately.

You can check this by using commands:

$ rpm -qa | grep expect for Redhat

$ dpkg -l expect for Ubuntu

The following script will do your work:

#!/usr/bin/expect
spawn su -
expect "Password: "
send "password\r"
interact
Share:
7,293

Related videos on Youtube

Author by

Rahul Kumar Dubey

B-tech 3rd year student on MNNIT allahabad

Updated on September 18, 2022

Comments

  • Rahul Kumar Dubey 4 months

    How can we write a shell script to become root? That is I donot want to input the password at prompt it should be within the script itself. I was trying to make but failed to do so.Is it possible, if Yes please Explain.

    • Admin
      Admin about 10 years
      Does it need to be a shell script? I don't see how that would be possible, but you could set it up so sudo su or sudo bash works without a password
    • Admin
      Admin about 10 years
      @MichaelMrozek sudo -i is preferred. It requires permissions to run sudo bash.
  • jordanm
    jordanm about 10 years
    setuid does not work on shell scripts unless you setuid the shell binary (not recommended).