1.写一个shell脚本
#!/bin/bash
# 更新系统
sudo apt update -y
# 安装openssh-server
sudo apt install openssh-server -y
# 启动SSH服务
sudo systemctl start ssh
# 设置SSH服务开机自启
sudo systemctl enable ssh
# 检查SSH服务状态
sudo systemctl status ssh
# 允许SSH连接
sudo ufw allow ssh
# 检查并修改SSH配置
if [ -f "/etc/ssh/sshd_config" ]; then
sudo sed -i 's/^PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config
sudo sed -i 's/^PubkeyAuthentication.*/PubkeyAuthentication yes/' /etc/ssh/sshd_config
sudo sed -i 's/^ChallengeResponseAuthentication.*/ChallengeResponseAuthentication no/' /etc/ssh/sshd_config
sudo sed -i 's/^PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config
fi
# 重启SSH服务
sudo systemctl restart ssh
2.将脚本传递至ubuntu系统上执行
直接运行脚本:
-
首先,确保脚本具有可执行权限。
bash
chmod +x script.sh
-
运行脚本。
bash
./script.sh