Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello, i want to run a script as a regular user, and in that script, automatically become root so that the script can execute a command.
I'm using DSL Linux.
I tried putting sudo and sudo su before the script, but that didn't work.
This is an automated script, so i don't have the luxury of just typing in sudo root_only_script.sh from a bash window.
The reason for this is because i have a samba share setup on the server but when files are moved to the share from windows computers, they require root permission in order to be changed. So i want to have a script that can change those permissions via a chmod 0777 -R /home/dsl/
The samba share is a no password, guests ok share. If there is a better solution than becoming root, that is welcome too.
You need to use sudo with the nopasswd option, since you don't want sudo to prompt for a password. In addition, you need sudo configured to allow use without a tty (a script).
Run visudo, and make sure the following statement has the exclamation point:
Defaults !requiretty
That will now allow sudo from a script.
Add an entry that will allow the script to be executed by the user. For example:
You don't want to directly edit the sudoers file. The reason is that sudo is very picky - The slightest syntax error will prevent all sudos from working. The visudo command runs a syntax check before changing the file, to prevent such an error.
If you have access to this samba server, you could call your script as a root cron job. Then the script will be running as root anyway. You will need to make sure that your script doesn't print our expect input, so you may need to add redirection to /dev/null for each command.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.