linux创建用户并授予sudo权限

版权声明:作者:傲娇天子 博文主页地址:https://blog.csdn.net/qq_41116956 欢迎转载,转载请在文章页面明显位置给出原文链接,谢谢 https://blog.csdn.net/qq_41116956/article/details/82661381

说道创建用户大家也许有很多方法了,这里先来一个绝对不会有错的方法:(推荐方法)

创建用户:useradd -m 用户名

设置密码:passwd 用户名

设置密码:******(这个地方也许不会显示)

确认密码:******

给予用户sudo权限以可以登录到root权限

vim /etc/sudoers(当然最好不要这么做,不过这里问题不大。而且这个这个命令需要root权限,文章后面有正确的做法)

注意:/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"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL    #注意,一定是以此行为基准
hadoop  ALL=(ALL:ALL) ALL    #不要选到下面的%admin了,不然会错误,而且改起来很麻烦
es    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL          #说的就是这行,改错了就需要root的权限了,但是如果没有root呢?

# 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

来看下虚拟机上的sudoers文件是什么样的:

注意:不要#后面的备注哦

#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults   env_keep += "HOME"

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on 
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
##      user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere 
root    ALL=(ALL)       ALL    #这里来,也是复制root这一行,只需要把root改成对应的用户名即可
es      ALL=(ALL)       ALL
## Allows members of the 'sys' group to run networking, software, 
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

## Allows people in group wheel to run all commands
# %wheel        ALL=(ALL)       ALL

## Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL

## Allows members of the users group to mount and unmount the 
## cdrom as root
# %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

## Allows members of the users group to shutdown this system
# %users  localhost=/sbin/shutdown -h now

做到这里就ok了,下面是比较优秀的做法:


创建用户名

useradd -d /home/crawler -m crawler -s /bin/sh

如果是/bin/bash环境,修改方式如下

vim /etc/passwd
es:x:500:500::/home/es:/bin/bash

设置密码:

passwd 用户名(这里和上面一样,输入2次密码即可)


好了,上面基本解决问题,当然,修改/etc/sudoers文件应该使用visudo命令,这样会提示你/etc/sudoers文件是否有错误,如果有错误就很麻烦了,当然,如果你能登录root也不算很麻烦:

使用方法:

[***@***]$:sudo visudo

下面附带不能登陆root的解决方法:


这里说下情况,小博主并不知道root的密码,所以平常使用root权限的时候都是sudo su命令,今天的问题是,我添加了一个新的用户,在增加sudo权限的时候,将/etc/sudoers文件改错了,所以sudo 命令报错,而修改/etc/sudoers文件需要root权限,但是我有不能使用sudo,这样怎么将/etc/sudoers文件修改回来呢?下面说的就是这个方法:

Ubuntu 16.04中进入单用户模式:(这里是开机F12选择Ubuntu,然后立即Esc键,进入Ubuntu高级选项)

注意:有地地方是一直按Shift或者直接Esc键也有F10,总而言之,就是要进入Ubuntu的单用户模式,有些可以选择Ubuntu高级选项

选择root选项,连续回车即可

进入root操作界面:

chmod 777 /dev/null
mount -o remount rw /
vi /etc/sudoers

当然,我用的是vim编辑器,但是奇怪的是并不能修改文件,所以实在不行也可以使用nano编辑器

最后exit,选择第一个resume重新启动即可

猜你喜欢

转载自blog.csdn.net/qq_41116956/article/details/82661381