2023年8月2日发(作者:)

linux下vi编辑只读⽂档⽆法保存的解决⽅法使⽤普通⽤户编辑 等配置⽂件:保存的时 候会提⽰:没有Root Permission可以⽤如下⽅法解决:保存时加上::w !sudo tee %我这⾥ 有⼀个问题。没有配置sudo权限。⽤sudo时提⽰"xxx is not in the sudoers file. This incident will be reported.其中XXX是你的⽤户名,也就是你的⽤户名没有权限使⽤sudo,我们只要修改⼀下/etc/sudoers⽂件就⾏了。下⾯是修改⽅ 法:1)进⼊超级⽤户模式。也就是输⼊"su -",系统会让你输⼊超级⽤户密码,输⼊密码后就进⼊了超级⽤户模式。(当然,你也可以直接⽤root⽤)

2)添加⽂件的写权限。也就是输⼊命 令"chmod u+w /etc/sudoers"。

3)编辑/etc/sudoers⽂件。也就是输⼊命令"vim /etc/sudoers",输⼊"i"进⼊编辑模式,找到这⼀ ⾏:"root ALL=(ALL) ALL"在起下⾯添加"xxx ALL=(ALL) ALL"(这⾥的xxx是你的⽤户名),然后保存(就是先按⼀ 下Esc键,然后输⼊":wq")退出。

4)撤销⽂件的写 权限。也就是输⼊命令"chmod u-w /etc/sudoers"。再次保存:查看是否 成功:$ grep sudo #test :w !sudo tee %解释::w : Write a file.可以将⽂件写⼊,⽂件仍然是只读模式,通过 :q! 退出!sudo : Call shell sudo : The output of the vi/vim write command is redirected using tee.% : Triggers the use of the current put, the ‘tee’ command is run as sudo and follows the vi/vim command on the current filename given.补充:在⽹上看了很多解答,⼤多是⼀句话::w !sudo tee %;或者是:你有权限修改⽂件吗?如果是root权限,可以:wq!实际上我想说的就是⼀句话:在打开⽂件时加上sudo。即:sudo vi /etc/crontab命令打开⽂件,此时便是以root权限进⼊该⽂件。此时退出⽂件使⽤:wq!就可以了。想必很多像我⼀样的⼩⽩在见到上⾯的答案时,最不了解的时⾃⼰出问题时是否具备root权限,苦恼全来源于此啊!⼩⽩加油!

2023年8月2日发(作者:)

linux下vi编辑只读⽂档⽆法保存的解决⽅法使⽤普通⽤户编辑 等配置⽂件:保存的时 候会提⽰:没有Root Permission可以⽤如下⽅法解决:保存时加上::w !sudo tee %我这⾥ 有⼀个问题。没有配置sudo权限。⽤sudo时提⽰"xxx is not in the sudoers file. This incident will be reported.其中XXX是你的⽤户名,也就是你的⽤户名没有权限使⽤sudo,我们只要修改⼀下/etc/sudoers⽂件就⾏了。下⾯是修改⽅ 法:1)进⼊超级⽤户模式。也就是输⼊"su -",系统会让你输⼊超级⽤户密码,输⼊密码后就进⼊了超级⽤户模式。(当然,你也可以直接⽤root⽤)

2)添加⽂件的写权限。也就是输⼊命 令"chmod u+w /etc/sudoers"。

3)编辑/etc/sudoers⽂件。也就是输⼊命令"vim /etc/sudoers",输⼊"i"进⼊编辑模式,找到这⼀ ⾏:"root ALL=(ALL) ALL"在起下⾯添加"xxx ALL=(ALL) ALL"(这⾥的xxx是你的⽤户名),然后保存(就是先按⼀ 下Esc键,然后输⼊":wq")退出。

4)撤销⽂件的写 权限。也就是输⼊命令"chmod u-w /etc/sudoers"。再次保存:查看是否 成功:$ grep sudo #test :w !sudo tee %解释::w : Write a file.可以将⽂件写⼊,⽂件仍然是只读模式,通过 :q! 退出!sudo : Call shell sudo : The output of the vi/vim write command is redirected using tee.% : Triggers the use of the current put, the ‘tee’ command is run as sudo and follows the vi/vim command on the current filename given.补充:在⽹上看了很多解答,⼤多是⼀句话::w !sudo tee %;或者是:你有权限修改⽂件吗?如果是root权限,可以:wq!实际上我想说的就是⼀句话:在打开⽂件时加上sudo。即:sudo vi /etc/crontab命令打开⽂件,此时便是以root权限进⼊该⽂件。此时退出⽂件使⽤:wq!就可以了。想必很多像我⼀样的⼩⽩在见到上⾯的答案时,最不了解的时⾃⼰出问题时是否具备root权限,苦恼全来源于此啊!⼩⽩加油!