1

How to change file permission in Linux so that only root user can access/open the file/dir in Linux.

EDIT

Doing

chmod 600 filename + sudo chown root:root filename  

changes it into only root accessible file. But I can see the file content only when I do $ sudo cat file.txt; but when I just click on it, it says file content can't be displayed. So, when I click on it, I want it to show me a dialog box or something, which prompts me to enter root password and then I can open the file. Is this possible ??

How do I change file permission back from root user to normal user A ?

EDIT 2

I can even do

sudo nautilus . 
and then supply root password to open file. But this is all from terminal. I want to open some kind of application/dialog box so that when I click that file/dir as normal user, it will prompt me to enter root password and not just tell me that you dont have proper file permission to open the file. I guess this must be possible, may be with some scripts. Any idea ?

Thanks :)

SOLVED here

5
  • I'm sorry, I don't know what "sth" means. As far as clicking, what application are you using? Jul 25, 2009 at 4:27
  • Sorry, sth = something. Jul 25, 2009 at 4:32
  • I dont know what you mean, but I usually open txt files using gedit and normal directories using nautilus, is this what you asked ?? Jul 25, 2009 at 4:34
  • I messed up while doing sudo visudo, I added timestamp_timeout = 0 and saved it. Now when I do sudo visudo, I get this >>> sudoers file: syntax error, line 7 <<< What now? What should I do to get back my original version ? Jul 25, 2009 at 13:43
  • I seriously need some help. I cant open any file now using root privilege. Is there any way to come out of this problem ?? Jul 25, 2009 at 13:54

2 Answers 2

8
sudo chown root:root filename
sudo chmod 600 filename

If it's an executable, use 700 for the permissions.

You can also specify the permissions in a symbolic style:

sudo chmod u=rw filename

For information on Nautilus Scripts and Extensions see here and here. See this for information on an extension called nautilus-gksu that adds an "Open as administrator" item to the context menu (plus other extensions).

1
  • +1 for nice reference, wish I had 15 rep :) Jul 25, 2009 at 5:55
2

You can use a program like gksudo to prompt you for a password, so if this is a text file you can run:

gksudo gedit filename

but there is no way I know of to make this happen by default.

Nautilus does possess a scripting feature, so you could install this Nautilus script to open a root nautilus window at the current location, which should then launch the appropriate program with root credentials.

This is pretty simple, so you should be able to modify this to open the file directly using whatever program you want. I don't know any details about the file, so I can't be any more specific.


OK here's how you do this:

#!/bin/bash
# Place in ~/.gnome2/nautilus-scripts
# I named it "Open As Root..." the filename will be the menu name
# Set as executable
gksudo gnome-open $@

It will now show up under right-click -> scripts and use the default gnome application after authenticating.

By default, sudo caches your password for a brief period of time, and so gtksudo does too. To prevent this, run:

sudo visudo

and add this to the Defaults line:

Defaults    timestamp_timeout = 0

This will disable the sudo caching and prompt you for a password every time.

9
  • I guess may be I am not clear, I can do all that doing "sudo nautlius ." which will then open root enabled nautilus window in the current dir and all this can be done from terminal. What I want is lets say I have a file.txt or dir A, and it will have this locked/crossed GUI symbol across the icon, so when I click on that, what now happens is, it pops a dialog box saying it cant be opened because you dont have proper file permissions. Instead of that, I want to enable a new dialog box, which will prompt me to enter password if I dont have enough permission. Hope this is clear. Jul 25, 2009 at 4:59
  • I've written a nautilus-script that should do what you want. Jul 25, 2009 at 5:39
  • +1 for scripts, wish I had 15 rep :) Jul 25, 2009 at 5:54
  • Thanks for scripts, it solved almost the problem. It asked me for root password for the first time only and it opened the file. But, when I clicked it again, it didnt asked for any password and it just opened the file. which I wish it will ask for password every-time I open the file; so that no other than root can access it again, even if other person is using the machine at same time. Jul 25, 2009 at 5:58
  • That's a behavior of sudo. Updated to provide a solution to this. Jul 25, 2009 at 6:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .