I want to modify /etc/hosts (and later also hostname) via a script and ssh. So I'm logged in via ssh and this works:
sudo nano /etc/hosts
It shows the remote hosts file in nano. However I was trying this (via commandline, but this should work in order to make it work in a script right?):
echo [mypassword] | sudo -S nano /etc/hosts
but get this output:
Recieved SIGHUP or SIGTERM
Buffer written to /etc/hosts.save
I just saw that this also happens with more simple text-files (e.g. test.txt). Any ideas how to fix this?
And yes, I am aware of the insecurity of the clear password. :)
Thanks in advance
I suggest using passwordless sudo than writing your password in clear typing in script.
In the script, use echo to write to the textfile, not nano.
like
or did i get your purpose right?
I found the
tee
command to be helpful in avoiding the sudo limitation on file redirection.This is the command I used to remotely append the hosts for all machines in my cluster to /etc/hosts:
The output for each iteration looks like this:
This SU answer was pivotal in building my final solution: https://superuser.com/a/1026359/587485
What worked for me is writing in a temporary file and replacing /etc/hosts with it
like @Pasi suggested, this needs passwordless sudo