CentOS -- 新建用戶並使能密鑰登錄


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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章