View Full Version : [ubuntu] How do I run a bash script as root?
cha0s619
October 7th, 2011, 12:24 PM
Hi,
I have this very strange problem where Network Manager overwrites /etc/resolv.conf every time I connect to any network. This is very annoying because it overwrites the setting for google dns with my ISP DNS which is censored. I decided to solve this problem by having the following script run automatically every 5 minutes using cron:
#!/bin/bash
# Purpose: Automatically overwrite /etc/resolv.conf with google dns settings
# Date: Friday October 7 2011
sudo echo "# Generated by Network Manager - Edited by fixdns.sh" > /etc/resolv.conf
sudo echo "# nameserver 192.168.0.1" >> /etc/resolv.conf
sudo echo "nameserver 8.8.8.8" >> /etc/resolv.conf
#END
Unfortunately I get an error saying permission denied because I need root permissions to edit that file. How can I get this script to work without being logged in as root?
Thanks
Basher101
October 7th, 2011, 12:25 PM
Rightclick on the file, then choose Protperties, then select at the top of the window that will pop up Permissions and set them to root.
IWantFroyo
October 7th, 2011, 12:27 PM
chmod +x filename.sh
cha0s619
October 7th, 2011, 12:47 PM
chmod +x filename.sh
No. The script is already executable. The problem is that it is running with regular user permissions. I need it to run with root permissions while I'm logged in as a regular user.
Rightclick on the file, then choose Protperties, then select at the top of the window that will pop up Permissions and set them to root.
I'm not seeing on option for setting permissions to root. All I see is changing read, write, execute permissions for user, group and others.
Basher101
October 7th, 2011, 12:51 PM
my bad...my ubuntu is in german so i was just translating <.<
just check the tab for Group and/or execute permissions. you can change it there i am 100% sure of it. If not, then the chmod command must be used.
WorMzy
October 7th, 2011, 01:12 PM
Run the script as
sudo script.sh
and everything in the script will be run as root. You can remove all instances of "sudo" from the script itself, those are unnecessary.
If you're curious, the reason why you're getting permission denied errors, is because of the way that redirection (>) works. The first part works as root, but the part after the > is run as the user running the script. If you need to redirect text to a root-owned file, use tee.
e.g.
echo "this works like >" | sudo tee file123
echo "this works like >>" | sudo tee -a file123
cha0s619
October 7th, 2011, 01:54 PM
my bad...my ubuntu is in german so i was just translating <.<
just check the tab for Group and/or execute permissions. you can change it there i am 100% sure of it. If not, then the chmod command must be used.
I am confused. Please clarify your instructions. Here is a screenshot with my menu and current permissions:
Screenshot.png: http://imageupload.org/thumb/thumb_116174.png (http://imageupload.org/?d=4B8CDD7D1)
3rdalbum
October 7th, 2011, 02:00 PM
Get rid of all instances of 'sudo' from it.
sudo chown root <filename>
sudo chmod u+s <filename>
This will "setuid" the file, which means that whenever the script is run it will run with the permissions of its owner - which in this case is "root".
cha0s619
October 7th, 2011, 02:25 PM
Get rid of all instances of 'sudo' from it.
sudo chown root <filename>
sudo chmod u+s <filename>This will "setuid" the file, which means that whenever the script is run it will run with the permissions of its owner - which in this case is "root".
I saw this elsewhere as well but for some strange reason it isn't working. :s
Anyway, I found a work around. I changed the file permissions to read only and I made the file immutable so that Network Manager can't delete it.
sudo chmod 444 /etc/resolv.conf
sudo chattr +i /etc/resolv.conf
Run the script as
sudo script.shand everything in the script will be run as root. You can remove all instances of "sudo" from the script itself, those are unnecessary.
If you're curious, the reason why you're getting permission denied errors, is because of the way that redirection (>) works. The first part works as root, but the part after the > is run as the user running the script. If you need to redirect text to a root-owned file, use tee.
e.g.
echo "this works like >" | sudo tee file123
echo "this works like >>" | sudo tee -a file123
Thanks alot, I didn't know about tee.
Thanks to everyone who responded
QLee
October 7th, 2011, 04:14 PM
I have this very strange problem where Network Manager overwrites /etc/resolv.conf every time I connect to any network.
This is not a strange problem, it is the expected behaviour for NetworkManager. You have it set to DHCP both the IP address and DNS and thus it does that and puts the DNS values in resolv.conf.
This is very annoying because it overwrites the setting for google dns with my ISP DNS which is censored. I decided to solve this problem by having the following script run automatically every 5 minutes using cron:
In my opinion, it would probably be better to correct the behaviour by editing the connection information in NetworkManager. On the IPV4 Settings tab in the property sheet for the connection there are fields for you to set your preferred Domain Name Servers for the connection. No problem if you choose to use your script instead, I just wanted to mention a different method that many might find easier.
CharlesA
October 7th, 2011, 04:33 PM
In my opinion, it would probably be better to correct the behaviour by editing the connection information in NetworkManager. On the IPV4 Settings tab in the property sheet for the connection there are fields for you to set your preferred Domain Name Servers for the connection. No problem if you choose to use your script instead, I just wanted to mention a different method that many might find easier.
+1. I believe you can set static DNS entries from network manager, but I haven't really used it for that since I have my DHCP server handling DNS servers and whatnot.
sisco311
October 7th, 2011, 04:37 PM
Get rid of all instances of 'sudo' from it.
sudo chown root <filename>
sudo chmod u+s <filename>
This will "setuid" the file, which means that whenever the script is run it will run with the permissions of its owner - which in this case is "root".
For security reasons the setiud/segid bit doesn't work on scripts.
ssbrshei
October 16th, 2011, 01:55 PM
There are several ways to achieve what you want. One ways is to get ride of all the sudo in your script and execute the script as root's cron job. (sudo crontab -e) Or you can just put your contents to a file, say /etc/resolv.conf.mine, and then simply put 'cp -p /etc/resolv.conf.mine /etc/resolv.conf' as root's cron job. Good luck!.
Powered by vBulletin® Version 4.2.2 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.