Linux 编辑 .bash_profile 时权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17357652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:20:06  来源:igfitidea点击:

Permission Denied when editing .bash_profile

linuxcommand-line-interface.bash-profile

提问by simplycoding

I'm still learning the CLI and the ins and outs of it, especially this .bash_profile. I feel overwhelmed with what I'm learning about this.

我仍在学习 CLI 和它的来龙去脉,尤其是这个 .bash_profile。我对我在这方面学到的东西感到不知所措。

Anyways, I can access .bash_profile. If I do nano ~/.bash_profilethen the file appears and I'm free to edit. And then I tried addingin the line I'm supposed to include:

无论如何,我可以访问 .bash_profile。如果我这样做nano ~/.bash_profile,文件就会出现,我可以自由编辑。然后我尝试添加我应该包括的行:

export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

However, when I try to save the file (or whatever .bash_profile is), I get the following error: [ Error writing /home/myname.bash_profile Permission denied ]

但是,当我尝试保存文件(或任何 .bash_profile 是)时,出现以下错误: [ Error writing /home/myname.bash_profile Permission denied ]

回答by Sandeep

You need root permissions to edit.

您需要 root 权限才能编辑。

Edit it with sudo or login as root and edit.

使用 sudo 编辑它或以 root 身份登录并编辑。

Thanks,

谢谢,

Sandeep.

桑迪普。

回答by gkayling

While you can do sudo nano ~/.bash_profileto edit the file, I feel like it's a bit weird that your .bash_profile needs root to be modified.

虽然你可以sudo nano ~/.bash_profile编辑文件,但我觉得你的 .bash_profile 需要 root 来修改有点奇怪。

If you try ls -la ~ | grep bash

如果你试试 ls -la ~ | grep bash

-rw-------    1 Greg  staff    8622 27 Jun 16:06 .bash_history
-rw-r--r--    1 Greg  staff    2189 28 Jun 01:24 .bash_profile

You see my .bash_profile is owned by me, Greg, not root. I think you'll find that when you do this, your .bash_profile will be owned by root. This means that when you want to edit the file, you need to use sudo, but I don't.

你看我的 .bash_profile 归我所有,Greg,而不是 root。我想你会发现,当你这样做时,你的 .bash_profile 将归 root 所有。这意味着当你想编辑文件时,你需要使用 sudo,但我不需要。

If you don't want to have to use sudo all the time to modify this file, you can change the owner of the file sudo chown yourusername ~/.bash_profile

如果你不想一直使用sudo来修改这个文件,你可以更改文件的所有者 sudo chown yourusername ~/.bash_profile

Now if you run the lscommand I showed above, you should see your name as the owner of the file, instead of root.

现在,如果你运行ls我上面显示的命令,你应该看到你的名字是文件的所有者,而不是 root。