WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: sudo is not all powerfull ?  (Read 5116 times)

Offline remus

  • Sr. Member
  • ****
  • Posts: 371
sudo is not all powerfull ?
« on: September 15, 2011, 07:06:27 AM »
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: [Select]
sudo cat /dev/null > smb.conf
gets me

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

But if I become root with su
And then run the command
Code: [Select]
cat /dev/null > smb.confIt works.

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

Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14815
Re: sudo is not all powerfull ?
« Reply #1 on: September 15, 2011, 07:15:35 AM »
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...

Offline ixbrian

  • Retired Admins
  • Sr. Member
  • *****
  • Posts: 436
Re: sudo is not all powerfull ?
« Reply #2 on: September 15, 2011, 08:07:10 AM »
To redirect to a file using sudo, try something like this:

Code: [Select]
sudo sh -c "cat /dev/null > file"
With your original command the shell was trying to do the redirect with the non-root users privileges. 

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: sudo is not all powerfull ?
« Reply #3 on: September 15, 2011, 11:34:56 AM »
The shortest is:
> file

Use sudo as needed.

ali

  • Guest
Re: sudo is not all powerfull ?
« Reply #4 on: September 22, 2011, 02:56:22 AM »
Can someone help me with the logic here ?
I thought I could use sudo for everything.

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: [Select]
[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]$


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: [Select]
[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]$
« Last Edit: September 22, 2011, 03:32:43 AM by ali »