编辑 .bash_profile 时出现“Permission Denied”的错误提示

10

我仍在学习CLI及其内部工作原理,尤其是 .bash_profile。我对所学内容感到不知所措。

无论如何,我可以访问.bash_profile。如果我执行nano ~/.bash_profile,那么文件就会出现,我就可以自由编辑。然后我尝试添加下面这行:

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

但是,当我尝试保存文件(.bash_profile)时,我收到以下错误提示:[ Error writing /home/myname.bash_profile Permission denied ]

2个回答

20

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

如果你尝试使用 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

我的 .bash_profile 文件的所有者是我,Greg,而不是root。我认为当您这样做时,您会发现您的 .bash_profile 文件将被root所拥有。这意味着当您想要编辑该文件时,您需要使用sudo命令,但我不需要。

如果您不想一直使用sudo命令来修改此文件,则可以更改文件的所有者:sudo chown yourusername ~/.bash_profile

现在,如果您运行我上面展示的 ls 命令,您应该会看到您的名称作为该文件的所有者,而不是root。


这绝对是正确的答案。而且,解释得非常好。 - Jake Johnson
@gykaling,但我在ls -la ~ | grep bash中看不到.bash_profile,我该怎么办? - sachsure

-1

您需要root权限才能进行编辑。

请使用sudo或以root身份登录并进行编辑。

谢谢,

Sandeep。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接