LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2008, 11:21 AM   #1
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 281

Rep: Reputation: 30
Edit sudoers by script


Is there any way to edit the sudoers file automatically through a script? I tried it with this line but it said "Warning: input is not from the terminal" and quit.
Code:
#!/bin/bash


echo "******" | sudo -S visudo 
echo "$USER ALL= NOPASSWD: /home/$USER/Desktop/scripts/cifs" >> /etc/sudoers
I realize how dangerous this is. I just wanted to try it out.
 
Old 05-27-2008, 01:49 PM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,902

Rep: Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590Reputation: 590
What about just using cat? I think vi can't be used non-interactively.
 
Old 05-27-2008, 01:58 PM   #3
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 281

Original Poster
Rep: Reputation: 30
What do you mean? Like using cat to place the information into the file itself?
 
Old 05-27-2008, 03:04 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 24,111

Rep: Reputation: 7098Reputation: 7098Reputation: 7098Reputation: 7098Reputation: 7098Reputation: 7098Reputation: 7098Reputation: 7098Reputation: 7098Reputation: 7098Reputation: 7098
Yes, but it'll have to be a two-step process.

cat "whatever you say" >> /etc/sudoers

That will put the line at the bottom (append) of the sudoers file. Or, you can do a:

cat /etc/sudoers | sed 's/your-user-name/your-user-name\,new-user-name/g' > newsudoers
mv /etc/sudoers old-sudoers
mv /etc/newsudoers /etc/sudoers

You'll have to then do a:

chmod 400 /etc/sudoers.

to make it work. Not sure what the perms need to be, but SUDO is very picky about what permissions are on that file. That's one of the things that VISUDO does, so take a good look at the existing sudoers file, before beginning. If the permissions are wrong, sudo won't work, and you'll have to su to root, and run visudo to fix it.

Please note that this is a very quick and dirty method, and that I wouldn't EVER do what you're trying to do.
 
Old 05-27-2008, 05:54 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,893

Rep: Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613
I agree it'd be nice to know why you'd want to do this, sounds like a possible security hole if you're not very careful.
 
Old 05-27-2008, 06:52 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594
Simple precaution is to *not* operate on the original but on a temporary copy of /etc/sudoers, then after you're done run 'visudo -q -c -s -f ' on the copy and let the exit value decide if it will be fine or fscked.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to edit "sudoers" file using visudo maniac matt Linux - Newbie 11 06-22-2013 08:38 AM
cannot edit /etc/sudoers unisol Ubuntu 13 06-18-2006 09:35 AM
How to edit "/etc/sudoers" lchxr Fedora 5 04-02-2006 11:29 AM
visudo lets me edit sudoers but not save ver 3.3.1 suguru MEPIS 4 12-19-2005 06:23 AM
I deleted /etc/sudoers and creates a new file call sudoers but now it doesnt for visu abefroman Linux - Software 1 11-10-2005 05:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration