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

时间:2017-04-06 16:16:13

标签: 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

如何在脚本中将此行添加到配置文件中?

1 个答案:

答案 0 :(得分:1)

The problem here is that redirection happens before command execution. So the redirection doesn't have the elevated privileges you expected it to.

There's more than one way around that problem. I generally use something like this.

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

Piping to sudo tee... avoids problems with quoting.


How bash executes commands

Redirections