从零开始的服务器运维生活

从零开始的服务器运维生活

前言

闲来无事,刚好手里有个闲置的 ECS 不知道用来做什么。思来想去,决定用它来搭建一个 Web 服务器,顺带扩展个博客模块,用来记录一些日常心得,以及服务器运维中产生的问题及解决思路。

  • 服务器使用的系统为: Debian 12

安全防护

因为初始默认使用的是 root 用户,root 用户权限极大,一旦 root 密码泄露,将导致服务器被入侵。所以建议登录成功后及时 添加用户修改 SSH 端口关闭 root 登录、或使用 SSH Key 登录,以增强服务器的安全性。

  • 添加用户

    # 创建用户名为 username 的用户
    adduser username
    
    # 添加用户 username 到 sudo 组
    usermod -aG sudo username
    
    # 切换到用户 username
    su username
    
  • 修改 ssh 登录

    # 编辑配置
    sudo vim /etc/ssh/sshd_config
    
    # 修改配置
    Port 22
    PermitRootLogin no
    PasswordAuthentication no
    PubkeyAuthentication yes
    AllowUsers username
    
    # 保存退出
    :wq
    
    # 创建 .ssh 目录
    mkdir ~/.ssh
    
    # 将 public_key 添加到 authorized_keys 中。 public_key: 指定 ssh key 的公钥。
    echo "<public_key>" >> ~/.ssh/authorized_keys
    
    # 修改权限
    chmod 600 ~/.ssh/authorized_keys
    
    # 重启 ssh 服务
    sudo service sshd restart
    
  • 测试 ssh 登录

    # ssh 登录
    ssh -p 22 -i ~/.ssh/id_rsa username@ip
    
    # 说明:
    # -p : 指定 ssh 端口。默认是 22 端口,可以省略此参数。
    # -i : 指定 ssh key 的路径。
    # username@ip: 指定用户名和服务器的 ip。
    

基本优化

  • 替换源

    因为默认使用的是 Debian 源,在国内访问速度会很慢,所以可以用 阿里云腾讯云华为云 等源来替换,这样速度会更快。

    # 备份源
    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
    
    # 编辑源
    sudo vim /etc/apt/sources.list
    
    # 替换源
    deb http://mirrors.aliyun.com/debian/ bookworm main non-free non-free-firmware contrib
    deb-src http://mirrors.aliyun.com/debian/ bookworm main non-free non-free-firmware contrib
    deb http://mirrors.aliyun.com/debian/ bookworm-updates main non-free non-free-firmware contrib
    deb-src http://mirrors.aliyun.com/debian/ bookworm-updates main non-free non-free-firmware contrib
    deb http://mirrors.aliyun.com/debian/ bookworm-backports main non-free non-free-firmware contrib
    deb-src http://mirrors.aliyun.com/debian/ bookworm-backports main non-free non-free-firmware contrib
    deb http://mirrors.aliyun.com/debian-security/ bookworm-security main non-free contrib
    deb-src http://mirrors.aliyun.com/debian-security/ bookworm-security main non-free contrib
    
    # 保存退出
    :wq
    
    # 应用源
    sudo apt-get update
    sudo apt-get upgrade -y
    
  • 安装常用软件

    # 更新源
    sudo apt-get update
    
    # 安装常用软件
    sudo apt-get install -y vim git curl wget htop net-tools
    
    # 说明
    # 1. vim: 编辑器。
    # 2. git: 版本控制。
    # 3. curl: 网络工具。
    # 4. wget: 网络工具。
    # 5. htop: 进程管理。
    # 6. net-tools: 网络工具。
    
  • 设置时区

    # 设置时区为 Asia/Shanghai
    sudo timedatectl set-timezone Asia/Shanghai
    
  • 设置 hostname

    # 设置 hostname 为 server
    sudo hostnamectl set-hostname server
    
  • 开启 swap

    swap 是 linux 中的一个虚拟内存,如果内存不足,建议在服务器上开启 swap,以解决内存不足的问题。

    1. 查看 swap 是否启用:

      # 查看 swap 状态,-m: 以 MB 为单位显示。
      free -m
      
      # 如果 swap 显示 0, 则需要开启 swap。
      
    2. 开启 swap:

      # 创建一个指定大小的文件。建议swap大小为内存的 2 倍。
      sudo fallocate -l 4G /swapfile
      
      # 修改文件权限
      sudo chmod 600 /swapfile
      
      # 格式化 swap 文件
      sudo mkswap /swapfile
      
      # 启用 swap
      sudo swapon /swapfile
      
      # 将 swap 文件添加到 fstab 文件中,使得 swap 文件在重启后自动启用。
      echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
      
      # 查看 swap 状态
      free -m
      

软件扩展

  • 使用 zsh 美化终端

    1. 安装 oh-my-zsh

      # 安装 oh-my-zsh
      sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
      
      # 设置 zsh 为默认终端
      chsh -s $(which zsh)
      
    2. 安装插件

      # 下载自动提示插件
      git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
      
      # 下载语法高亮插件
      git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
      
      # 编辑配置文件
      vim ~/.zshrc
      
      # 添加插件
      plugins=(
      zsh-autosuggestions
      zsh-syntax-highlighting
      git
      )
      
      # 保存退出
      :wq
      
      # 重新加载配置文件
      source ~/.zshrc
      
    3. 安装主题

      # 下载主题
      git clone https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
      
      # 编辑配置文件
      vim ~/.zshrc
      
      # 修改主题
      ZSH_THEME="powerlevel10k/powerlevel10k"
      
      # 保存退出
      :wq
      
      # 重新加载配置文件
      source ~/.zshrc
      
      # 配置主题
      p10k configure
      
LICENSED UNDER CC BY-NC-SA 4.0