溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在centos 7中修改sshd

發(fā)布時(shí)間:2021-05-26 11:16:51 來(lái)源:億速云 閱讀:322 作者:Leah 欄目:服務(wù)器

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)如何在centos 7中修改sshd,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

1.新建用戶wwweee000

[root@localhost ~]# useradd wwweee000
[root@localhost ~]# passwd wwweee000
Changing password for user wwweee000.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.

2.#Port 22字段刪掉,將22改為其他不被使用的端口,服務(wù)器端口最大可以開(kāi)到65536.//注意Port是大寫(xiě)的"P"

[root@localhost ~]# cat -n /etc/ssh/sshd_config|grep "Port *"
  17 #Port 22
  100 #GatewayPorts no
[root@localhost ~]# cat /etc/ssh/sshd_config|grep -n "Port *"
17:#Port 22
100:#GatewayPorts no
[root@localhost ~]# awk "/Port */" /etc/ssh/sshd_config
#Port 22
#GatewayPorts no

上面還是不能滿足輸出結(jié)果:我們只要Port 22的答案.

[root@localhost ~]# cat /etc/ssh/sshd_config|grep -n "Port\ "      
17:#Port 22
[root@localhost ~]# cat -n /etc/ssh/sshd_config|grep "Port\ "      
  17 #Port 22
[root@localhost ~]# cat /etc/ssh/sshd_config|grep -n "Port\ "      
17:#Port 22
[root@localhost ~]# awk "/Port\ /" /etc/ssh/sshd_config         
#Port 22
[root@localhost ~]# sed -i "17s/#Port 22/Port 22/g" /etc/ssh/sshd_config 
[root@localhost ~]# awk "/Port\ /" /etc/ssh/sshd_config         
Port 22

-n顯示行號(hào);\反斜杠實(shí)質(zhì)定義

3.不使用vi/vim進(jìn)行修改Port 22 為 4096

[root@localhost ~]# sed -i "17s/Port 22/Port 4096/g" /etc/ssh/sshd_config 
[root@localhost ~]# cat /etc/ssh/sshd_config|grep "Port\ "
Port 4096

4.修改 #PermitRootLogin yes 為 PermitRootLogin no

[root@localhost ~]# cat -n /etc/ssh/sshd_config|grep "PermitRootLogin"
  49 #PermitRootLogin yes
  104 # the setting of "PermitRootLogin without-password".
[root@localhost ~]# sed -i "49s/#PermitRootLogin yes/PermitRootLogin no/g" /etc/ssh/sshd_config
[root@localhost ~]# cat -n /etc/ssh/sshd_config|grep "PermitRootLogin no"
  49 PermitRootLogin no

5.重啟sshd服務(wù)

[root@localhost ~]# systemctl restart sshd.service

6.使用 wwweee000 用戶登錄變化

[wwweee000@localhost ~]$

user普通用戶不能編輯/etc/ssh/sshd_config,需要切換root才能編輯,切換root命令 :  su

7.root進(jìn)行運(yùn)行,編寫(xiě)shell腳本。//腳本通用centos 7 (主要:sshd_config端口號(hào)定義輸入對(duì)root登錄禁止)

#!/bin/bash
sshd_Port=`cat /etc/ssh/sshd_config|grep "Port\ "`
echo "  當(dāng)前sshd端口:$sshd_Port"
read -ep "  請(qǐng)輸入sshd服務(wù)的連接端口Port (1-65536);
  請(qǐng)確保其他端口沖突和防火墻端口開(kāi)放業(yè)務(wù):" sshd_Port_read
echo "  你輸入的端口號(hào):$sshd_Port_read"
sed -i "s/$sshd_Port/Port $sshd_Port_read/g" /etc/ssh/sshd_config
echo "  已經(jīng)設(shè)置端口為:`cat /etc/ssh/sshd_config|grep "Port\ "`"
sed -i "s/#PermitRootLogin yes/PermitRootLogin no/g" /etc/ssh/sshd_config
systemctl restart sshd.service
exit 0

運(yùn)行測(cè)試(可循環(huán)使用)本次防火墻關(guān)閉/生產(chǎn)服務(wù)器都是關(guān)閉防火墻的

[root@localhost wwweee000]# firewall-cmd --state 
not running
[root@localhost ~]# sh sshd_config_Port.sh 
  當(dāng)前sshd端口:Port 22
  請(qǐng)輸入sshd服務(wù)的連接端口Port (1-65536)
  請(qǐng)確保其他端口沖突和防火墻端口開(kāi)放業(yè)務(wù):4096
  你輸入的端口號(hào):4096
  已經(jīng)設(shè)置端口為:Port 4096

上述就是小編為大家分享的如何在centos 7中修改sshd了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI