主頁 > 知識庫 > CentOS新建用戶并使能密鑰登錄的方法

CentOS新建用戶并使能密鑰登錄的方法

熱門標簽:硅谷的囚徒呼叫中心 檢查注冊表項 百度競價點擊價格的計算公式 阿里云 美圖手機 網(wǎng)站建設(shè) 使用U盤裝系統(tǒng) 智能手機

CentOS 默認只有一個 root 用戶,但是 root 用戶的權(quán)限過大,而且不利于多人協(xié)作,基于權(quán)限管理和安全的原因,我們?yōu)橄到y(tǒng)新建一個用戶,并且使能其 SSH 登錄,同時禁止 root 用戶的登錄;

基于CentOS Linux release 7.6.1810 (Core)實踐;

新建用戶

在 CentOS 中,adduseruseradd沒有區(qū)別:

[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命令的一個軟連接;

關(guān)于軟連接,你可以暫時把它理解成 Windows 系統(tǒng)中的快捷方式;

使用useradd命令創(chuàng)建新用戶:

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

在大多數(shù) Linux 的發(fā)行版本中,useradd命令并不會在/home/下創(chuàng)建對應(yīng)的用戶目錄,如果想要創(chuàng)建,需要在命令中添加-m (--create-home)選項;但是,CentOS 會為我們自動創(chuàng)建這個用戶目錄;

如果我們想要以這個用戶名登錄系統(tǒng),必須為其設(shè)置一個密碼:

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

然后,我們就可以用這個用戶登錄系統(tǒng):

[luizyao@centos_7_6_1810 ~]$ whoami
luizyao

為新用戶授權(quán)

通常情況下,新用戶在自己的用戶目錄(/home/luizyao/)下?lián)碛型暾臋?quán)限,其它目錄需要他人授權(quán);而我們最常用的就是 root 用戶的權(quán)限,這時候sudo命令就可以幫助到我們:它允許信任的用戶以其他用戶的身份去執(zhí)行命令,默認使用的是 root 用戶;

新用戶并不在信任名單中,所以我們無法借用 root 用戶身份去執(zhí)行命令:

注意:此時,以新用戶的身份登錄系統(tǒng)的;

[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 的身份登錄系統(tǒng);

方法一:把新用戶添加到wheel用戶組中

基于 RedHat 分發(fā)版本的系統(tǒng),如 CentOS 和 Fedora,用戶組wheel已經(jīng)被授予 sudo 的權(quán)限;所以,我們可以通過把新用戶添加到wheel用戶組中,來獲取 sudo 的權(quán)限:

[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 的權(quán)限執(zhí)行命令了:

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

注意:

這種方法下,執(zhí)行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

從用戶組中刪除用戶??梢允褂萌缦旅睿?/p>

[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 權(quán)限,這種方式更加靈活一點;并且,有兩種方法為新用戶配置權(quán)限:

1.你可以直接在/etc/sudoers文件中配置新用戶的權(quán)限,但是要注意這個文件的默認權(quán)限是只讀的,所以你要先添加寫入權(quán)限,編輯完以后,再恢復(fù)為只讀;

請使用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)執(zhí)行任何命令(第三個ALL),并且不需要密碼:

[luizyao@centos_7_6_1810 root]$ sudo whoami
root

注意:文件的名字可以是任意的,只是通常我們會配置成用戶名;

新用戶使能 SSH 密鑰登錄

此時,以新用戶的身份登錄系統(tǒng);

創(chuàng)建密鑰對:

[luizyao@centos_7_6_1810 ~]$ ssh-keygen -t ecdsa # 橢圓曲線數(shù)字簽名算法
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/

此時,我們?nèi)匀恍枰艽a登錄:

yaomengdeMacBook-Air:~ yaomeng$ ssh luizyao@<ip 地址>
Enter passphrase for key "/Users/yaomeng/.ssh/id_ecdsa": # 輸入私鑰密碼,登錄失敗
luizyao@www.luizyao.com 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 文件,所以這里我直接重命名;如果之前已經(jīng)有 authorized_keys 文件,可以使用以下命令,把公鑰添加到文件末尾:

cat >> ~/.ssh/authorized_keys < ~/.ssh/id_ecdsa.pub

注意 authorized_keys 文件、~/.ssh/ 目錄、或者 用戶的 home 目錄(/home/luizyao/)對其他用戶賦予了寫入的權(quán)限,那么sshd判斷此文件已經(jīng)不安全,將不會使用這個文件,除非你已經(jīng)設(shè)置 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 luizyao@www.luizyao.com
Enter passphrase for key "/Users/yaomeng/.ssh/id_ecdsa": # 私鑰密碼 
Last login: Wed Sep 18 00:00:41 2019 from 49.65.108.161

去使能 SSH 密碼登錄

現(xiàn)在,我們?nèi)匀豢梢允褂妹艽a登錄,這還是不安全的,現(xiàn)在我們就來禁止使用密碼登錄系統(tǒng);

對于 CentOS 系統(tǒng)來說,只需要修改 SSH 配置文件/etc/ssh/sshd_config中的PasswordAuthenticationno;

再重啟 SSH 服務(wù):

[luizyao@centos_7_6_1810 ~]$ sudo systemctl restart sshd

我們便禁止了 SSH 的密碼登錄,只能使用密鑰登錄;

其它

為了進一步提升系統(tǒng)的安全性,我們還可以做一些事情:

禁止 root 用戶使用 SSH 登錄

只需要修改 SSH 配置文件/etc/ssh/sshd_config中的PermitRootLoginno,再重啟 SSH 服務(wù);

使用非常規(guī)的 SSH 端口

默認的 SSH 端口是22,我們可以修改為不常用的端口:修改 SSH 配置文件/etc/ssh/sshd_config中的Port值(例如:10178),再重啟 SSH 服務(wù);

我們還需要修改防火墻中有關(guān) sshd 的配置,CentOS 7 默認使用 firewalld 防火墻,我們對其做如下配置:

將firewalld 關(guān)于 ssh 的默認配置文件,復(fù)制到系統(tǒng)配置文件夾內(nèi):

[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

為防火墻添加如下規(guī)則,并重載配置:

[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

總結(jié)

以上所述是小編給大家介紹的CentOS新建用戶并使能密鑰登錄的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

標簽:湖北 山南 湘潭 黃山 賀州 懷化 煙臺 通遼

巨人網(wǎng)絡(luò)通訊聲明:本文標題《CentOS新建用戶并使能密鑰登錄的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266