ubuntu 16创建用户并赋予相关权限

cuinew

温馨提示:这篇文章已超过1540天没有更新,请注意相关的内容是否还可用!

一般情况下,我们新购买的ubuntu服务器只有一个超级用户(root),而站在安全的角度来看,后期运维管理工作都不会用root进行,因为那样做的风险性太高了。可以通过创建一个非root的用户来进行相关的运维工作,并赋予这个用户一定的使用权限,这样妈妈再也不用担心您的服务器了。
本文将以创建一个用户,并赋予这个用户root的使用权限为例进行讲解,很适合小白新手,因为我也是一个小白新手。设置其他权限的可自己根据相关情况进行修改。

创建用户

创建一个cuinew的用户:# adduser cuinew

root@IOdZsy190:~# adduser cuinew  //创建一个名为“cuinew”的用户
Adding user `cuinew' ...
Adding new group `cuinew' (1000) ...
Adding new user `cuinew' (1000) with group `cuinew' ...
Creating home directory `/home/cuinew' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for cuinew
Enter the new value, or press ENTER for the default
    Full Name []: cuiwanyong  //用户全称
    Room Number []: 911  //房间号码
    Work Phone []: 13855559999  //工作电话
    Home Phone []:      //家庭电话
    Other []: cuinew work  //其他信息
Is the information correct? [Y/n] y  //确认以上信息,请输入“y”
root@IOdZsy190:~# 

先查看系统管理组的配置情况,主要是看管理组是否开放
命令:# vim /etc/sudoers

root@IOdZsy190:~# vim /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root    ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL  //这就是管理组,组名为"admin",前面没有#注释,证明是开启这个组的。
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d

因为/etc/sudoers这个文件默认权限为只读(440),我们查看完信息后退出这个文件的方法为:
ESC键,再输入英文冒号:,再输入q!,最后回车,才能退出。

从上面的信息来看,系统管理组为admin,前面没有#注释,证明这个管理组是开启状态的。
了解这些后,我们把刚刚创建的用户(cuinew)加入到这个管理组中去。
加入管理组:# usermod -a -G admin cuinew

修改配置文件

通过修改配置文件/etc/sudoers来给用户cuinew赋予root的权限
因为默认/etc/sudoers配置文件是只读权限(440),要修改它,就先修改这个文件的权限
把/etc/sudoers权限修改为读写(640):# chmod -v u+w /etc/sudoers

root@IOdZsy190:~# chmod -v u+w /etc/sudoers  //修改权限为读写(640)
mode of '/etc/sudoers' changed from 0440 (r--r-----) to 0640 (rw-r-----)
//文件/etc/sudoers权限由0440被修改为0640
root@IOdZsy190:~#

修改好/etc/sudoers权限后,接下来我们就可以修改配置文件,给用户cuinew赋予root的使用权限
修改/etc/sudoers配置文件:# vim /etc/sudoers
输入i,进入配置文件的编辑模式

root@IOdZsy190:~# vim /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root    ALL=(ALL:ALL) ALL  //root  任何主机=(任何用户:任何组)  执行任何命令
//第一个ALL登陆任何主机,第二个ALL以任何用户身份来执行(默认为root),第三个ALL以任何组身份来执行,第四个ALL执行任何命令
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL  //%admin表示在admin这个组内的所有用户,访问任何主机=(以任何身份)  执行任何命令
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL  //有sudo使用权限的用户,访问任何主机=(以任何身份)  执行任何命令
cuinew  ALL=(cuinew:admin)  NOPASSWD: ALL  //执行sudo命令时免输入密码,NOPASSWD:和ALL之间要有一个空格
//一定要在最后一行添加,否则会被前面的(包括以%开始的那几句)覆盖。
//同时也可以设置为cuinew  ALL=(ALL)  ALL
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d

修改好配置文件后,按ESC键,再输入:,然后输入wq,最后回车即可保存。
最后别忘了把/etc/sudoers配置文件的权限修改回原来的只读(440)
修改回只读权限:# chmod -v u-w /etc/sudoers

root@IOdZsy190:~# chmod -v u-w /etc/sudoers
mode of '/etc/sudoers' changed from 0640 (rw-r-----) to 0440 (r--r-----)
root@IOdZsy190:~#

至此,我们就给ubuntu服务器创建了一个用户,并赋予这个用户管理权限这事就完成了。

文章版权声明:除非注明,否则均为崔牛网原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
AddoilApplauseBadlaughBombCoffeeFabulousFacepalmFecesFrownHeyhaInsidiousKeepFightingNoProbPigHeadShockedSinistersmileSlapSocialSweatTolaughWatermelonWittyWowYeahYellowdog
评论列表 (暂无评论,2169人围观)

还没有评论,来说两句吧...

目录[+]

取消
微信二维码
微信二维码
支付宝二维码