使用普通用戶編輯nginx.conf 等配置文件:

保存的時 候會提示:沒有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 nginx.conf
#test :w !sudo tee %
解釋:
:w : Write a file.可以將文件寫入,文件仍然是只讀模式,通過 :q! 退出
!sudo : Call shell sudo command.
tee : The output of the vi/vim write command is redirected using tee.
% : Triggers the use of the current filename.
Simply 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權限,苦惱全來源于此啊!
小白加油!