Skip to content

2. 常用 Linux 命令与技巧

本章节介绍在 RHEL 8.10(主机名 ZSLinux)上使用 SecureCRT 执行的常用 Linux 命令,配合 SecureFX 传输文件。内容涵盖 Shell 基础、文件操作、软件管理和服务监控,简洁实用,适合初学者快速掌握和运维人员日常操作。所有命令在 ZSLinux 环境中测试,确保实验一致性。

2.1 前提条件

  • RHEL 8.10 已安装(参考第 1 章),主机名设置为 ZSLinux
  • 使用 SecureCRT 登录(SSH2 协议,端口 22,虚拟机 IP)。
  • 使用 SecureFX 传输文件(SFTP 协议)。

2.2 Shell 基础

  • 查看 Shell 类型:
    bash
    echo $SHELL  # 显示当前 Shell 路径
    # 应显示 /bin/bash
  • 创建并运行脚本:
    bash
    echo '#!/bin/bash' > test.sh  # 创建脚本文件
    echo 'echo "Hello from ZSLinux"' >> test.sh  # 添加输出命令
    chmod +x test.sh  # 赋予执行权限
    ./test.sh  # 运行脚本
  • 查看命令帮助:
    bash
    man ls  # 查看 ls 命令手册
    ls --help  # 显示 ls 帮助信息

2.3 常用命令

2.3.1 文件与目录管理

  • 列出文件:
    bash
    ls -l  # 长格式显示文件
    ls -a  # 显示隐藏文件
  • 创建和删除:
    bash
    mkdir /data  # 创建目录
    touch file.txt  # 创建空文件
    rm -f file.txt  # 删除文件
  • 复制和移动:
    bash
    cp file.txt /data/  # 复制文件
    mv /data/file.txt /data/file2.txt  # 移动/重命名文件
  • 查看文件内容:
    bash
    cat /etc/hostname  # 显示文件内容
    tail -f /var/log/messages  # 实时查看日志

2.3.2 软件管理

  • 安装和更新软件:
    bash
    dnf install -y htop  # 安装 htop
    dnf update -y htop  # 更新 htop 软件包

2.3.3 服务管理

  • 管理服务:
    bash
    dnf install -y httpd  # 安装 httpd 服务
    systemctl enable --now httpd  # 启用并启动 httpd
    systemctl status httpd  # 查看服务状态
  • 查看日志:
    bash
    journalctl -u httpd  # 查看 httpd 日志
    journalctl -f  # 实时查看系统日志

2.4 系统监控

  • 进程监控:
    bash
    top  # 实时查看进程
    ps aux | grep httpd  # 查找 httpd 进程
  • 磁盘使用:
    bash
    df -h  # 显示磁盘使用情况
  • 内存使用:
    bash
    free -h  # 显示内存使用情况
  • 网络状态:
    bash
    ss -tuln  # 显示网络端口

2.5 验证

  • 检查主机名:
    bash
    hostnamectl  # 显示主机名
    # 应显示 hostname: ZSLinux

2.6 实践任务

  1. 使用 SecureCRT 登录 ZSLinux,运行 echo $SHELL 确认 Shell。
  2. 创建并运行脚本 test.sh,输出 Hello from ZSLinux
  3. 安装 htop 并查看进程。

2.7 自测问题

  • 问题:如何确认当前主机名?
    • 答案hostnamectl 显示 hostname: ZSLinux。
  • 问题:如何安装并启动 httpd 服务?
    • 答案dnf install -y httpdsystemctl enable --now httpd

Released under the MIT License.