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

Unix script can't alter postgres hba.conf configuration file on Ubuntu

我正在尝试通过配置脚本在 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