CentOS新建用户并使能密钥登录的方法

新建用户 为新用户授权 新用户使能 SSH 密钥登录 其它 禁止 root 用户使用 SSH 登录 使用非常规的 SSH…

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

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

    新建用户

    在 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

    为新用户授权

    通常情况下,新用户在自己的用户目录(/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 的身份登录系统;

    方法一:把新用户添加到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

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

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

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

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

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

    bash [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

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

    新用户使能 SSH 密钥登录

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

    创建密钥对:

    [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]—–+

    下载私钥到本地:

    基于 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”: # 输入私钥密码,登录失败
    [email protected] password: # luizyao 的用户密码
    Last login: Tue Sep 17 22:50:22 2019

    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 con‐
    tent 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 unautho‐
    rized 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 [email protected]
    Enter passphrase for key “/Users/yaomeng/.ssh/id_ecdsa”: # 私钥密码
    Last login: Wed Sep 18 00:00:41 2019 from 49.65.108.161

    去使能 SSH 密码登录

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

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

    再重启 SSH 服务:

    [luizyao@centos_7_6_1810 ~]$ sudo systemctl restart sshd

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

    其它

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

    禁止 root 用户使用 SSH 登录

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

    使用非常规的 SSH 端口

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

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

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

    [luizyao@centos_7_6_1810 ~]$ sudo cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/

    修改配置文件中的端口配置:

    <!– /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>

    重载 firewalld 配置:

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

    禁 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

    总结

    以上所述是小编给大家介绍的CentOS新建用户并使能密钥登录的方法,大家如有疑问可以留言,或者联系站长。感谢亲们支持!!!
    如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

    香港服务器首选港服(Server.HK),2H2G首月10元开通。
    港服(Server.HK)(www.IDC.Net)提供简单好用,价格厚道的香港/美国云服务器和独立服务器。IDC+ISP+ICP资质。ARIN和APNIC会员。成熟技术团队15年行业经验。

    为您推荐

    ssh远程超时中断的解决办法

    有时我们网络正常的,但SSH连接经常出现中断的情况,以及在SSH远程时很慢的问题。 这是由于OpenSSL服务默认启用了...

    Linux系统防火墙放行端口

    如果您服务器内安装了宝塔面板,请直接登陆宝塔面板,安全,里面添加放行端口。如果添加后不生效,把防火墙开关一下即可。本教程...

    Linux主机简单判断被CC攻击的网站命令-比较直接有效

    CC攻击很容易发起,并且几乎不需要成本,导致现在的CC攻击越来越多。 大部分搞CC攻击的人,都是用在网上下载的工具,这些...

    linux环境下测试get和post请求

    Linux环境下测试get和post请求 ?get,post,curl   get请求 curl: curl ...

    umount卸载磁盘提示target is busy

    umount卸载磁盘提示target is busy. (目标忙) 的问题解决方案   umount卸载磁盘提...
    返回顶部