0

I am having a problem doing what I need. I can save a ton of time if I do this right.

I basically need to loop over every host, and I can get that part on my own.

But I can never get the command to come out right.

I basically want to do this for every host

 chmod 750 /root; chown -R root:root /root;exit;

I've tried so many variants and all of them them will just ssh to the server and do nothing on that server. They never actually run that command on the destination server. Instead they end up running the command on the server I am running ssh from.

Also note public keys are already set up so passwords are no issue.

So to give a better understanding I will show this

for h in HOSTNAMES do
  ssh $h; OTHER STUFF
done
5
  • Are you chaining your "connect to server" command with this using a &? Commented Dec 16, 2015 at 23:00
  • Have you tried chmod 750 /root && chown -R root:root /root? Commented Dec 16, 2015 at 23:01
  • 1
    You're not actually showing how you ssh into your servers and execute these commands, though that probably is the problem, here Commented Dec 16, 2015 at 23:02
  • 1
    how are you managing entering the password for root? I think that has to be the number 1 problem. But we need to see some failed code to help fix it, we can't write it for you ;-) OK? Good luck. Commented Dec 16, 2015 at 23:03
  • secure equivs is already set up Commented Dec 16, 2015 at 23:04

2 Answers 2

1

The command to the remote server is given as the final argument to ssh. Also the exit command is not required, as the connection is closed after commands are completed.

for h in $HOSTNAMES
do
  ssh $h "chmod 750 /root; chown -R root:root /root"
done
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah I was just wanting to make sure the exit was done... I'll try this when I can later.
This definitely worked. Thanks. I had to modify a few things on my end because of how we can do something but yeah this was great.
1

this works for me.

C02NQ9GLG3QD:~ lcerezo$ ssh -K myhost "last -1;uptime"
lcerezo   pts/2        172.31.0.40      Wed Dec 16 17:31 - 17:39  (00:07)
wtmp begins Tue Sep  8 17:15:21 2015
16:12  up 1 day,  5:06, 6 users, load averages: 2.16 1.98 2.00
C02NQ9GLG3QD:~ lcerezo$`

Have you looked at tools like pdsh? For mass configuration though, I think your time will be better spent learning a configuration management tool such as:

  • ansible
  • puppet
  • cfengine
  • salt stack

2 Comments

That might work but not in this instance. I don't have that ability.
Which ability? the -K flag is my usage, but not relevant to the question. the important part is to put these in quotes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.