Linux创建新用户

##

useradd -g root kk -p yw2667899

然后新建用户 adduser username 创建的同时会让你输入密码和一些基本信息,基本信息可以不填

将用户添加到 sudo 组 adduser username sudo

加sudo权限

3.添加root权

添加root权限

如果需要让此用户有root权限,执行命令: sudo vim /etc/sudoers

修改文件如下:

User privilege specification

root ALL=(ALL) ALL username ALL=(ALL) ALL 保存退出,username 用户就拥有了root权限。 ———————————————— 版权声明:本文为CSDN博主「breeze5428」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/breeze5428/article/details/52837768

更多配置

使用一行命令指定用户名、密码和用户组:

useradd -g root -p $(echo mypasswd | openssl passwd -1 -stdin) username

使用该命令不会为新用户配置默认目录,所以更好的定义方法应该如下:

useradd -u ABCDE -g users -d /home/username -s /bin/bash -p $(echo mypasswd | openssl passwd -1 -stdin) username

其中,密码调用了openssl进行加密(默认必须加密,否则无法登陆)。

In Linux, useradd is used to configure everything including username and password. For security reasons, the password should in encrypted, and you can use openssl for making md5 passwords, this helps to specify the password if it’s in plain text.
Openssl passwd will generate hash of mypasswd to be used as secure password.

默认没有给sudo 用户组sudo 权限。打开/etc/sudoers文件,可以看到:

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

可取消注释赋予整个sudo组root权限。

也可以通过以下命令赋予某一特定用户权限

echo "user1 All=(ALL:ALL) ALL" >> /etc/sudoers

注意:使用>>以附加,如果>会全部删除重写

使用密钥

使用密钥进行登陆时,密钥文件放在相应用户家目录的文件夹里

参考:

  1. https://www.jianshu.com/p/138398b6bc72 add sudo
  2. https://quickfever.com/linux-create-user-with-password/ one line(passwd)
  3. https://www.tecmint.com/13-basic-cat-command-examples-in-linux/ 关于cat
  4. https://www.baeldung.com/linux/file-append-lines 关于echo
  5. https://blog.csdn.net/qq_15504597/article/details/82839414 使用密钥进行登陆