I've been curious ever since I've started bash scripting in Ubuntu, and I have been wondering if there was a way to log in as root or run a sudo command without physically typing the password manually when it prompts you in the terminal.

Is it possible to hard-code the password into a line, such as "echo mypassword" and have sudo read through the echo command instead of typing for myself?

I've looked around that you could, if you had set the root to have no password, which is something I do not want.

I've just been playing around with bash and seeing if I could login as root and run a command in root through the bash script only:

#!/bin/bash
echo mypassword | sudo apt-get install frozen-bubble

This is just out of my pure curiosity, and was just wondering how to implement it if it was possible. Thanks.

4 Answers
4

You should not hardcode your password. If you want use sudo without password, you must edit sudoers file. To edit sudoers, type command

$ sudo visudo

Visudo uses vi as editor. If you are not familiar in vi, you could set environment variable VISUAL or EDITOR to you favourite editor, eg.VISUAL=nano. The following environment variables are used only if visudo was configured with the --with-env-editor