1. 修改系统时间:

    1. 自动同步:
      1. 修改时区:
        \cp -a /usr/share/zoneinfo/PRC /etc/localtime
      2. 安装ntpdate(端口:udp 123):
        yum -y install ntp
      3. 同步时间命令:
        /usr/sbin/ntpdate us.pool.ntp.org
      4. 加入计划任务(每10分钟同步一次):
        */10 * * * * /usr/sbin/ntpdate  us.pool.ntp.org(国外)
        */10 * * * * /usr/sbin/ntpdate  cn.pool.ntp.org(国内)
    2. 手动修改:
      1. 更改系统时间:
        date -s "2017-12-18 09:40:00"
      2. 写入系统时间:
        hwclock -w
  2. 修改主机名:

    1. 永久修改:
      vi /etc/hostname
    2. 临时修改:
      hostname lee
    3. 查看:
      hostname
  3. DNS配置:

    1. 静态IP-域名映射:
      vim /etc/hosts
      xx.xx.xx.xx www.test.com
    2. 配置域名解析服务器:
      vim /etc/resolv.conf
      nameserver 8.8.8.8
      nameserver 114.114.114.114
  4. 修改系统语言:

    1. 永久:
      vi /etc/profile,添加
      export  LANG="zh_CN.UTF-8"export  LANG="en_US.UTF-8"
    2. 临时:
      命令行下
      LANG="zh_CN.UTF-8"
  5. 别名设置:

    1. 添加:
      alias ll='ls -lah --color'
    2. 删除:
      unalias ll
  6. yum 常见操作:

    1. yum 源存储位置:
      /etc/yum.repos.d
    2. 下载epel-release:
      yum -y install epel-release
    3. 列出软件的所有可用版本:
      yum list [软件] --showduplicates
    4. 安装指定版本:
      yum -y install [软件]-[版本]
    5. 查看软件详细信息:
      yum info [软件]-[版本]
    6. 清除缓存:
      yum clean all
      yum makecache
    7. 跳过 gpg校验:
      yum [命令] --nogpgcheck
  7. vim配置:

    1. 配置:
      vim ~/.vimrc
      set nocompatible
      set number
      set ruler
      set wrap
      set showcmd
      set history=1000
      set nobackup
      set noswapfile
      "set cursorline
      "set cursorcolumn
      set showmatch
      set autoindent
      set cindent
      syntax enable
      syntax on
      set t_Co=256
      set ignorecase
      "set mouse=a
      set tabstop=4
      set shiftwidth=4
      set softtabstop=4
      set smarttab
      set expandtab
      set encoding=utf-8
      set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
      set ff=unix
      filetype on
      filetype indent on
      filetype plugin on
      filetype plugin indent on
    2. 修改默认编辑器:
      vim /etc/profile
      export EDITOR=/usr/bin/vim
  8. 安装网络工具:

    1. 网卡诊断:
      yum -y install net-tools
    2. 路由诊断:
      yum -y install bind-utils
  9. 安装开发者工具:
    yum -y groupinstall "development tools"

  10. 关闭selinux:

    1. 修改配置文件:
      vim /etc/selinux/config
      将
      SELINUX=enforcing
      改为
      SELINUX=disabled

      setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
    2. 临时生效:
      setenforce 0
  11. 移除firewalld、添加iptables:

    1. 移除firewalld:
      yum -y remove firewalld
    2. 添加iptables:
      yum -y install iptables iptables-services
    3. 启用iptables:
      systemctl enable iptables
  12. 修改ssh端口:

    1. 打开配置文件:
      vim /etc/ssh/sshd_config
    2. 找到port:
      Port 22
  13. 修改欢迎语:
    vim /etc/motd
    Happy codding!!!

  14. 软链接操作:

    1. 创建:
      # 将 /home/lee/go 目录链接到当前
      ln -s /home/lee/go ./go
    2. 删除:
      # 注意,后面没有 /
      rm -rf ./go
    3. 修改:
      # 将之前创建的 /home/lee/go 的指向替换为 /home/lee/golang
      ln -snf /home/lee/golang ./go
  15. 升级内核:

    1. 导入公共密钥:
      rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
    2. 安装 yum 源:
      yum install -y https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
    3. 替换为国内的 yum 源(可选):
      sed -i "s/mirrorlist=/#mirrorlist=/g" /etc/yum.repos.d/elrepo.repo
      sed -i "s#elrepo.org/linux#mirrors.tuna.tsinghua.edu.cn/elrepo#g" /etc/yum.repos.d/elrepo.repo
    4. 查看可用的内核版本:
      yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
    5. 升级为长期支持(lt)版本:
      yum -y --enablerepo=elrepo-kernel install kernel-lt
    6. 查看可用内核版本及启动顺序:
      awk -F\' '$1=="menuentry " {print i++ " : " $2}' /boot/grub2/grub.cfg
    7. 查看启动顺序:
      yum -y install grub2-pc
      grub2-editenv list
    8. 设置开机启动:
      grub2-set-default 0
    9. 重启:
      reboot
    10. 检查内核是否升级成功:
      uname -smr
  16. 设置密码:
    passwd

  17. 允许 ssh 通过密码登录:
    vim /etc/ssh/sshd_config

    PermitRootLogin yes
    PasswordAuthentication yes

    systemctl restart sshd

  18. linux 常用命令:

    1. 查看 centos 系统版本:
      cat /etc/redhat-release
    2. 查看 linux 发型版本:
      lsb_release -a
    3. 查看 linux 内核、版本、架构(32位/64位):
      uname -smr
      # amd/arm
      dpkg --print-architecture
    4. 查看 cpu 使用率 – htop
      # 安装
      yum -y install htop
      # 使用
      htop
    5. 查看 cpu 核心数:
      cat /proc/cpuinfo| grep "cpu cores"| uniq
    6. 查看 内存 使用率:
      # 以 MB 为单位显示内存
      free -m
    7. 查看 磁盘 使用率:
      df -h
    8. 统计 文件/文件夹 占用磁盘大小:
      du -sh 文件/文件夹
    9. 统计 文件总行数:
      wc -l 文件名
    10. 根据启动命令 显示进程号:
      ps -aux | grep nginx
    11. 杀死进程:
      # 根据命令 杀死进程
      pkill nginx
      # 根据进程号 杀死进程
      kill -9 进程号(pid)
    12. 根据 端口号 查找命令(需要安装 net-tools):
      netstat -nutlp | grep 80
      或
      lsof -i:80
    13. 下载文件:
      wget http://www.test.com/xx.jpg -O xx.jpg
    14. 压缩/解压文件:
      1. 压缩:
        # 仅压缩
        tar -czf xxx.tar.gz /path/to/dir
        # 压缩并移除原文件夹
        tar -czf xxx.tar.gz /path/to/dir --remove-files
      2. 解压:
        # 解压到当前目录
        tar -xzf xxx.tar.gz
        # 解压到指定目录
        tar -xzf xxx.tar.gz -C /path/to/dir
    15. 写入多行内容到文件:
      # ">":写入;">>":追加
      cat << EOF >> test.txt
      第一行
      第二行
      ...
      EOF
    16. 批量删除指定后缀的文件:
      find . -name "*.meta" | xargs rm -rf
    17. 找出系统中大于20兆的文件:
      find / -size +20M
    18. 查看服务运行日志:
      journalctl -f -u nginx
    19. 以其他用户的身份执行命令:
      su otherUser -l -c "/run/some/command"
    20. curl 代理设置:
      export http_proxy=http://x.x.x.x:1087;export https_proxy=http://x.x.x.x:1087;
    21. 释放内存:
      swapoff -a
      swapon -s
      swapon -a
      echo 3 > /proc/sys/vm/drop_caches
    22. 关闭防火墙:
      systemctl stop firewalld && systemctl disable firewalld
    23. 关闭 selinux:
      setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
文档更新时间: 2024-04-20 10:57   作者:lee