0

I want to block usb mass storage in some of the systems in the LAN. I have written a bash script which will modify the file. I am running the script as default admin in ubuntu. But the script is not able to modify the file. I am getting the following error "bash: /etc/modprobe.d/blacklist.conf: Permission denied"

Below is my bash script

#/bin/bash
password='mypassword'                  #admin password of remote system
val='blacklist usb-storage'            #text which i want to add in the file
for sysName in $(cat systemDetails)    #systemDetails is file which stores
do
ssh $sysName 'echo '$password' | sudo -S echo '$val ' >> /etc/modprobe.d/blacklist.conf'
echo
done
#script ends

NOTE: I have configured my system such that no ssh password is required.

Any pointers in this regard will be really helpful.

kaushik karan
  • 351
  • 2
  • 7
  • check: ls -la /etc/modprobe.d/blacklist.conf. see what are the permissions. – griffon vulture Apr 29 '15 at 10:06
  • It looks like you have a problem with quoting. And why are you using sudo like that when you could have run it directly under root account? – marbu Apr 29 '15 at 10:07
  • The redirection in your command as it is written is processed by the shell that invokes sudo, you need to write/redirect to be initiated by the process started by sudo (the elevated process). – Mat Apr 29 '15 at 10:17
  • to use root account i have to pair my ssh-key of root with all the systems which i want to configure – kaushik karan Apr 29 '15 at 10:19
  • @griffonvulture: the result is -rw-r--r-- 1 root root 1627 Apr 27 11:00 /etc/modprobe.d/blacklist.conf – kaushik karan Apr 29 '15 at 10:25

1 Answers1

-2

Try this:

ssh $sysName "echo $password | sudo -S sh -c 'echo $val >> /etc/modprobe.d/blacklist.conf'"
Jahid
  • 21,542
  • 10
  • 90
  • 108
  • While this code may solve the problem, a few words of explanation would help all of the readers to gain more insight into the solution. – Thom Apr 29 '15 at 11:37
  • The accepted answer in this post explains a lot: http://stackoverflow.com/questions/82256/how-do-i-use-sudo-to-redirect-output-to-a-location-i-dont-have-permission-to-wr – Jahid Apr 29 '15 at 11:42