1

I am trying to change the "hostname --transient" to some computers with a for loop using a variable. I want it to collect the "hostname -f" from the server itself and, using the variable, change it automatically. When I run it, it tells me "Invalid number of arguments."

Command:

for i in `cat /tmp/serverlist`; do echo "Hostname in $i" ssh -o StrictHostKeyChecking=no root@$i "hostname=$(hostname -f); hostnamectl --transient set-hostname $hostname" done 

    1 Answer 1

    3

    Because you are using double quotes, the $hostname variable is being expanded on your local machine, before launching ssh, so it s presumably empty. You can get the same error with:

    $ hostnamectl --transient set-hostname Too few arguments. 

    Since you want these to be expanded on the remote host, use single quotes instead (also fixing a couple of other bad practices):

    while IFS= read -r i; do ssh -o StrictHostKeyChecking=no "root@$i" 'hostnamectl --transient set-hostname "$(hostname -f)"' done 
    1
    • It was because of that, thank you very much! Tips for improvement are appreciated!
      – henrolos
      CommentedOct 9, 2023 at 11:10

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.