Off-Topic > Off-Topic - Tiny Tux's Corner

sudo is not all powerfull ?

(1/1)

remus:
Hi all,

Just found out how to erase the contents of a file without deleting the file. Here are two ways I learn't to do it.

echo -n > YOURFILE
cat /dev/null > YOURFILE

I'm actually trying to clear the default contents of the /usr/local/etc/samba/smb.conf file with sudo but I get an error message.


--- Code: --- sudo cat /dev/null > smb.conf
--- End code ---

gets me


--- Code: ----sh: can't create smb.conf: Permission denied

--- End code ---

But if I become root with su
And then run the command

--- Code: ---cat /dev/null > smb.conf
--- End code ---
It works.

Can someone help me with the logic here ?
I thought I could use sudo for everything.

Juanito:
As I recall, the script /usr/local/tce.installed/samba3 checks for the presence of an existing /usr/local/etc/samba/smb.conf and will not overwrite if one is already present...

ixbrian:
To redirect to a file using sudo, try something like this:


--- Code: ---sudo sh -c "cat /dev/null > file"
--- End code ---

With your original command the shell was trying to do the redirect with the non-root users privileges. 

gerald_clark:
The shortest is:
> file

Use sudo as needed.

ali:

--- Quote from: remus on September 15, 2011, 03:06:27 AM ---Can someone help me with the logic here ?
I thought I could use sudo for everything.

--- End quote ---

the logic is simple, you gave root permissions to the echo not to the redirect, the redirect (>) is session specific, the root privileges are not passed through it
to achieve what you want you have to pipeline the command
first echo something then use a sudo command to overwrite the file


--- Code: ---[ali@linux chuck]$ cat test
hello dear johnny
[ali@linux chuck]$ sudo echo "" > test
bash: test: Permission denied
[ali@linux chuck]$ echo -n | sudo tee test
[ali@linux chuck]$ cat test
[ali@linux chuck]$

--- End code ---


here's more proof that it's session specific
the > doesn't care what is before it and it's good because it doesn't
you don't want your log files to be owned by root
it's like this
(sudo echo hi) > test
> is owned by user that's why the output file is owned by user, you were just doing it wrong
you should use > to redirect what the user sees, here's a good example

--- Code: ---[ali@linux chuck]$ cat test
[ali@linux chuck]$ echo "why can i still see the output?" | sudo tee test
why can i still see the output?
[ali@linux chuck]$ cat test
why can i still see the output?
[ali@linux chuck]$ echo "i'd rather not see it, thank you" | sudo tee test > /dev/null
[ali@linux chuck]$ cat test
i'd rather not see it, thank you
[ali@linux chuck]$

--- End code ---

Navigation

[0] Message Index

Go to full version