2

I am writing a bash script and would like to ssh into a remote machine and execute the adduser command. This is not working, all that I get is the --help text for adduser when I run the code below.

ssh backup@pizza.cs.fredonia.edu 'useradd $username; mkdir /home/$username;' || echo "Unable to create user on pizza.cs.fredonia.edu";;

Ubuntu 14.01

  • Are you using a literal $username? If so, where are you getting that from? Are you expecting the shell to expand it? When I run useradd $username in a local shell I get the help text. – Seth Sep 17 '14 at 3:25
  • Is the user backup@pizza.cs.fedonia.edu able to run useradd? You typically have to be root or sudo useradd assuming that the user backup has sudo rights. – slm Sep 17 '14 at 5:06
  • That is what I now need to add, he does not have the rights to adduser – mrplow911 Sep 17 '14 at 16:10
4

Have you tried using the double quote? Inside single quotes, BASH will not expand the variable $username.

For example, if $username=bob, then this command will expand the variable:

ssh user@hostname "useradd $username; mkdir /home/$username;" 

the quoted portion will expand to:

useradd bob; mkdir /home/bob;

But if you use single quotes, like this:

ssh user@hostname 'useradd $username; mkdir /home/$username;'

Then the quoted portion remains unchanged. It will be interpreted as:

useradd $username; mkdir /home/$username;

BTW, the ; at the end, after $username, isn't necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.