CentOS 默认只有一个 root 用户,但是 root 用户的权限过大,而且不利于多人协作,基于权限管理和安全的原因,我们为系统新建一个用户,并且使能其 SSH 登录,同时禁止 root 用户的登录;

基于CentOS Linux release 7.6.1810 (Core)实践;

1. 新建用户

在 CentOS 中,adduseruseradd没有区别:

[root@centos_7_6_1810 ~]# ll /usr/sbin/ | grep user
lrwxrwxrwx 1 root root 7 Jun 24 10:14 adduser -> useradd
-rwxr-xr-x. 1 root root 33104 Aug 3 2017 fuser
-rwxr-xr-x. 1 root root 15832 Apr 13 2018 lnewusers
-rwxr-xr-x. 1 root root 15752 Apr 13 2018 luseradd
-rwxr-xr-x. 1 root root 11576 Apr 13 2018 luserdel
-rwxr-xr-x. 1 root root 19896 Apr 13 2018 lusermod
-rwxr-xr-x 1 root root 76232 Mar 14 2019 newusers
-rwxr-xr-x 1 root root 33072 Mar 14 2019 runuser
-rwxr-xr-x. 1 root root 19720 Apr 11 2018 sasldblistusers2
-rwxr-x--- 1 root root 118224 Mar 14 2019 useradd
-rwxr-x--- 1 root root 80400 Mar 14 2019 userdel
-rwxr-x--- 1 root root 113856 Mar 14 2019 usermod
-rwsr-xr-x. 1 root root 11376 Oct 31 2018 usernetctl

从上面的命令中可以看出:adduser只不过是useradd命令的一个软连接;

关于软连接,你可以暂时把它理解成 Windows 系统中的快捷方式;

使用useradd命令创建新用户:

[root@centos_7_6_1810 ~]# useradd luizyao
[root@centos_7_6_1810 ~]# ls /home/
luizyao

在大多数 Linux 的发行版本中,useradd命令并不会在/home/下创建对应的用户目录,如果想要创建,需要在命令中添加-m (--create-home)选项;但是,CentOS 会为我们自动创建这个用户目录;

如果我们想要以这个用户名登录系统,必须为其设置一个密码:

[root@centos_7_6_1810 ~]# passwd luizyao
Changing password for user luizyao.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

然后,我们就可以用这个用户登录系统:

[luizyao@centos_7_6_1810 ~]$ whoami
luizyao

2. 为新用户授权

通常情况下,新用户在自己的用户目录(/home/luizyao/)下拥有完整的权限,其它目录需要他人授权;而我们最常用的就是 root 用户的权限,这时候sudo命令就可以帮助到我们:它允许信任的用户以其他用户的身份去执行命令,默认使用的是 root 用户;

新用户并不在信任名单中,所以我们无法借用 root 用户身份去执行命令:

注意:此时,以新用户的身份登录系统的;

[luizyao@centos_7_6_1810 /]$ sudo whoami
[sudo] password for luizyao:
luizyao is not in the sudoers file. This incident will be reported.

在 CentOS 中,我们有两种方法把新用户添加到 Sudoers 列表中:

注意:此时,以 root 的身份登录系统;

2.1. 方法一:把新用户添加到wheel用户组中

基于 RedHat 分发版本的系统,如 CentOS 和 Fedora,用户组wheel已经被授予 sudo 的权限;所以,我们可以通过把新用户添加到wheel用户组中,来获取 sudo 的权限:

[root@centos_7_6_1810 ~]# groups luizyao
luizyao : luizyao
[root@centos_7_6_1810 ~]# usermod -aG wheel luizyao
[root@centos_7_6_1810 ~]# groups luizyao
luizyao : luizyao wheel

我们通过usermod命令把新用户添加到wheel用户组中,可以使用groups命令查看用户所属的用户组;

这个时候,新用户就可以借助 root 的权限执行命令了:

[luizyao@centos_7_6_1810 root]$ sudo whoami
[sudo] password for luizyao:
root

注意:

  • 这种方法下,执行sudo命令需要输入新用户的密码,因为这是wheel用户组的默认配置,如下所示:

    # /etc/sudoers
    
    106 ## Allows people in group wheel to run all commands
    107 %wheel ALL=(ALL) ALL
    108
    109 ## Same thing without a password
    110 # %wheel ALL=(ALL) NOPASSWD: ALL
  • 从用户组中删除用户。可以使用如下命令:

    [root@centos_7_6_1810 ~]# gpasswd -d luizyao wheel
    Removing user luizyao from group wheel
    [root@centos_7_6_1810 ~]# groups luizyao
    luizyao : luizyao

2.2. 方法二:把新用户添加到sudoers列表中

/etc/sudoers文件中,可以配置用户和用户组的 sudo 权限,这种方式更加灵活一点;并且,有两种方法为新用户配置权限:

  • 你可以直接在/etc/sudoers文件中配置新用户的权限,但是要注意这个文件的默认权限是只读的,所以你要先添加写入权限,编辑完以后,再恢复为只读;

    请使用visodu命令修改/etc/sudoers文件,因为它会帮你检查语法错误;

  • 你也可以在/etc/sudoers.d目录下,为新用户添加一个专门的配置文件(推荐):

    [root@centos_7_6_1810 ~]# echo "luizyao  ALL=(ALL) NOPASSWD:ALL" | tee /etc/sudoers.d/luizyao
    luizyao ALL=(ALL) NOPASSWD:ALL
    [root@centos_7_6_1810 ~]# ll /etc/sudoers.d/luizyao
    -rw-r--r-- 1 root root 32 Sep 17 17:51 /etc/sudoers.d/luizyao

    上述命令表示:luizyao 可以在任何主机上(第一个ALL)以任何用户的身份(第二个ALL,默认为 root)执行任何命令(第三个ALL),并且不需要密码:

    [luizyao@centos_7_6_1810 root]$ sudo whoami
    root

    注意:文件的名字可以是任意的,只是通常我们会配置成用户名;

3. 新用户使能 SSH 密钥登录

此时,以新用户的身份登录系统;

  1. 创建密钥对:

    [luizyao@centos_7_6_1810 ~]$ ssh-keygen -t ecdsa  # 椭圆曲线数字签名算法
    Generating public/private ecdsa key pair.
    Enter file in which to save the key (/home/luizyao/.ssh/id_ecdsa): # 选择密钥对存放的文件夹
    Created directory '/home/luizyao/.ssh'.
    Enter passphrase (empty for no passphrase): # 私钥的密码
    Enter same passphrase again: # 确认私钥密码
    Your identification has been saved in /home/luizyao/.ssh/id_ecdsa.
    Your public key has been saved in /home/luizyao/.ssh/id_ecdsa.pub.
    The key fingerprint is:
    SHA256:FljQN9JFxB/C83Mv7N3rFNLCxXICRxaKzKDb+Tzsgwo luizyao@centos_7_6_1810
    The key's randomart image is:
    +---[ECDSA 256]---+
    | .+.. B==. |
    | .o* = X o |
    | .. .* o B = |
    | o .. . X .|
    | . oS = =.|
    | .+ = o|
    | E .= . +.|
    | . .... o o|
    | .. .. .o.|
    +----[SHA256]-----+
  2. 下载私钥到本地:

    基于 Mac OS 的实践;

    使用scp命令下载私钥:

    yaomengdeMacBook-Air:~ yaomeng$ scp luizyao@<ip 地址>:/home/luizyao/.ssh/id_ecdsa ~/.ssh/

    此时,我们仍然需要密码登录:

    yaomengdeMacBook-Air:~ yaomeng$ ssh luizyao@<ip 地址>
    Enter passphrase for key "/Users/yaomeng/.ssh/id_ecdsa": # 输入私钥密码,登录失败
    luizyao@<ip 地址> password: # luizyao 的用户密码
    Last login: Tue Sep 17 22:50:22 2019
  3. SSH 免密登录

    重命名公钥为authorized_keys

    [luizyao@centos_7_6_1810 ~]$ mv ~/.ssh/id_ecdsa.pub ~/.ssh/authorized_keys
    [luizyao@centos_7_6_1810 ~]$ ll ~/.ssh/
    total 8
    -rw-r--r-- 1 luizyao luizyao 185 Sep 17 22:58 authorized_keys
    -rw------- 1 luizyao luizyao 314 Sep 17 22:58 id_ecdsa

    注意:

    • 因为我之前并没有 authorized_keys 文件,所以这里我直接重命名;如果之前已经有 authorized_keys 文件,可以使用以下命令,把公钥添加到文件末尾:

          cat >> ~/.ssh/authorized_keys < ~/.ssh/id_ecdsa.pub
    • authorized_keys 文件、~/.ssh/ 目录、或者 用户的 home 目录(/home/luizyao/)对其他用户赋予了写入的权限,那么sshd判断此文件已经不安全,将不会使用这个文件,除非你已经设置 StrictModes 为 no;

      你可以通过man sshd命令查看帮助文档:

            ~/.ssh/authorized_keys
      Lists the public keys (DSA, ECDSA, Ed25519, RSA) that can be used for logging in as this user. The format of this file is described above. The content of the file is not highly sensitive, but the recommended permissions are read/write for the user, and not accessible by others. If this file, the ~/.ssh directory, or the user's home directory are writable by other users, then the file could be modified or replaced by unauthorized users. In this case, sshd will not allow it to be used unless the StrictModes option has been set to “no”.

    此时,我们就可以使用 SSH 免密登录:

    yaomengdeMacBook-Air:~ yaomeng$ ssh luizyao@<ip 地址>
    Enter passphrase for key "/Users/yaomeng/.ssh/id_ecdsa": # 私钥密码
    Last login: Wed Sep 18 00:00:41 2019 from 49.65.108.161
  4. 去使能 SSH 密码登录

    现在,我们仍然可以使用密码登录,这还是不安全的,现在我们就来禁止使用密码登录系统;

    对于 CentOS 系统来说,只需要修改 SSH 配置文件/etc/ssh/sshd_config中的PasswordAuthenticationno

  5. 重启 SSH 服务:

    [luizyao@centos_7_6_1810 ~]$ sudo systemctl restart sshd

    我们便禁止了 SSH 的密码登录,只能使用密钥登录;

4. 其它

为了进一步提升系统的安全性,我们还可以做一些事情:

4.1. 禁止 root 用户使用 SSH 登录

只需要修改 SSH 配置文件/etc/ssh/sshd_config中的PermitRootLoginno,再重启 SSH 服务;

4.2. 使用非常规的 SSH 端口

默认的 SSH 端口是22,我们可以修改为不常用的端口:修改 SSH 配置文件/etc/ssh/sshd_config中的Port值(例如:10178),再重启 SSH 服务;

我们还需要修改防火墙中有关 sshd 的配置,CentOS 7 默认使用 firewalld 防火墙,我们对其做如下配置:

  1. 将firewalld 关于 ssh 的默认配置文件,复制到系统配置文件夹内:

    [luizyao@centos_7_6_1810 ~]$ sudo cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/
  2. 修改配置文件中的端口配置:

    <!-- /etc/firewalld/services/ -->
    
    <?xml version="1.0" encoding="utf-8"?>
    <service>
    <short>SSH</short>
    <description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
    <port protocol="tcp" port="10178"/>
    </service>
  3. 重载 firewalld 配置:

    [luizyao@centos_7_6_1810 ~]$ sudo firewall-cmd --reload
    success

4.3. 禁 ping

为防火墙添加如下规则,并重载配置:

[luizyao@centos_7_6_1810 ~]$ sudo firewall-cmd --permanent --add-icmp-block=echo-reply
[luizyao@centos_7_6_1810 ~]$ sudo firewall-cmd --permanent --add-icmp-block=echo-request
[luizyao@centos_7_6_1810 ~]$ sudo firewall-cmd --reload

4.4. 安装 dnf 包管理器

dnf 是新一代的 rpm 软件包管理器,提升了性能。他首先出现在 Fedora 18 这个发行版中。目前,其正式成为 Fedora 22 默认包管理器;

[luizyao@centos_7_6_1810 ~]$ sudo yum install dnf

CentOS -- 新建用户并使能密钥登录的更多相关文章

  1. Centos 新建用户

    Centos 新建用户 为什么要新建用户? 因为root的权限太多,不方便多人多角色使用,所以添加一个用户 添加用户 新建用户 adduser '用户名' 添加用户密码 passwd '用户名' 输入 ...

  2. mysql新建用户在本地无法登录

    新建了一个mysql用户,但是无法在本地登录,即使已经授权任一ip都可以登录,甚至特地写清楚localhost登录,还是不行,情况如下 [root@localhost zabbix-release-3 ...

  3. 关于CentOS普通用户无法登录SSH问题

    在CentOS中,假若一切都顺利的话,你建新一个用户,它就自动添加到SSHD登录服务中的处的了,不必手动或再设置什么,但有些情况下,我们的系统只允许root用户登录,那么,我们就要进行一些必要的设置, ...

  4. Linux实战案例(4)CentOS清除用户登录记录和命令历史方法

    CentOS清除用户登录记录和命令历史方法 清除登陆系统成功的记录[root@localhost root]# echo > /var/log/wtmp //此文件默认打开时乱码,可查到ip等信 ...

  5. ZH奶酪:Linux新建用户+远程登录

    7.想在VBUbuntu中新建一个账户,但是用useradd命令发现没有权限,原来Ubuntu默认不是root权限登录,需要按照下边的教程才能新建用户. http://www.linuxidc.com ...

  6. mysql新建用户本地无法登录

    mysql新建用户本地无法登录 MySQLDebianGoogleAccess  出此是用mysql,因为root权限过高,所以新建一用户appadmin,权限仅为要用到的数据库.创建语句如下:gra ...

  7. [转]在 SQL Server 2008 中新建用户登录并指定该用户的数据库

    提要:我在 SQL Server 中新建用户登录时,出现了三种错误,错误代码分别是 18456.15128.4064 -----------------------------------  正 文 ...

  8. 【转载】SQL Server 2008 中新建用户登录并指定该用户的数据库

    提要:我在 SQL Server 中新建用户登录时,出现了三种错误,错误代码分别是 18456.15128.4064 -----------------------------------  正 文 ...

  9. linux新建用户登录不了

    useradd----创建用户命令 简单的创建普通用户(当然得在root登录下执行) useradd username -p password userdel username 删除用户 用上面的命令 ...

随机推荐

  1. sql server 使用链接服务器远程查询

    --PKselect * from sys.key_constraints where object_id = OBJECT_ID('TB')--FKselect * from sys.foreign ...

  2. Parallel.For循环与普通的for循环

    前两天看书发现了一个新的循环Parallel.For,这个循环在循环期间可以创建多个线程并行循环,就是说循环的内容是无序的.这让我想到了我前面的牛牛模拟计算是可以用到这个循环的,我前面的牛牛模拟计算是 ...

  3. github将本地仓库的代码上传到Github

    本篇主要参考博文:https://blog.csdn.net/IT_faquir/article/details/52516214 你要先完成上一篇的操作,即将代码上传到本地仓库中,才能上传到gith ...

  4. odoo开发笔记 -- 用户字段值,默认给当前登录用户

    场景描述: 在一些视图下,当系统用户创建某条记录的时候,需要给某个用户字段设置默认值,即:默认值为系统的当前登录用户,如何实现? 处理方式: 在定义模型的时候,给该字段赋值就可以: operator_ ...

  5. fgets注意事项

    这是yjy的习题库,中途我在使用fgest时颇费了一点心思,特此记录一下. #include <stdio.h> #include <string.h> #include &l ...

  6. 登陆服务器提示“You need to run "nvm install N/A" to install it before using it.”

    一.登陆服务器提示“You need to run "nvm install N/A" to install it before using it.” 二.执行命令: nvm ls ...

  7. flutter upgrade之后出现Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from

    错误信息 Initializing gradle... Resolving dependencies... Running Gradle task 'assembleDebug'... /Users/ ...

  8. Golang常见小细节总结(1)

    本系列不定期更新,用于记录平常开发过程中出现的一些小问题 Array 类型的值作为函数参数    可以理解slice是对array的一个视图,底层还是array所以会被修改 通过map的ok来确 ...

  9. 如何优雅的使用telnet测试端口连通性

    telnet命令是TELNET协议的用户接口,它支持两种模式:命令模式和会话模式,虽然telnet支持许多命令,但大部分情况下,我们只是使用它查看目标主机是否打开了某端口(默认是23). 其执行结果有 ...

  10. DRF概述

    目录 一. REST 1. 什么是编程? 2. 什么是REST? 二. 知识准备 1. CBV(class based view) 2. 类方法 classmethod和classonlymethod ...