安装 Debian 系统后的几组常用命令

设置默认时区

将系统默认时区设置为中国上海

timedatectl set-timezone Asia/Shanghai
timedatectl

网卡设为ethX形式

将不规则的网卡名改为ethX形式,如eth0eth1

if ! grep -q "net.ifnames=0 biosdevname=0" /boot/grub/grub.cfg; then
    echo 'GRUB_TIMEOUT=0' >/etc/default/grub.d/15_timeout.cfg
    echo 'GRUB_CMDLINE_LINUX="$GRUB_CMDLINE_LINUX net.ifnames=0 biosdevname=0"' >/etc/default/grub.d/15_cmdline.cfg
    grub-mkconfig -o /boot/grub/grub.cfg
fi

根据地区设置仓库源

此脚本将根据IP地理位置自动修改,若修改失败请手动添加

if [ "$(wget -qO- http://ipip.rehi.org/country_code)" == "CN" ]; then
    if [ -f /etc/apt/sources.list ]; then
        sed -i 's/deb.debian.org/mirrors.tencent.com/g' /etc/apt/sources.list
        sed -i 's/security.debian.org/mirrors.tencent.com/g' /etc/apt/sources.list
    fi
    if [ -f /etc/apt/sources.list.d/debian.sources ]; then
        sed -i 's/deb.debian.org/mirrors.tencent.com/g' /etc/apt/sources.list.d/debian.sources
        sed -i 's/security.debian.org/mirrors.tencent.com/g' /etc/apt/sources.list.d/debian.sources
    fi
    if [ -d /etc/apt/mirrors ]; then
        for lf in `ls /etc/apt/mirrors/*.list`; do
            sed -i 's/deb.debian.org/mirrors.tencent.com/g' $lf
            sed -i 's/security.debian.org/mirrors.tencent.com/g' $lf
        done
    fi
    apt update
fi

重置ssh服务证书

若使用第三方镜像请务必执行此操作,防止密钥泄露

ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key
ssh-keygen -t dsa -P "" -f /etc/ssh/ssh_host_dsa_key
ssh-keygen -t ecdsa -P "" -f /etc/ssh/ssh_host_ecdsa_key
ssh-keygen -t ed25519 -P "" -f /etc/ssh/ssh_host_ed25519_key

允许root使用密码远程登录

允许root使用密码或rsa密钥登录

cat > /etc/ssh/sshd_config.d/my.conf <<EOF
Port 22
PermitRootLogin yes
PasswordAuthentication yes
PubkeyAcceptedKeyTypes=+ssh-rsa
EOF

扩容硬盘分区

云服务器硬盘可能为/dev/vda

if ! type growpart >/dev/null 2>&1; then
    apt install -y cloud-guest-utils
fi
if [ -e /dev/vda1 ]; then
    growpart /dev/vda 1
    resize2fs /dev/vda1
fi

设置虚拟内存

设置4G虚拟内存,尽量防止OOM错误

dd if=/dev/zero of=/mnt/swap bs=1M count=4096
chmod 0600 /mnt/swap
mkswap /mnt/swap
swapon /mnt/swap
if ! grep -q swap /etc/fstab; then
    echo "/mnt/swap swap swap defaults 0 0" >> /etc/fstab
fi
文章作者: 若海; 原文链接: https://www.rehiy.com/post/452/; 转载需声明来自若海观澜

添加新评论