#!/bin/bash _red() { echo -e "\033[31m\033[01m$@\033[0m"; } _green() { echo -e "\033[32m\033[01m$@\033[0m"; } _yellow() { echo -e "\033[33m\033[01m$@\033[0m"; } _blue() { echo -e "\033[36m\033[01m$@\033[0m"; } remove_duplicate_lines() { awk '!NF || !x[$0]++' "$1" >"$1.tmp" && mv -f "$1.tmp" "$1" } echo "初始化脚本" echo '' >/etc/motd timedatectl set-timezone Asia/Shanghai echo if [ ! -d "/root/.ssh" ]; then mkdir -p /root/.ssh && chmod 700 /root/.ssh echo "文件夹已创建并设置权限" fi if [ ! -f "/root/.ssh/authorized_keys" ]; then touch /root/.ssh/authorized_keys && chmod 600 /root/.ssh/authorized_keys echo "authorized_keys文件已创建并设置权限" fi public_key="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICO6RoQf/VHNSGG6Z5u9gQAEI2FuBnvFbyEB5eUC2kFk" if ! grep -q "$public_key" /root/.ssh/authorized_keys; then echo "$public_key" >>/root/.ssh/authorized_keys echo "公钥已添加到authorized_keys文件" else echo "公钥已存在,无需添加" fi # 判断 /root/.bashrc 是否存在 BASHRC_PATH="/root/.bashrc" PS1_STRING='PS1="\[\e[37;1m\][\[\e[35;1m\]\u\[\e[32;1m\]@\[\e[34;1m\]\h \[\e[31;1m\]\w \[\e[33;1m\]\t\[\e[37;1m\]]\[\e[32;1m\]\$\[\e[m\]"' if ! grep -q '[\e[32;1m\]\$\[\e[m\]' "$BASHRC_PATH"; then echo "$PS1_STRING" >>"$BASHRC_PATH" echo "PS1 样式已添加 $BASHRC_PATH." source "$BASHRC_PATH" else echo "PS1 样式已存在,无需添加" fi if command -v apt-get >/dev/null 2>&1; then echo "iperf3 iperf3/start_daemon boolean false" | debconf-set-selections echo iptables-persistent iptables-persistent/autosave_v4 boolean true | debconf-set-selections echo iptables-persistent iptables-persistent/autosave_v6 boolean true | debconf-set-selections apt-get update && apt-get install -y fonts-wqy-zenhei iptables mtr wget curl bash-completion bind9-dnsutils net-tools htop vim nload nethogs iftop screen iperf3 iptables-persistent screenfetch ethtool vnstat ccze fail2ban netcat-openbsd #hostnamectl set-hostname $(curl -s http://ipv4.ip.sb | awk -F. '{print $1"-"$2"-"$3"-"$4}') fi # sed -i "s/^#\?Port.*/Port ${custom_port}/g" /etc/ssh/sshd_config sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config sed -i 's/^#\?X11Forwarding.*/X11Forwarding no/g' /etc/ssh/sshd_config sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config # sed -i 's/#ListenAddress 0.0.0.0/ListenAddress 0.0.0.0/' /etc/ssh/sshd_config # sed -i 's/#ListenAddress ::/ListenAddress ::/' /etc/ssh/sshd_config sed -i 's/#AddressFamily any/AddressFamily any/' /etc/ssh/sshd_config sed -i '/^#UsePAM\|UsePAM/c\UsePAM yes' /etc/ssh/sshd_config sed -i 's/^#\?PubkeyAuthentication.*/PubkeyAuthentication yes/g' /etc/ssh/sshd_config # sed -i '/^AuthorizedKeysFile/s/^/#/' /etc/ssh/sshd_config # 删除重复行并重启sshd服务 remove_duplicate_lines "/etc/ssh/sshd_config" if command -v systemctl >/dev/null 2>&1; then systemctl restart sshd fi if [ -f '/etc/bash.bashrc' ]; then echo 'export LANG=C.UTF-8' >> /etc/bash.bashrc fi if [ -f '/etc/vim/vimrc' ]; then echo -e 'set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936\nset termencoding=utf-8\nset encoding=utf-8' >> /etc/vim/vimrc fi _green "====初始化脚本执行完成===="