-3

I have this code in a linux executable file to start atom from there:

#! /bin/bash

sudo atom

I wanted to include sudo password after that lines of code, so the program will run automatically.

viktaur
  • 31
  • 1
  • 4
  • [`sudo -S`](https://linux.die.net/man/8/sudo). – KamilCuk Apr 18 '19 at 08:47
  • 1
    That said, *don't do it this way!* (bad idea). Anyone who can read the file can read the sudo password. Instead run `visudo` and set the appropriate permissions for this user to run `sudo` without a password (you will probably have to add the user to the `wheel` (or lately the `sudo`) group as well. – David C. Rankin Apr 18 '19 at 08:47
  • 1
    Possible duplicate of [Use sudo with password as parameter](https://stackoverflow.com/questions/11955298/use-sudo-with-password-as-parameter) – Tsyvarev Apr 18 '19 at 10:47

3 Answers3

2

I wouldn't suggest hardcoding a password into your script as it carries a security risk.

Strictly speaking you can by doing:

echo "yourpassword" | sudo -S <command>

The -S flag will read the password from stdin.

There is another better way for you to allow password-less sudo commands by modifying your sudoers file.

Esteban Garcia
  • 2,171
  • 16
  • 24
0

Why you not use sudoers? Try running atom without password.

Edit /etc/sudoers via visudo:

visudo -f /etc/sudoers

Put new line and save:

youuser ALL=(ALL:ALL) NOPASSWD: atom

do not forget to replace youuser

qwsj
  • 426
  • 3
  • 13
0

You dont need this. You need to use visudo:

sudo visudo

Then add line like this:

username ALL=(root) NOPASSWD: /path/to/script.sh

And then run your script using sudo script.sh without password

jww
  • 97,681
  • 90
  • 411
  • 885
struckoff
  • 41
  • 7