0
点赞
收藏
分享

微信扫一扫

Linux新建用户

90哦吼 2022-01-04 阅读 76

Linux新建用户

一、创建用户

1、创建一个新用户:pdx_haokai

 [root@VM-0-3-centos ~]# adduser pdx_haokai

2、为新用户创建初始化密码

passwd 用户名
然后输入和确认该用户的密码

[root@VM-0-3-centos ~]# passwd pdx_haokai
Changing password for user pdx_haokai.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

二、用户授权

个人用户的权限只可以在本home下有完整权限,其他目录需要别人授权。经常需要root用户的权限,可以通过修改sudoers文件来赋予权限。

新创建的用户并不能使用sudo命令,需要给他添加授权。

1、查找sudoers文件路径并赋予权限

相关命令含义:

 whereis sudoers                     						# 查找sudoers文件路径
 ls -l /etc/sudoers                  						# 查看权限
 -r--r----- 1 root root 4353 Jan  4 21:20 /etc/sudoers		# 只有读权限
 chmod -v u+w /etc/sudoers	 								# 赋予读写权限
 [root@VM-0-3-centos ~]# whereis sudoers
sudoers: /etc/sudoers.d /etc/sudoers /usr/share/man/man5/sudoers.5.gz
[root@VM-0-3-centos ~]# ls -l /etc/sudoers
-r--r----- 1 root root 4353 Jan  4 21:20 /etc/sudoers
[root@VM-0-3-centos ~]# chmod -v u+w /etc/sudoers
mode of ‘/etc/sudoers’ changed from 0440 (r--r-----) to 0640 (rw-r-----)
[root@VM-0-3-centos ~]#

2、修改sudoers文件

输入命令 vim /etc/sudoers 修改sudoers文件,添加新用户信息:

Allow root to run any commands anywhere

root ALL=(ALL) ALL
prefma ALL=(ALL) ALL #这个是新用户

然后输入命令 wq! 保存修改。

在这里插入图片描述

为什么用wq!来执行保存

用vi vim修改文件时遇到一个问题,当我:wq时,提示’readonly’ option is set (add ! to override)

这是因为权限的问题造成的,这个文件现在是只读不能写的,在修改前应该执行sudo

但我们修改了,只需要对其进行强制保存,执行:wq!即可保存并退出

3、收回权限

[root@VM-0-3-centos ~]# chmod -v u-w /etc/sudoers
mode of ‘/etc/sudoers’ changed from 0640 (rw-r-----) to 0440 (r--r-----)

4、新用户登录

新建连接,使用新创建的用户登录,并进行验证,比如:

[root@VM-0-3-centos ~]$ pwd
/home/prefma
[root@VM-0-3-centos ~]$ ls -l /etc/sudoers
-r--r----- 1 root root 3995 Oct 16 22:42 /etc/sudoers
举报

相关推荐

0 条评论