#!/bin/bash # 运行方式:bash <(curl -s https://static.imily.net/sh/ssh/install.sh) #判断是否存在.ssh,如果不存在则创建 if [ ! -d /root/.ssh ]; then mkdir /root/.ssh fi wget -O /root/.ssh/authorized_keys https://static.imily.net/sh/ssh/id_rsa.pub chmod 600 /root/.ssh/authorized_keys # Path: sshd_config.sh # 修改sshd_config配置文件 # PubkeyAuthentication yes # PermitRootLogin yes sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config # 上述可能修改失败,因为sshd_config文件中可能没有这两个配置项,所以需要手动添加 # PubkeyAuthentication yes # PermitRootLogin yes if [ -z "`grep 'PubkeyAuthentication yes' /etc/ssh/sshd_config`" ]; then echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config fi if [ -z "`grep 'PermitRootLogin yes' /etc/ssh/sshd_config`" ]; then echo "PermitRootLogin yes" >> /etc/ssh/sshd_config fi # 设置ssh超时时间,需要判断ssh_config文件是否存在 if [ -f /etc/ssh/ssh_config ]; then sed -i 's/#ServerAliveInterval 0/ServerAliveInterval 60/' /etc/ssh/ssh_config sed -i 's/#ServerAliveCountMax 3/ServerAliveCountMax 3/' /etc/ssh/ssh_config fi # 重启ssh服务 systemctl restart sshd clear echo "ssh配置完成" # 打印当前IP curl -s https://api.lyhuadu.com/dns echo ""