-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5882622
commit fa288c9
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
title: 让Linux用户sudo操作免密码 | ||
lang: zh-CN | ||
date: 2024-06-14 09:28:54 | ||
author: makaspacex | ||
cover: | ||
tags: | ||
--- | ||
|
||
# 让Linux用户sudo操作免密码 | ||
|
||
## 前言 | ||
当前用户不是 root 时,有些操作会因为权限不够而被拒绝,需要 sudo 才可以完成。但是每次 sudo 都需要输入密码,很烦,干脆修改 sudoers,让 sudo 不需要验证密码。 | ||
|
||
#解决 | ||
为了防止改到 /etc/sudoers 的权限,而出现无法用 sudo 的问题,需要切到 root 用户,然后再修改 /etc/sudoers。 | ||
```shell | ||
$ su root | ||
$ vim /etc/sudoers | ||
``` | ||
可以看到有这么一段内容: | ||
```bash | ||
## | ||
## User privilege specification | ||
## | ||
root ALL=(ALL) ALL | ||
%admin ALL=(ALL) ALL | ||
``` | ||
|
||
这两行是允许 root 用户和 admin 用户组的所有用户,在所有主机上执行所有命令,当然是需要 passwd 的。 | ||
|
||
如果想要免密码,可以添加 `NOPASSWD: NOPASSWD:`。 | ||
::: tip | ||
但是建议不要直接改`/etc/sudoers`。 系统更新或者配置更新时该文件会被刷掉。而且你也提到了会有权限问题。 | ||
建议在/etc/sudoers.d 目录下新建一个文件(名字不要带. 不要有~后缀),加上你的那行配置就可以了 | ||
::: | ||
|
||
例如给 bingo 用户添加免密码,编辑`/etc/sudoers.d/sudonopasswd`, 添加如下行 | ||
```bash | ||
bingo ALL=(ALL) NOPASSWD: NOPASSWD: ALL | ||
``` | ||
以后 sudo 就不再需要密码验证了。 | ||
|
||
## 后话 | ||
最好要切换到 root 用户再改 sudoers,否则如果一不小心改了 sudoers 的 owner,sudo 就没办法用了,后果很严重。 | ||
|
||
|
||
|