简体   繁体   English

Shell在创建配置文件时抱怨文件权限

[英]Shell complains about file permissions when creating a config file

I'm not completely sure if I should ask here, over at the Unix forums or somewhere completely different but, here we go. 我不确定是否应该在Unix论坛或完全不同的地方问这个问题,但是,我们开始吧。

I'm using Packer to create a set of images (running Debian 8) for AWS and GCE, and during this process I want to install HAProxy and set up a config file for it. 我正在使用Packer为AWS和GCE创建一组映像(运行Debian 8),在此过程中,我想安装HAProxy并为其设置配置文件。 The image building and package installation goes smooth, but I'm having problems with file permissions when I'm trying to either create the config file or overwrite the existing one. 映像构建和软件包安装可以顺利进行,但是在尝试创建配置文件或覆盖现有文件时,文件权限出现问题。

My Packer Shell Provisioner runs a set of scripts as the user admin (as far as I know I can't SSH into this setup with root ), where as the one I'm having trouble with looks like this: 我的Packer Shell Provisioner以用户admin身份运行一组脚本(据我所知,我无法使用root SSH进入此设置),其中我遇到问题的脚本如下所示:

#!/bin/bash

# Install HAProxy
sudo apt-get update
sudo apt-get install -y haproxy

# Create backup of default config file
sudo mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak

# Write content over to new config file
OLDIFS=$IFS
IFS=''
sudo cat << EOF > /etc/haproxy/haproxy.cfg
# Content line 1
# Content line 2
# (...)
EOF
IFS=$OLDIFS

The log output gives me this error: /tmp/script_6508.sh: line 17: /etc/haproxy/haproxy.cfg: Permission denied 日志输出给我这个错误: /tmp/script_6508.sh: line 17: /etc/haproxy/haproxy.cfg: Permission denied

I've also thought of having a premade config file moved over to the newly created image, but I'm not sure how to do that. 我还考虑过将预制的配置文件移到新创建的映像上,但是我不确定该怎么做。 And that wouldn't work without writing permissions either, right? 而且,如果没有写权限也不行,对吗?

So, does anyone know how I can set up my Shell script to fix this? 那么,有谁知道我该如何设置Shell脚本来解决此问题? Or if there is another viable solution? 还是有其他可行的解决方案?

The problem with the script is the line 脚本的问题是该行

sudo cat << EOF > /etc/haproxy/haproxy.cfg

The redirection to /etc/haproxy/haproxy.cfg happens before sudo is called, and thus requires that the file can be created and written to by whatever user is running the script. 重定向到/etc/haproxy/haproxy.cfg会在调用sudo之前发生,因此要求该文件可以由运行脚本的任何用户创建和写入。

Your idea of changing the permissions and ownership of that file solves this issue by making the file writable by the user running the script, but really, you seem to be executing every single line of the script as root in any case, so why not just drop all the sudo s altogether and run the whole thing as root ? 您更改该文件的权限和所有权的想法通过使运行脚本的用户可写该文件来解决此问题,但实际上,无论如何,您似乎都以root身份执行脚本的每一行。完全丢弃所有sudo并以root身份运行整个程序?

$ sudo myscript.sh   # executed by the 'admin' user

EDIT: Since this script isn't run on the target machine manually, there are two solutions: 编辑:由于此脚本不是手动在目标计算机上运行,​​所以有两种解决方案:

  1. Go with the chmod solution. 使用chmod解决方案。
  2. Write the config file to a temporary file and move it with sudo . 将配置文件写入临时文件,然后使用sudo移动它。

The second solution involves changing the line 第二种解决方案涉及更改生产线

sudo cat << EOF > /etc/haproxy/haproxy.cfg

to

cat <<EOF >/tmp/haproxy.cfg.tmp

and then after the EOF further down 然后在EOF进一步下降之后

sudo cp /tmp/haproxy.cfg.tmp /etc/haproxy/haproxy.cfg
rm -f /tmp/haproxy.cfg.tmp

This is arguably "cleaner" than messing around with file permissions. 可以说,这比弄乱文件权限更“干净”。

As Kusalananda pointed out, the problem is that output redirection happens in the shell which calls sudo . 正如Kusalananda指出的那样,问题在于输出重定向发生在调用sudo的shell中。

In such situations I generally use this simple trick: 在这种情况下,我通常使用以下简单技巧:

TMPFILE=`tempfile`
cat << EOF > $TMPFILE
# Content line 1
# Content line 2
# (...)
EOF
sudo cp $TMPFILE /etc/haproxy/haproxy.cfg
rm $TMPFILE

I create a temp file and put content there (no need for sudo for that step). 我创建了一个临时文件,并将内容放在那里(该步骤不需要sudo)。 And then with sudo, copy the temp file to the final destination. 然后使用sudo将临时文件复制到最终目标。 Finally: delete the temp file. 最后:删除临时文件。 (I use the copy to make the file ownership to belong to the root; the move would keep the user/group of the calling user. Alternatively, one can use the chmod / chown to fix the permissions.) (我使用副本使文件所有权属于根;此举将保留主叫用户的用户/组。或者,可以使用chmod / chown来修复权限。)

The solution to this was quite simple, and 123's comment gave me the right answer: chown 解决这个问题的方法非常简单,123的评论给了我正确的答案: chown

By changing this 通过改变这个

sudo mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak

to this 对此

sudo chown admin /etc/haproxy/haproxy.cfg
sudo chmod 644 /etc/haproxy/haproxy.cfg
sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak

I now have both the permissions and ownership I need for my setup to work. 现在,我同时拥有设置所需的权限和所有权。

EDIT 编辑

Other users have provided better and more viable solutions, as well as answered some issues around my script. 其他用户提供了更好,更可行的解决方案,并回答了有关我的脚本的一些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM