centos - Using SUDO to execute a general command inside a non-accessible directory
2014-04
I want to grep something inside a directory. But the user dont have permissions in the direcotry. Because of some reasons, i am not allowed to change permissions, group or owner. Just want to know i can access that directory by some entry in the sudoers file.
grep -r "some phrase" /path/to/restricted-dir
Is there any way to execute the above command using SUDO. ?
sudo -i
grep -r "some phrase" /path/to/restricted-dir
exit
sudo -i puts you in interactive mode, so your prompt turns to '#' and all commands you run are executed as root, until you 'exit'.
I don't understand your problem, why not just use sudo
?
sudo grep -r "some phrase" /path/to/restricted-dir
For example:
$ sudo tree -up
.
└── [drwx------ root ] foo
├── [drwx------ root ] bar
│ └── [drwx------ root ] baz
│ └── [drwx------ root ] foobar
│ └── [-rwx------ root ] foo.txt
└── [-rwx------ root ] foo.txt
4 directories, 2 files
As you can see above, I have a folder structure with three nested subdirectories of foo
all of which are owned by root and only root has access to them. Therefore, running normal grep
fails:
$ grep -r foo foo/
grep: foo/: Permission denied
Running with sudo
works perfectly and finds the string foo
in both files that contain it:
$ sudo grep -r foo foo/
foo/bar/baz/foobar/foo.txt:foobar
foo/foo.txt:foobar
For more complex commands, you can use either sudo -i
as suggested by @FrankThomas or su
if you have the root
account enabled.
I feel kinda silly asking this question. I'm using CentOS 5.4 and KDE. I downloaded an archive and I want to drag/drop the contents into a folder that I need root access to write to.
I can obviously go into terminal and sudo blah blah. But how do I get sudo access for desktop procedures? Like for simple dragging and dropping of files? KDE just tells me that I don't have permission to do that, but doesn't give me the option of entering the root password or sudo.
Create a shortcut to your favourite file manager (Konqueror, Dolphin or whatever) on your desktop. Right click on it, and click "Properties". On the "Application" tab, click "Special settings", and check the "Run as different user" check box (the exact phrases may be different, because I'm not using English KDE). At least it is how it is done in KDE 4, but on KDE 3 it is very similar. Or you can simply write "kdesu" before the command name.
Open the file manager with this shortcut, and do the copying from here. Be warned, though, that any application you run from this file manager will be run as root.
gksu
, kdesu
or gksudo
, kdesudo
See more: Ubuntu Forums – HOWTO: Easily open any file as root via drag & drop
Create a launcher with the following command:
gksudo "gnome-open %u"
You'll have to use kdesu <appname>
Example: Launch Konsole, and type kdesu dolphin
or kdesu konqueror
Follow these directions, but replace nautilus
with dolphin
or whatnot.