尝试在 /etc 目录下创建新的备份配置文件时出现了 “Permission Denied” 的错误。

huangapple go评论75阅读模式
英文:

Go "Permission Denied" when trying to create a new backup config file inside /etc

问题

我正在使用功能来保存配置文件的备份的API。以下是您要翻译的代码部分:

func (s *saver) Save(path string, updated []byte) error {
	backupContents, openErr := os.ReadFile(path)
	if openErr == nil {
		t := time.Now()
		backupPath := path + "." + t.Format("2006-01-02") + ".bk"
		err := os.WriteFile(backupPath, backupContents, 0777)
		if err != nil {
			return err
		}

	}
	if openErr == nil || errors.Is(openErr, os.ErrNotExist) {
		file, err := os.Create(path)
		if err != nil {
			return err
		}
		defer file.Close()
		if _, err := file.Write(updated); err != nil { //update file
			return err
		}
	} else if openErr != nil {
		return openErr
	}
	return nil
}

然而,我遇到了错误open /etc/dhcpcd.conf.2022-03-24.bk: permission denied

我可以成功地将文件写入/etc/dhcpcd.conf,因为我的API二进制文件以root权限运行,为什么在/etc中创建新文件会出现权限错误?我考虑过umask,但我认为这不是问题,我的默认umask是0022,但我将其设置为0000进行尝试,但仍然出现相同的权限错误。我还尝试将os.WriteFile替换为os.OpenFile(使用os.O_RDWR|os.O_CREATE标志)或os.Create,但仍然出现权限被拒绝的错误。

这是/etc文件夹的权限:
drwxr-xr-x 115 root root 4096 Mar 24 10:56

请帮忙看看,非常感谢~~

英文:

I am writing APIs with functionalities to save backups of config files inside /etc.

	backupContents, openErr := os.ReadFile(path)
	if openErr == nil {
		t := time.Now()
		backupPath := path + "." + t.Format("2006-01-02") + ".bk"
		err := os.WriteFile(backupPath, backupContents, 0777)
		if err != nil {
			return err
		}

	}
	if openErr == nil || errors.Is(openErr, os.ErrNotExist) {
		file, err := os.Create(path)
		if err != nil {
			return err
		}
		defer file.Close()
		if _, err := file.Write(updated); err != nil { //update file
			return err
		}
	} else if openErr != nil {
		return openErr
	}
	return nil
}

However, I get the error open /etc/dhcpcd.conf.2022-03-24.bk: permission denied"

I can write to /etc/dhcpcd.conf successfully in the same function as my API binaries run with root access, how come creating a new file in /etc has permission errors? I thought of umask though I think it is not the issue, my default umask was 0022 but I set it to 0000 instead to try, but got the same permission errors. I also tried to replace os.WriteFile with os.OpenFile (with os.O_RDWR|os.O_CREATE flags) or os.Create but get the same permission denied errors.

Here is the permission of the /etc folder:
drwxr-xr-x 115 root root 4096 Mar 24 10:56

Please help, thanks a lot~~

答案1

得分: 1

你可能是以非root用户身份运行脚本。
尝试以root用户或使用sudo运行脚本。

在/etc目录中创建/删除/编辑文件需要root权限。

英文:

you are probably running the script with a user that is not root user.
try running the script with root user or with sudo.

/etc dir requires root permissions for creating/removing/editing files

答案2

得分: 1

你需要 root 权限,在运行脚本之前使用 sudo 命令,或者在 bash 中使用 sudo su 获取 root 访问权限。

英文:

You need root permission, run with sudo before the command to run the script or sudo su in bash to get root access.

huangapple
  • 本文由 发表于 2022年3月24日 19:25:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/71601612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定