pantz.org banner
Echo lines into a root owned file with sudo
Posted on 07-16-2011 19:40:21 UTC | Updated on 07-16-2011 19:52:48 UTC
Section: /software/shell/ | Permanent Link

This sounds super easy. Just echoing some lines into root owned file using using sudo. You figure something like "sudo echo blah >> /tmp/test" would work fine. You would be wrong. Let me demonstrate. You need sudo access to the root user to do this. Here is an example of the problem:

user@host$ touch /tmp/test
user@host$ sudo chown root: /tmp/test

user@host$ ls -la /tmp/test
-rw-r--r-- 1 root root 0 2011-07-16 14:34 /tmp/test

user@host$ sudo echo blah >> /tmp/test
bash: /tmp/test: Permission denied

The echo program is running as root because we used sudo, but the shell that's redirecting echo's output to the root owned file is still running as the unprivileged user. The current shell does the redirection before sudo starts. To fix this we need to run the whole pipeline under sudo.

user@host$ echo "echo blah >> /tmp/test" | sudo bash

user@host$ cat /tmp/bash
blah

Doing this as an ssh command to another machine adds some more complication but to get it to work use the following command. This works best if you have ssh keys setup and NOPASSWD access to sudo to root. On machines with remote root login disabled you are forced to use sudo to perform ugly commands like the following.

ssh user@host 'sudo su root -c "echo \"echo test >> /tmp/test\" | sudo bash"'

The above command uses "sudo su root" because of an issue I saw with OpenBSD. If you did not put in the root user then you would get the error "su: no such login class:". Putting in the root user fixes this issue.

If you have more than one line to execute on a remote server then I would suggest just putting all of the commands you want to execute into a script and copy the script to the machine with scp and then executing with another ssh command. This saves you from executing that nasty ssh line above and writing a whole script filled with ssh commands.

Del.icio.us! | Digg Me! | Reddit!

Related stories


RSS Feed RSS feed logo
About


3com
3ware
alsa
alsactl
alsamixer
amd
android
apache
areca
arm
ati
auditd
awk
badblocks
bash
bind
bios
bonnie
cable
carp
cat5
cdrom
cellphone
centos
chart
chrome
cifs
cisco
cloudera
comcast
commands
comodo
compiz-fusion
corsair
cpufreq
cpufrequtils
cpuspeed
cron
crontab
crossover
cu
cups
cvs
database
dbus
dd
dd_rescue
ddclient
debian
decimal
dhclient
dhcp
diagnostic
diskexplorer
disks
dkim
dns
dos
dovecot
drac
dsniff
dvdauthor
e-mail
echo
editor
emerald
ethernet
expect
ext3
ext4
fat32
fedora
fetchmail
fiber
filesystems
firefox
firewall
flac
flexlm
floppy
flowtools
fonts
format
freebsd
ftp
gdm
gmail
gnome
greasemonkey
greylisting
growisofs
grub
hacking
hadoop
harddrive
hba
hex
hfsc
html
html5
http
https
idl
ie
ilo
intel
ios
iperf
ipmi
iptables
ipv6
irix
javascript
kde
kernel
kickstart
kmail
kprinter
krecord
kubuntu
kvm
lame
ldap
linux
logfile
lp
lpq
lpr
maradns
matlab
memory
mencoder
mhdd
mkinitrd
mkisofs
moinmoin
motherboard
mouse
movemail
mplayer
multitail
mutt
myodbc
mysql
mythtv
nagios
nameserver
netflix
netflow
nginx
nic
ntfs
ntp
nvidia
odbc
openbsd
openntpd
openoffice
openssh
openssl
opteron
parted
partimage
patch
perl
pf
pfflowd
pfsync
photorec
php
pop3
pop3s
ports
postfix
power
procmail
proftpd
proxy
pulseaudio
putty
pxe
python
qemu
r-studio
raid
recovery
redhat
router
rpc
rsync
ruby
saltstack
samba
schedule
screen
scsi
seagate
seatools
sed
sendmail
sgi
shell
siw
smtp
snort
solaris
soundcard
sox
spam
spamd
spf
sql
sqlite
squid
srs
ssh
ssh.com
ssl
su
subnet
subversion
sudo
sun
supermicro
switches
symbols
syslinux
syslog
systemrescuecd
t1
tcpip
tcpwrappers
telnet
terminal
testdisk
tftp
thttpd
thunderbird
timezone
ting
tls
tools
tr
trac
tuning
tunnel
ubuntu
unbound
vi
vpn
wget
wiki
windows
windowsxp
wireless
wpa_supplicant
x
xauth
xfree86
xfs
xinearama
xmms
youtube
zdump
zeromq
zic
zlib