CenOS7 shell脚本重装OpenSSH

 编辑文件

vim resetsshd.sh

 

shell代码

#!/bin/bash

# 检查是否以 root 用户运行
if [ "$EUID" -ne 0 ]; then
  echo "请以 root 用户运行此脚本!"
  exit 1
fi

echo "开始重新安装 OpenSSH 服务器..."

# 停止 sshd 服务
echo "停止 sshd 服务..."
systemctl stop sshd

# 卸载现有的 OpenSSH 服务器
echo "卸载现有的 OpenSSH 服务器..."
yum remove openssh-server -y

# 删除旧的配置文件和目录(可选)
echo "删除旧的配置文件和目录..."
rm -rf /etc/ssh/
rm -rf /var/empty/sshd/

# 安装 OpenSSH 服务器
echo "安装 OpenSSH 服务器..."
yum install openssh-server -y

# 检查安装是否成功
if ! rpm -q openssh-server; then
  echo "OpenSSH 服务器安装失败,请检查错误日志!"
  exit 1
fi

# 创建默认的 sshd 配置文件(如果不存在)
if [ ! -f /etc/ssh/sshd_config ]; then
  echo "恢复默认的 sshd 配置文件..."
  cp /usr/share/doc/openssh-server/sshd_config /etc/ssh/sshd_config
fi

# 设置正确的权限
echo "设置正确的权限..."
chmod 600 /etc/ssh/sshd_config
chown root:root /etc/ssh/sshd_config

# 启动并启用 sshd 服务
echo "启动并启用 sshd 服务..."
systemctl start sshd
systemctl enable sshd

# 检查服务状态
echo "检查 sshd 服务状态..."
systemctl status sshd --no-pager

echo "OpenSSH 服务器已成功重新安装!"

执行代码

sh resetsshd.sh

安装过程:

开始重新安装 OpenSSH 服务器...
停止 sshd 服务...
Failed to stop sshd.service: Unit sshd.service not loaded.
卸载现有的 OpenSSH 服务器...
Loaded plugins: fastestmirror, post-transaction-actions
Repository epel is listed more than once in the configuration
No Match for argument: openssh-server
No Packages marked for removal
删除旧的配置文件和目录...
安装 OpenSSH 服务器...
Loaded plugins: fastestmirror, post-transaction-actions
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * elrepo: mirror.fcix.net
Resolving Dependencies
--> Running transaction check
---> Package openssh-server.x86_64 0:7.4p1-23.el7_9 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package               Arch          Version               Repository      Size
================================================================================
Installing:
 openssh-server        x86_64        7.4p1-23.el7_9        updates        459 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 459 k
Installed size: 970 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : openssh-server-7.4p1-23.el7_9.x86_64                         1/1 
  Verifying  : openssh-server-7.4p1-23.el7_9.x86_64                         1/1 

Installed:
  openssh-server.x86_64 0:7.4p1-23.el7_9                                        

Complete!
openssh-server-7.4p1-23.el7_9.x86_64
设置正确的权限...
启动并启用 sshd 服务...
检查 sshd 服务状态...
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2025-04-02 18:50:21 CST; 80ms ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 22597 (sshd)
   CGroup: /system.slice/sshd.service
           └─22597 /usr/sbin/sshd -D

Apr 02 18:50:21 adorepop.com systemd[1]: Starting OpenSSH server daemon...
Apr 02 18:50:21 adorepop.com sshd[22597]: Server listening on 0.0.0.0 port 22.
Apr 02 18:50:21 adorepop.com sshd[22597]: Server listening on :: port 22.
Apr 02 18:50:21 adorepop.com systemd[1]: Started OpenSSH server daemon.
OpenSSH 服务器已成功重新安装!
----------------------------------------------------------------------------
★[2025-04-02 18:50:21] Successful
----------------------------------------------------------------------------
开始重新安装 OpenSSH 服务器...
停止 sshd 服务...
卸载现有的 OpenSSH 服务器...
Loaded plugins: fastestmirror, post-transaction-actions
Repository epel is listed more than once in the configuration
No Match for argument: openssh-server
No Packages marked for removal
删除旧的配置文件和目录...
安装 OpenSSH 服务器...
Loaded plugins: fastestmirror, post-transaction-actions
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package openssh-server.x86_64 0:7.4p1-23.el7_9 will be installed
--> Processing Dependency: openssh = 7.4p1-23.el7_9 for package: openssh-server-7.4p1-23.el7_9.x86_64
--> Running transaction check
---> Package openssh.x86_64 0:7.4p1-23.el7_9 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package               Arch          Version               Repository      Size
================================================================================
Installing:
 openssh-server        x86_64        7.4p1-23.el7_9        updates        459 k
Installing for dependencies:
 openssh               x86_64        7.4p1-23.el7_9        updates        510 k

Transaction Summary
================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 970 k
Installed size: 2.8 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                              411 kB/s | 970 kB  00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
** Found 2 pre-existing rpmdb problem(s), 'yum check' output follows:
git-1.8.3.1-25.el7_9.x86_64 has missing requires of openssh-clients
systemtap-client-4.0-13.el7.x86_64 has missing requires of openssh-clients
  Installing : openssh-7.4p1-23.el7_9.x86_64                                1/2 
  Installing : openssh-server-7.4p1-23.el7_9.x86_64                         2/2 
  Verifying  : openssh-7.4p1-23.el7_9.x86_64                                1/2 
  Verifying  : openssh-server-7.4p1-23.el7_9.x86_64                         2/2 

Installed:
  openssh-server.x86_64 0:7.4p1-23.el7_9                                        

Dependency Installed:
  openssh.x86_64 0:7.4p1-23.el7_9                                               

Complete!
openssh-server-7.4p1-23.el7_9.x86_64
设置正确的权限...
启动并启用 sshd 服务...
检查 sshd 服务状态...
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2025-04-04 08:53:13 CST; 79ms ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 12293 (sshd)
   CGroup: /system.slice/sshd.service
           └─12293 /usr/sbin/sshd -D

Apr 04 08:53:13 adorepop.com systemd[1]: Starting OpenSSH server daemon...
Apr 04 08:53:13 adorepop.com sshd[12293]: Server listening on 0.0.0.0 port 22.
Apr 04 08:53:13 adorepop.com systemd[1]: Started OpenSSH server daemon.
Apr 04 08:53:13 adorepop.com sshd[12293]: Server listening on :: port 22.
OpenSSH 服务器已成功重新安装!
----------------------------------------------------------------------------
★[2025-04-04 08:53:13] Successful
---------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值