Bypass ‘sudo’ : Execute root commands without entering password in Linux

Keywords: Bypass sudo, execute root commands without ‘sudo’ 

 

Many times we encounter a scenario where we have to enter password to execute some commands. Many Linux commands needs root previlege to execute them. If you are trying to execute a command using ‘sudo’ in a script or by a function using any programming language, then you’ll encounter this problem. If you don’t want to enter your password every time while running your program or script then you can follow one of the following options:

1. Bypass ‘sudo’, provide ‘password’ automatically

(a). Create a text file and save your password in it.

(b). Now excute your command by following this syntex:

$ sudo -S <command> < <path-of-passwordfile>

For example : I crated a file named ‘password.conf’ and stored it in my home directory. Now I wan to execute the ‘ntpdate’ command to syncronise my system with NTP server, then I’ll execute the command like this:

sudo -S ntpdate pool.ntp.org < /home/password.conf

This will automatically pass the password form your file to command prompt.

If you are executing the command form a program written in C,Java etc then execute above command from their system function. e.g in C using system() etc

system(“sudo -S ntpdate pool.ntp.org < /home/password.conf”);

2. Bypass ‘sudo’, Edit ‘sudoers’ file

The ‘sudoers’ file has the following syntax:

<USER><HOSTNAME>=<COMMAND>

Where

  • USER: Name of the user like roor,admin or your name
  • HOSTNAME: Where command is allowed to run. It is the hostname of the system where this rule applies.
  • COMMAND: A simple filename allows the user to run the command with any arguments. You may also specify command line arguments (including wildcards). Alternately, you can specify “” to indicate that the command may only be run without command line arguments.

For example you have apache intsalled on yourt machine and want a user let’s “Techawarey” to grant permission to restart it without password prompt. Then you have to edit “/etc/sudoers” file.

$ sudo visudo

You have to use “sudo” before it if you are not a root user.

Now appen the following line in this:

Techawarey dbserver=/etc/init.d/apache-perl restart

Now user “Techawarey” can restart the apache server by “sudo /etc/init.d/apache-perl restart”.

Please Like and Share if you find it Useful… 🙂

Leave a comment....