【问题标题】:Unix script can't alter postgres hba.conf configuration file on UbuntuUnix 脚本无法更改 Ubuntu 上的 postgres hba.conf 配置文件
【发布时间】:2017-04-06 16:16:12
【问题描述】:

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

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

【问题讨论】:

  • 您的sudo 覆盖echo,而不是>>。如果你引用整个事情,它应该会更好。
  • echo "host..." | sudo tee -a filename 工作吗?
  • @MikeSherrill'CatRecall' 似乎有效。你会添加一个答案,以便我可以信任你吗?

标签: bash postgresql ubuntu unix vagrant


【解决方案1】:

这里的问题是重定向发生在命令执行之前。所以重定向没有你期望的提升权限。

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

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

通过管道连接到sudo tee... 可以避免引用问题。


How bash executes commands

Redirections

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    相关资源
    最近更新 更多