Unable to edit file remotely using ssh

Hi,

I am trying to edit the /etc/sudoers file using ksh, however I am unable to do that. Please find the code below:

ssh $i "/usr/local/bin/sudo /usr/bin/echo "$user_id     ALL=(ALL) ALL" >> /etc/sudoers"

I've tried changing the double quotes to single but it is still not working. The user_id will be given when the script runs. I've tried this also:

ssh $i '/usr/local/bin/sudo /usr/bin/echo "$user_id     ALL=(ALL) ALL" >> /etc/sudoers'

I am getting the below error in both the cases:

ksh: /etc/sudoers: cannot create

Any suggestions will be appreciated.

Thanks

Looks to be a permission issue with your credentials on the remote machine. Are you sure the userid thats used with ssh login is part of sudoers on the remote machine ?

Also pls check permissions of /etc/sudoers on the remote system.

The user which is executing the script is added into the sudoers of the remote machine also the permissions of the sudoers file are given below:

# grep -i testmgmt /etc/sudoers
testmgmt        ALL=(ALL) NOPASSWD: /usr/sbin/useradd, /usr/sbin/userdel, /usr/local/bin/sudo, /usr/sam/lbin/usermod.sam, /usr/bin/perl, /log/script/server_script/deluser.sh, /log/script/server_script/deluser1.sh, /usr/bin/echo
# ls -ld /etc/sudoers
-r--r-----   1 root       root          1928 Sep  3 14:39 /etc/sudoers

Please suggest now, as the user is able to create the users on the remote machine but unable to edit the sudoers file.

The safest way to edit sudoers file without changing its permissions is using visudo

This thread will give you some hint

linux - How do I edit /etc/sudoers from a script? - Stack Overflow