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. QT5.12 qtcreate 在Ubuntu14.04

    Ubuntu14.04 下出现了 symbol dbus_message_get_allow_interactive_authorization, version LIBDBUS_1_3 not de ...

  2. 前端解析 excel docx

    在研究中... https://www.npmjs.com/package/xlsx https://www.jianshu.com/p/68a420a68ded https://www.jiansh ...

  3. CentOS7下的CDH 6.2.0 安装过程

    #install OS centos 7.5#install lsb packageyum install -y redhat-lsb #install net-tools package yum i ...

  4. IfcBeam属性

    IfcBeam属性和结构 <xs:element name="IfcBeam" type="ifc:IfcBeam" substitutionGroup= ...

  5. python 使用sqlalchemy进行数据库操作

    sqlalchemy是python下一个著名的数据库orm库,可以方便地进行数据表创建.数据增删改查等操作 最详细的教程,见官方:https://docs.sqlalchemy.org 这里列举一些常 ...

  6. object literal对象字面量

    <JavaScript高级程序设计(第3版)>有个陌生的新词:对象字面量,无法理解.看了一下英文原版,英文是object literal ,还是不太理解.后来想明白了,主要是literal ...

  7. djang-celery使用带密码的redis

    前言: 网上很多django-celery使用redis(使用不带密码的redis)的用法都是千篇一律,那带密码的redis该怎么使用了呢,没有看到一篇有帮助的,在官网搜了下,发现以下用法,请看下面 ...

  8. LeetCode_485. Max Consecutive Ones

    485. Max Consecutive Ones Easy Given a binary array, find the maximum number of consecutive 1s in th ...

  9. 使用vs code开发.net core2.2时OmniSharp.MSBuild.ProjectLoader无法解析"xxx"的解决方法

    如图: 都是常用的nuget包呢,怎么无法解析呢? 第一反应是环境问题,果断搜索,baidu.google.bing.github一顿好搜啊,竟没有找到答案,看来是掉进了一个黄金坑! 重装vscode ...

  10. 【Docker学习之二】Docker部署安装

    环境 docker-ce-19.03.1-3.el7.x86_64 一.Docker的部署安装 Docker采用Linux(内核)技术,所以只能运行在Linux上,官方说Linux kernel至少3 ...