Vagrant:config.vm.provision不允许我将文件复制到etc/nginx/conf.d?

11

我正在使用Nginx服务器。我想使用Vagrantfile将一个配置文件复制到/etc/nginx/conf.d。我使用的命令是:

config.vm.provision "file", source: "./bolt.local.conf", destination: "/etc/nginx/conf.d/bolt.local.conf"

我收到的错误信息是:
Failed to upload a file to the guest VM via SCP due to a permissions
error. This is normally because the SSH user doesn't have permission
to write to the destination location. Alternately, the user running
Vagrant on the host machine may not have permission to read the file.

我正在使用bento/ubuntu-16.04镜像。

我试图寻找一种方法来更改provision命令的权限,但我只找到了更改config.vm.share_folder命令所有者的方法。

你知道答案吗?

1个回答

22

正如错误信息所提示,以及文档中所述:

通过文件提供程序上传的文件是使用SSH或PowerShell用户完成的。这很重要,因为这些用户通常没有自己的提升权限。如果您想上传到需要提升权限的位置,请将它们上传到临时位置,然后使用shell提供程序将其移动到指定位置。

所以,默认情况下使用vagrant用户来进行scp文件,但您无法使用该用户访问/etc/

要使其工作,您需要将其上传到临时位置,然后使用shell提供程序将其移动到目标目录:

config.vm.provision "file", 
  source: "./bolt.local.conf", 
  destination: "/tmp/bolt.local.conf"

config.vm.provision "shell",
  inline: "mv /tmp/bolt.local.conf /etc/nginx/conf.d/bolt.local.conf"

这个可以工作是因为在shell provisioners上,默认情况下privileged选项为true。但是,仅仅为了复制一个配置文件而使用两个provisioners有点复杂,对吧?

那么,如果文件已经在共享文件夹中,您可以使用shell provisioner将其复制到nginx目录中,这样最终会得到以下内容:

# This is the default and serve just as a reminder
config.vm.synced_folder ".", "/vagrant"
config.vm.provision "shell",
  inline: "cp /vagrant/bolt.local.conf /etc/nginx/conf.d/bolt.local.conf"

感谢您抽出时间回答这个问题! - TJ Zimmerman
NP,很高兴能帮忙 :) - lee-pai-long

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接