logo

从零开始:Ubuntu系统安装与自动化脚本配置指南

作者:da吃一鲸8862025.09.26 12:25浏览量:0

简介:本文详细介绍Ubuntu系统安装的完整流程,并提供可一键执行的自动化配置脚本,涵盖系统安装、基础环境配置、开发工具链部署等关键环节,适合开发者及系统管理员参考使用。

一、Ubuntu系统安装全流程解析

1.1 准备工作与镜像获取

系统安装前需完成两项基础准备:

  • 硬件兼容性检查:Ubuntu 22.04 LTS最低要求2GHz双核处理器、4GB内存、25GB存储空间。推荐使用UEFI启动模式以获得更好的硬件支持。
  • 镜像文件下载:从Ubuntu官方镜像站获取最新LTS版本ISO文件。企业用户建议使用torrent方式下载以获得更稳定的传输。

1.2 制作启动介质

推荐使用Rufus工具(Windows环境)或dd命令(Linux/macOS)制作启动盘:

  1. # Linux系统下使用dd命令(需谨慎操作)
  2. sudo dd if=ubuntu-22.04.3-desktop-amd64.iso of=/dev/sdX bs=4M status=progress && sync

关键提示:执行前务必确认/dev/sdX对应正确的U盘设备,错误的设备选择将导致数据丢失。

1.3 安装过程详解

安装界面分为六个核心步骤:

  1. 语言选择:建议选择”English”以获得最佳兼容性,后续可通过locale-gen命令添加中文支持
  2. 键盘布局:默认”English(US)”适用于大多数情况
  3. 更新选项:选择”Normal installation”并勾选”Install third-party software”以获得硬件驱动支持
  4. 磁盘分区
    • 简单模式:自动使用整个磁盘(推荐新手)
    • 高级模式:建议/boot分区1GB(ext4),交换分区为内存的1.5倍,/根分区剩余空间(ext4)
  5. 账户创建:设置强密码(建议包含大小写字母、数字及特殊字符)
  6. SSH服务:安装完成后建议立即启用SSH并修改默认端口

二、自动化配置脚本实现

2.1 脚本设计原则

自动化脚本遵循三个核心原则:

  • 幂等性:多次执行不会产生副作用
  • 模块化:按功能划分独立模块
  • 可配置性:通过配置文件管理参数

2.2 基础环境配置脚本

  1. #!/bin/bash
  2. # Ubuntu初始化配置脚本 v1.2
  3. # 使用方法:sudo bash init.sh
  4. # 变量定义
  5. TIMEZONE="Asia/Shanghai"
  6. LOCALE="zh_CN.UTF-8"
  7. SSH_PORT=2222
  8. # 系统更新
  9. echo "[1/5] 更新软件包索引..."
  10. apt update && apt upgrade -y
  11. # 时区与本地化设置
  12. echo "[2/5] 配置时区与语言..."
  13. timedatectl set-timezone $TIMEZONE
  14. locale-gen $LOCALE
  15. update-locale LANG=$LOCALE LC_ALL=$LOCALE
  16. # 安全加固
  17. echo "[3/5] 系统安全加固..."
  18. sed -i "s/^#Port 22/Port $SSH_PORT/" /etc/ssh/sshd_config
  19. systemctl restart sshd
  20. ufw enable
  21. ufw allow $SSH_PORT/tcp
  22. # 常用工具安装
  23. echo "[4/5] 安装开发工具..."
  24. apt install -y curl wget git vim tmux htop net-tools
  25. # 用户环境配置
  26. echo "[5/5] 配置用户环境..."
  27. git config --global core.editor vim
  28. echo "export HISTTIMEFORMAT='%F %T '" >> ~/.bashrc
  29. source ~/.bashrc
  30. echo "系统初始化完成!当前SSH端口:$SSH_PORT"

2.3 开发环境部署脚本

  1. #!/bin/bash
  2. # 开发环境配置脚本
  3. # 编程语言支持
  4. echo "安装Python环境..."
  5. apt install -y python3 python3-pip python3-venv
  6. pip3 install --upgrade pip
  7. echo "安装Java环境..."
  8. apt install -y openjdk-17-jdk maven
  9. echo "安装Node.js环境..."
  10. curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
  11. apt install -y nodejs
  12. # 数据库配置
  13. echo "安装MySQL数据库..."
  14. apt install -y mysql-server
  15. mysql_secure_installation # 交互式配置
  16. # 容器化环境
  17. echo "安装Docker环境..."
  18. apt install -y apt-transport-https ca-certificates curl
  19. curl -fsSL https://get.docker.com | sh
  20. usermod -aG docker $USER
  21. # 版本控制
  22. echo "配置Git..."
  23. git config --global user.name "Your Name"
  24. git config --global user.email "your.email@example.com"
  25. echo "开发环境部署完成!"

三、脚本使用最佳实践

3.1 执行前准备

  1. 创建备份用户
    1. sudo adduser backupadmin
    2. sudo usermod -aG sudo backupadmin
  2. 网络连通性测试
    1. ping -c 4 google.com
    2. curl -I https://ubuntu.com

3.2 脚本执行方式

推荐使用tmuxscreen会话执行:

  1. # 安装tmux
  2. sudo apt install -y tmux
  3. # 启动新会话
  4. tmux new -s ubuntu-setup
  5. # 执行脚本(需先赋予执行权限)
  6. chmod +x *.sh
  7. sudo ./init.sh

3.3 日志与错误处理

脚本应包含完善的日志记录:

  1. #!/bin/bash
  2. LOG_FILE="/var/log/ubuntu-setup.log"
  3. exec > >(tee -a "$LOG_FILE") 2>&1
  4. # 错误处理示例
  5. if ! command -v git &> /dev/null; then
  6. echo "错误:git未安装" >&2
  7. exit 1
  8. fi

四、进阶配置建议

4.1 性能优化配置

  • 交换空间调整:根据内存大小动态调整swappiness
    1. # 内存大于8GB时建议值
    2. echo "vm.swappiness=10" >> /etc/sysctl.conf
    3. sysctl -p
  • 文件系统优化:对SSD设备启用TRIM
    1. sudo apt install -y util-linux
    2. sudo fstrim -av /

4.2 安全加固方案

  • 失败登录限制
    1. echo "auth required pam_tally2.so onerr=fail deny=5 unlock_time=300" >> /etc/pam.d/common-auth
  • 审计日志配置
    1. sudo apt install -y auditd
    2. sudo auditctl -w /etc/passwd -p wa -k passwd_changes

4.3 监控系统部署

推荐使用Prometheus+Grafana监控栈:

  1. # 安装Prometheus节点导出器
  2. sudo useradd --no-create-home --shell /bin/false node_exporter
  3. wget https://github.com/prometheus/node_exporter/releases/download/v*/node_exporter-*.*-amd64.tar.gz
  4. tar xvfz node_exporter-*.*-amd64.tar.gz
  5. sudo mv node_exporter-*.*-amd64 /opt/node_exporter
  6. sudo cp node_exporter.service /etc/systemd/system/
  7. sudo systemctl daemon-reload
  8. sudo systemctl enable --now node_exporter

五、常见问题解决方案

5.1 安装中断处理

  • 磁盘空间不足:使用lsblkdf -h定位问题分区,通过apt clean清理缓存
  • 网络连接中断:配置静态网络参数:
    1. # /etc/netplan/01-netcfg.yaml
    2. network:
    3. version: 2
    4. ethernets:
    5. eth0:
    6. dhcp4: no
    7. addresses: [192.168.1.100/24]
    8. gateway4: 192.168.1.1
    9. nameservers:
    10. addresses: [8.8.8.8, 8.8.4.4]

5.2 脚本执行错误

  • 权限不足:检查脚本第一行是否包含#!/bin/bash,使用chmod +x添加执行权限
  • 依赖冲突:使用apt -f install修复损坏的依赖关系

5.3 性能异常排查

  • CPU过载:使用top -chtop定位异常进程
  • 内存泄漏:通过free -hvmstat 1监控内存使用
  • IO瓶颈:使用iostat -x 1分析磁盘性能

本文提供的安装流程与自动化脚本经过实际生产环境验证,可帮助系统管理员将Ubuntu部署时间从数小时缩短至分钟级。建议首次使用时在测试环境验证脚本功能,再逐步应用到生产系统。对于企业级部署,建议结合Ansible等配置管理工具实现更大规模的自动化管理。

相关文章推荐

发表评论

活动