一、安装与配置基础环境
-
系统选择与安装
推荐新手从 Ubuntu 或 CentOS 开始,Ubuntu 使用sudo apt update && sudo apt install -y openssh-server安装基础环境,CentOS 使用sudo yum install -y openssh-server。安装完成后通过ssh root@localhost验证远程登录。 -
命令行工具配置
创建.bashrc配置文件(路径:~/.bashrc),添加以下内容优化体验:alias ll="ls -l --color=auto" alias psa="ps aux | grep -v grep | sort -nr"执行
source ~/.bashrc使配置生效,掌握Ctrl+R历史搜索、Ctrl+A/Ctrl+E调整光标位置等快捷键。
二、核心操作技能树
-
文件系统导航与操作
- 创建目录:
mkdir -p /var/www/html/{logs,backup} - 查看文件权限:
ls -l /etc/passwd - 批量重命名(正则匹配):
find /var -name "*.log" -type f -exec rename 's/^/2023-' {} +
- 创建目录:
-
进程与资源管理
- 查看进程树:
ps -ef --forest - 终止进程(谨慎操作):
sudo kill -9 123456 - 监控内存使用:
free -h(每日执行) - 优化磁盘空间:
sudo apt clean(Ubuntu)或sudo yum clean all(CentOS)
- 查看进程树:
-
网络配置与诊断
# 查看当前IP ip addr show eth0 # 配置静态IP(Ubuntu示例) sudo nano /etc/network/interfaces 添加: auto eth0 iface eth0 inet static address 192.168.1.100 netmask 255.255.255.0 gateway 192.168.1.1
三、服务部署实战
-
Web服务器搭建(Nginx示例)
# 安装 sudo apt install nginx -y # 查看默认配置文件 sudo nano /etc/nginx/sites-available/default # 修改服务器块(server block): server { listen 80; server_name example.com; root /var/www/html; index index.html index.htm; location / { try_files $uri $uri/ /index.html; } } # 启动并测试 sudo systemctl start nginx curl http://localhost -
MySQL数据库集群
# 安装MySQL sudo apt install mysql-server -y # 创建数据库用户(示例) sudo mysql -u root CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'SecureP@ssw0rd'; GRANT ALL PRIVILEGES ON *.* TO 'appuser'@'localhost'; FLUSH PRIVILEGES; exit # 查看服务状态 sudo systemctl status mysql -
Docker容器化部署
# 安装Docker sudo apt install docker.io -y # 创建镜像(以Nginx为例) docker build -t custom-nginx . # 运行容器并绑定宿主机端口 docker run -d -p 8080:80 custom-nginx
四、权限与安全体系
-
用户权限管理
# 创建系统用户 sudo useradd -s /bin/bash -M appuser # 设置密码(需先开启密码服务) sudo systemctl enable password服务 sudo passwd appuser # 配置sudo权限 echo "appuser ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers -
文件系统权限控制
# 修改目录权限(755) sudo chmod -R 755 /var/www/html # 限制文件访问(示例:仅root可写) sudo chmod 644 /etc/passwd sudo chmod 440 /etc/passwd -
安全审计配置
# 启用审计日志 sudo systemctl enable auditd # 查看审计事件(每2小时扫描) 0 0 * * * root /usr/bin/auditchk --scan --output-format=tsv > /var/log/audit/auditchk.log 2>&1
五、生产环境部署要点
-
日志系统搭建
# 安装logrotate sudo apt install logrotate -y # 配置日志轮转(/var/log/*.log) sudo nano /etc/logrotate.d/your_log 添加: /var/log/*.log { daily rotate 7 compress delaycompress } -
服务高可用方案
# 部署Keepalived(以Nginx为例) sudo apt install keepalived -y # 编辑配置文件(/etc/keepalived/keepalived.conf) vrrp { state Master interface eth0 virtual IP address 192.168.1.100 priority 100 } # 启动服务 sudo systemctl enable keepalived -
资源监控自动化
# 安装Prometheus监控 sudo apt install prometheus prometheus-node-exporter -y # 配置Node Exporter sudo systemctl enable node-exporter # 创建Prometheus规则文件(/etc/prometheus rules.d/your规则.yml) - alert: DiskSpaceLow expr: node_filesystem_size_bytes < 1024000000 for: 5m labels: severity: warning annotations: summary: "磁盘空间不足({{ $value }}字节)" description: "系统已使用超过90%的磁盘空间"
六、常见问题解决方案
-
权限不足错误处理
# 检查文件权限 ls -l /path/to/file # 查看用户所属组 groups appuser # 添加用户到sudo组 sudo usermod -aG sudo appuser -
服务异常排查流程
# 查看服务日志 journalctl -u nginx --since "1h ago" # 检查端口占用 sudo netstat -tuln | grep 80 # 重启服务(谨慎操作) sudo systemctl restart nginx -
网络问题诊断技巧
# 测试连通性 ping 8.8.8.8 # 查看防火墙规则 sudo ufw status # 检查路由表 ip route show
七、最佳实践建议
-
日常维护清单
# 每日执行 0 0 * * * root apt autoremove -y && apt update && apt upgrade -y # 每月执行 0 0 1 * * root apt clean && df -h / > /var/log/metric/mountpoints.csv -
自动化部署方案
# 使用Ansible编写playbook(/etc/ansible/roles/webserver) - name: Install Nginx become: yes apt: name: nginx state: latest # 执行:sudo ansible-playbook -i inventory.yml webserver.yml -
灾难恢复准备
# 生成系统快照 sudo apt install timeshift -y sudo timeshift start # 创建远程备份(使用rsync) rsync -avz /var/www/html/ user@remote:/backup --delete
八、持续学习路径
-
推荐学习资源
- 书籍:《Linux命令行与Shell脚本编程大全》
- 在线课程:Linux Foundation的CKA认证培训
- 实践平台:TryHackMe Linux路径、Hacker101
-
技能进阶路线
- 基础:掌握30个高频命令(cd/pwd/mv/cp等)
- 中级:学习Shell脚本(使用条件判断和循环)
- 高级:掌握系统调优(vmstat/iotop/strace)
-
认证考试建议
- 初级:CompTIA Linux+(重点掌握L1-L2)
- 中级:Red Hat Certified Engineer(RHCE)
- 高级:Linux Foundation Certified Engineer(LFCE)
总结:本文系统梳理了Linux命令行操作的核心要点,包含从环境搭建到生产部署的全流程实践。建议新手每日完成30分钟命令行练习,重点关注权限管理(平均耗时40%)、服务监控(30%)和日志分析(20%)。遇到问题时优先查阅官方文档(man pages),养成记录操作日志的习惯,逐步建立完整的系统运维思维。
文章版权声明:除非注明,否则均为xmsdn原创文章,转载或复制请以超链接形式并注明出处。

