hossain
hossain

Reputation: 23

Automatically input root password using bash script

I use the following bash script to run my local webserver on Fedora 29. After executing the script root password needs to be typed two times.

#!/bin/bash
systemctl enable httpd.service
systemctl start httpd.service
systemctl start mariadb.service

How can I save the root password inside above script and pass the same while prompted for?

Thanks in advance for your co-operation.

Upvotes: 0

Views: 1619

Answers (2)

hyperTrashPanda
hyperTrashPanda

Reputation: 868

This is generally a bad idea, for many reasons. You shouldn't pass around passwords, let alone in plaintext. Also allowing passwordless execution of commands, let alone as root can be disastrous.
The suggestion by Romeo Ninov to configure systemctl to be executed without any prompts is absolutely forbidden when other people will have access to your system, and downright a bad practice even if you're working by yourself.

What's a better idea is to add a new entry to yout sudoers file, allowing you to call this script and its content using sudo, without prompting for your root password.

Check out this post on how achieve this.

Upvotes: 1

Romeo Ninov
Romeo Ninov

Reputation: 7235

Much better is to use sudo command and configure your user for passwordless execution of systemctl command

#!/bin/bash
sudo systemctl enable httpd.service
sudo systemctl start httpd.service
sudo systemctl start mariadb.service

Upvotes: 1

Related Questions