`gcloud compute copy-files`: permission denied when copying files

前端 未结 7 1924
滥情空心
滥情空心 2020-12-04 22:24

I\'m having a hard time copying files over to my Google Compute Engine. I am using an Ubuntu server on Google Compute Engine.

I\'m doing this from my OS X terminal a

相关标签:
7条回答
  • 2020-12-04 22:35

    insert root@ before the instance name:

    local:$ gcloud compute copy-files /Users/Bryan/Documents/Websites/gce/index.php root@example-instance:/var/www/html --zone us-central1-a
    
    0 讨论(0)
  • 2020-12-04 22:38

    The reason this doesn't work is that your username does not have permissions on the GCE VM instance and so cannot write to /var/www/html/.

    Note that since this question is about Google Compute Engine VMs, you cannot SSH directly to a VM as root, nor can you copy files directly as root, for the same reason: gcloud compute copy-files uses scp which relies on ssh for authentication.

    Possible solutions:

    1. (also suggested by Faizan in the comments) this solution will require two steps every time

      1. use gcloud compute copy-files to transfer files/directories where your user can write to, e.g., /tmp or /home/$USER

      2. login to the GCE VM via gcloud compute ssh or via the SSH button on the console and copy using sudo to get proper permissions:

        # note: sample command; adjust paths appropriately

        sudo cp -r $HOME/html/* /var/www/html

    2. this solution is one step with some prior prep work:

      1. one-time setup: give your username write access to /var/www/html directly; this can be done in several ways; here's one approach:

        # make the HTML directory owned by current user, recursively

        sudo chown -R $USER /var/www/html

      2. now you can run the copy in one step:

        gcloud compute copy-files /Users/Bryan/Documents/Websites/gce/index.php example-instance:/var/www/html --zone us-central1-a

    0 讨论(0)
  • 2020-12-04 22:38

    I use a bash script to copy from my local machine to writable directory on the remote GCE machine; then using ssh move the files.

    SRC="/cygdrive/d/mysourcedir"
    TEMP="~/incoming"
    DEST="/var/my-disk1/my/target/dir"
    

    You also need to set GCE_USER and GCE_INSTANCE

    echo "=== Pushing data from $SRC to $DEST in two simple steps"
    echo "=== 1) Copy to a writable temp directoy in user home"
    gcloud compute copy-files "$SRC"/*.* "${GCE_USER}@${GCE_INSTANCE}:$TEMP"
    echo "=== 2) Move with 'sudo' to destination"
    gcloud compute ssh ${GCE_USER}@${GCE_INSTANCE} --command "sudo mv $TEMP/*.* $DEST" 
    

    In my case I don't want to chown the target dir as this causes other problems with other scripts ...

    0 讨论(0)
  • 2020-12-04 22:38

    This is to copy files from remote machine to your machine. And make sure you have ssh setup because this will use default ssh keys. This worked for me:

    gcloud compute scp 'username'@'instance_name':~/source_dir  /destination_dir --recurse
    

    This is the generic syntax, so if you want to copy files from your machine to remote machine, you can use this. --recurse : required to copy directories with other files inside

    Syntax: gcloud compute scp 'SOURCE' 'DESTINATION'

    NOTE: run it without root

    0 讨论(0)
  • 2020-12-04 22:42

    UPDATE

    gcloud compute copy-files is deprecated.

    Use instead:

    $ gcloud compute scp example-instance:~/REMOTE-DIR ~/LOCAL-DIR \ --zone us-central1-a
    

    More info: https://cloud.google.com/sdk/gcloud/reference/compute/scp

    0 讨论(0)
  • 2020-12-04 22:49

    I had the same problem and didn't get it to work using the methods suggested in the other answers. What finally worked was to explicitly send in my "user" when copying the file as indicated in the official documentation. The important part being the "USER@" in

    gcloud compute scp [[USER@]INSTANCE:]SRC [[[USER@]INSTANCE:]SRC …] [[USER@]INSTANCE:]DEST
    

    In my case I could initially transfer files by typing:

    gcloud compute scp instance_name:~/file_to_copy /local_dir
    

    but after I got the permission denied I got it working by instead typing:

    gcloud compute scp my_user_name@instance_name:~/file_to_copy /local_dir
    

    where the username in my case was the one I was logged in to Google Cloud with.

    0 讨论(0)
提交回复
热议问题