Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to scp a file to /var/www because I'm playing with apache. To move the file I do scp -i <ec2-shh-key> localfile root@<ec2-instance-ip>:/var/www/fileremote.

But this gives me an error:

Please login as the user "ubuntu" rather than the user "root".

How can I solve this problem?

EDIT: I have discovered this superuser question which answers the question by first scp'ing the file to a non-sudo dir, then passing a sudo mv command over ssh to move the file. Is there no way to do this in one step with just scp?

share|improve this question
up vote 1 down vote accepted

If you are getting the error you list then you are not specifying "ubuntu@" in the scp command, or you have copied the /root/.ssh/authorized_keys file to /home/ubuntu/.ssh/authorized_keys (which would be bad as you could then not log in with ssh).

It is true that once you do start specifying "ubuntu@" you will get a permissions error, but you can get around this using a command like this:

rsync -vazSHAX --rsync-path "sudo rsync" LOCALILE ubuntu@HOST:/var/www/REMOTEFILE

The key here is the --rsync-path option.

The rsync command will even let you copy whole directory trees as in:

rsync -vazSHAX --rsync-path "sudo rsync" LOCALDIR/ ubuntu@HOST:REMOTEDIR/

If you started the instance with a keypair generated by EC2 then you may also need to specify this option in rsync pointing to the local copy of the downloaded ssh key:

--rsh "ssh -i KEYPAIR.pem"

Here's an article I wrote about how to use sudo, ssh, rsync on Ubuntu EC2 instances:

Using sudo, ssh, rsync on the Official Ubuntu Images for EC2

Here's an article where I explain how to upload your default personal ssh key to EC2 so that ssh and rsync don't need extra identity options:

Uploading Personal ssh Keys to Amazon EC2

share|improve this answer
Ok, I'm sorry, I did make an error in my question -- I was in fact doing root@<ec2-inst-ip>; nevertheless, your answer solves my problem. Thanks for the good links. – kalaracey May 16 '12 at 22:57

You can. Create a group, say "www-admin", add your username to that group, chown /var/www/fileremote to :www-admin, and chmod that path to allow group write.

Of course, if the path has been chown-ed to something else (non-default group), that group will lose all access. But in this case, just add your username to that group.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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