在CentOS7系统中,一般默认管理员账号都是root用户,但是root用户权限比较大,因此如果有需要其它用户来协助管理的话,最好是添加一个新用户,而不是直接给root账号给他用。
那么,在CentOS 7中怎么添加一个新用户并授权呢?本文以RAKsmart Linux VPS为例,下面简单的来说说。
1、创建新用户
创建一个用户名为:newuser
[root@appmoney ~]# adduser newuser
接下来为这个用户设置初始化密码,系统会判断密码复杂度,不过可以强行忽略:
[root@appmoney ~]# passwd newuser
2、给新用户授权
创建用户之后,权限只可以在本home下有完整权限,其他目录要看别人授权。而经常需要root用户的权限,这时候sudo可以化身为root来操作。
新创建的用户并不能使用sudo命令,需要给他添加授权。
sudo命令的授权管理是在sudoers文件里的。可以看看sudoers:
[root@appmoney ~]# sudoers
bash: sudoers: 未找到命令…
[root@localhost ~]# whereis sudoers
sudoers: /etc/sudoers /etc/sudoers.d /usr/libexec/sudoers.so /usr/share/man/man5/sudoers.5.gz
找到这个文件位置之后再查看权限:
[root@appmoney ~] # ls -l /etc/sudoers
-r–r—– 1 root root 4251 12月5 15:08 /etc/sudoers
从上面可以看到,只有只读的权限,如果想要修改的话,需要先添加w权限:
[root@appmoney ~# chmod -v u+w /etc/sudoers
mode of “/etc/sudoers” changed from 0440 (r–r—–) to 0640 (rw-r—–)
然后就可以添加内容了,在下面的一行下追加新增的用户:
[root@appmoney ~] # vim /etc/sudoers
## Allow root to run any commands anywher
root ALL=(ALL) ALL
linuxidc ALL=(ALL) ALL #这个是新增的用户
:wq保存退出,这时候要记得将写权限收回:
[root@appmoney ~]# chmod -v u-w /etc/sudoers
mode of “/etc/sudoers” changed from 0640 (rw-r—–) to 0440 (r–r—–)
这时候使用新用户登录,使用sudo:
[[root@appmoney ~]$ sudo cat /etc/passwd
[sudo] password for linuxidc:
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
第一次使用会提示需要输入密码才可以下一步。如果不想需要输入密码怎么办,将最后一个ALL修改成NOPASSWD: ALL。