bash - Unix 脚本无法更改 Ubuntu 上的 postgres hba.conf 配置文件

标签 bash postgresql ubuntu unix vagrant

我正在尝试通过配置脚本在 ubuntu/vagrant 上设置 postgres 9.6。我的部分脚本使用以下命令向 pg_hba.conf 添加了一行:

sudo -u postgres echo "host all all all md5">>/etc/postgresql/9.6/main/pg_hba.conf

但是,这给了我错误 -bash:/etc/postgresql/9.6/main/pg_hba.conf: Permission denied

这很奇怪,因为我可以使用 sudo nanosudo -u postgres nano 编辑文件。

文件的权限如下: -rw-r----- 1 postgres postgres 4641 Apr 6 16:11 pg_hba.conf

如何在脚本中将这一行添加到我的配置文件中?

最佳答案

这里的问题是重定向发生在命令执行之前。因此,重定向没有您期望的提升权限。

解决这个问题的方法不止一种。我通常使用这样的东西。

echo "host..." | sudo tee -a /etc/postgresql/9.6/main/pg_hba.conf

管道连接到 sudo tee... 避免了引用问题。


How bash executes commands

Redirections

关于bash - Unix 脚本无法更改 Ubuntu 上的 postgres hba.conf 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43260573/

相关文章:

postgresql - 如何根据 PostgreSQL 中的先前值计算计算列的值

mysql - 无法为我的 Ubuntu 机器重置 mysql 密码

ubuntu - 如何在 GNUPlot 图中的每个条上给出文本

linux - 为什么 git bash 不将我的 python 安装的路径转换为 ​​nix 表示法?

python - Duo API Bash 调用

linux - Bash 脚本 - 将第 2 列除以中间的列,但将 1 和 4 保留在两侧

sql - 如何从 postgresql 中的时间戳中提取工作日?

PostgreSQL : How to join with multiple cross-reference table?

python - 如何在 vim 中启用 python3?

bash - 更改 ls 命令的 emacs bash 颜色