Ubuntu – How To Give Execute Permissions To Root User File In Ubuntu

file-permissionsUbuntu

I have been following this iptables guide online and came across a problem where I tried to give myself execute permissions to a file that is owned by root and stored in root's home folder. Here is the online document as a reference:
iptables guide

It says:

Make sure you can execute the script

$ chmod +x /root/fw.stop

And then:

You can run the script

$ /root/fw.stop

When I entered the command "chmod +x /root/fw.stop", I received an error "chmod: cannot access `/root/fw.stop': Permission denied".

Obviously, if I want to run the script, I can do so by simply typing "sudo /root/fw.stop", but I'm trying to learn; did the writer of the guide just make a mistake with his syntax that he listed, or am I doing something wrong?

I tried "sudo bash -c "chmod +x /root/fw.stop"" and then tried to run the script again as myself with the command "/root/fw.stop" but I received the error "bash: /root/fw.stop: Permission denied".

Best Answer

The author made a number of mistakes.

  1. The script should be created, chmod'd and executed as root.
  2. The script is missing the shebang to tell it what interpreter to use.

This is what you should be doing:

As root (sudo -s) create the file /root/fw.stop:

$ sudo -s
Enter foo's password:
# nano -w /root/fw.stop

Then enter the following script (note the change in the first line):

#!/bin/sh
echo "Stopping firewall and allowing everyone..."
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

Then you can chmod it (still as root):

# chmod +x /root/fw.stop

Then you can run it (yes, still as root):

# /root/fw.stop

If you want to run it as anyone other than root then you should use sudo (sudo /root/fw.stop). The setuid bit is possible but a serious security hole as it would allow anyone to run the script.

Related Question