内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

CentOS如何新建用戶并使能密鑰登錄-創(chuàng)新互聯(lián)

小編給大家分享一下CentOS如何新建用戶并使能密鑰登錄,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),雙流企業(yè)網(wǎng)站建設(shè),雙流品牌網(wǎng)站建設(shè),網(wǎng)站定制,雙流網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,雙流網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

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

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

新建用戶

在 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命令的一個(gè)軟連接;

關(guān)于軟連接,你可以暫時(shí)把它理解成 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命令并不會(huì)在/home/下創(chuàng)建對(duì)應(yīng)的用戶目錄,如果想要?jiǎng)?chuàng)建,需要在命令中添加-m (--create-home)選項(xiàng);但是,CentOS 會(huì)為我們自動(dòng)創(chuàng)建這個(gè)用戶目錄;

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

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

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

[luizyao@centos_7_6_1810 ~]$ whoami
luizyao

為新用戶授權(quán)

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

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

注意:此時(shí),以新用戶的身份登錄系統(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 列表中:

注意:此時(shí),以 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命令查看用戶所屬的用戶組;

這個(gè)時(shí)候,新用戶就可以借助 root 的權(quán)限執(zhí)行命令了:

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

注意:

這種方法下,執(zhí)行sudo命令需要輸入新用戶的密碼,因?yàn)檫@是wheel用戶組的默認(rèn)配置,如下所示:

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

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

請(qǐng)使用visodu命令修改/etc/sudoers文件,因?yàn)樗鼤?huì)幫你檢查語法錯(cuò)誤;

2.你也可以在/etc/sudoers.d目錄下,為新用戶添加一個(gè)專門的配置文件(推薦):

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 可以在任何主機(jī)上(第一個(gè)ALL)以任何用戶的身份(第二個(gè)ALL,默認(rèn)為 root)執(zhí)行任何命令(第三個(gè)ALL),并且不需要密碼:

[luizyao@centos_7_6_1810 root]$ sudo whoami
root

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

新用戶使能 SSH 密鑰登錄

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

創(chuàng)建密鑰對(duì):

[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): # 選擇密鑰對(duì)存放的文件夾 
Created directory '/home/luizyao/.ssh'.
Enter passphrase (empty for no passphrase): # 私鑰的密碼
Enter same passphrase again: # 確認(rèn)私鑰密碼
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 的實(shí)踐;

使用scp命令下載私鑰:

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

此時(shí),我們?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

注意:

因?yàn)槲抑安]有 authorized_keys 文件,所以這里我直接重命名;如果之前已經(jīng)有 authorized_keys 文件,可以使用以下命令,把公鑰添加到文件末尾:

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

注意 authorized_keys 文件、~/.ssh/ 目錄、或者 用戶的 home 目錄(/home/luizyao/)對(duì)其他用戶賦予了寫入的權(quán)限,那么sshd判斷此文件已經(jīng)不安全,將不會(huì)使用這個(gè)文件,除非你已經(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”.

此時(shí),我們就可以使用 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);

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

再重啟 SSH 服務(wù):

[luizyao@centos_7_6_1810 ~]$ sudo systemctl restart sshd

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

其它

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

禁止 root 用戶使用 SSH 登錄

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

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

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

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

將firewalld 關(guān)于 ssh 的默認(rèn)配置文件,復(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

以上是“CentOS如何新建用戶并使能密鑰登錄”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

本文標(biāo)題:CentOS如何新建用戶并使能密鑰登錄-創(chuàng)新互聯(lián)
分享地址:http://www.rwnh.cn/article10/ceghdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)App設(shè)計(jì)、自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、App開發(fā)、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
南和县| 肇州县| 临朐县| 泸定县| 夏邑县| 沙河市| 朔州市| 常德市| 津市市| 抚州市| 石门县| 九寨沟县| 扬州市| 潜山县| 武汉市| 黔东| 海伦市| 武城县| 东辽县| 香港| 灌云县| 中方县| 南岸区| 巴里| 德保县| 和平县| 霍城县| 民勤县| 房山区| 清涧县| 钟祥市| 光山县| 大冶市| 大兴区| 赤水市| 龙井市| 临湘市| 和顺县| 礼泉县| 肇州县| 玉环县|