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

bpsygsoo  于 5个月前  发布在  Nginx
关注(0)|答案(2)|浏览(59)

我正在使用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盒子。
我试图寻找一种方法来改变供应命令的权限,但我只找到了改变config.vm.share_folder命令所有者的方法。
你知道答案吗?

jgwigjjp

jgwigjjp1#

正如错误消息所建议的,也来自documentation
文件提供程序以SSH或PowerShell用户的身份进行文件上传。这一点很重要,因为这些用户自己通常没有提升的权限。如果要将文件上传到需要提升权限的位置,我们建议将文件上传到临时位置,然后使用shell提供程序将它们移动到位。
因此vagrant用户(如果未修改)用于scp文件,但您无法使用它访问/etc/
要使其工作,您需要将其上传到一个临时位置,然后使用shell provider将其移动到目标目录:

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"

字符串
这是因为privileged选项在shell provisioners上默认为true。但是让两个provisioners只复制一个配置文件有点复杂,对吧?
好吧,如果文件已经在你的共享文件夹中,你可以使用shell provider将其复制到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"

vmjh9lq9

vmjh9lq92#

vagrant用户有权访问的第一个复制文件,例如/home/vagrant
然后把它移到你想要的地方。

config.vm.provision "file", source: "./sources.list", destination: "./sources.list"
config.vm.provision "shell", inline: "mv ./sources.list /etc/apt/sources.list"

字符串
这很好,因为file provisioners使用普通用户,而shell provisioners使用root用户来执行作业。

相关问题