public:it:linux:maintain

这是本文档旧的修订版!


maintain

  • 日志:utmp, wtmp, btmp
    • utmp maintains a full accounting of the current status of the system, system boot time (used by uptime), recording user logins at which terminals, logouts, system events etc.
    • wtmp acts as a historical utmp
    • btmp records failed login attempts
  • 登录记录last,lastb:https://linux.die.net/man/1/last
  • man bash
  • bash shell 脚本教程:Advanced Bash-Scripting Guide包含很多示例!:-O
  • fish shell a fully-equipped command line shell (like bash or zsh) that is smart and user-friendly
  • 输入命令, 按下 Alt-# ,命令被注释, 可以不被执行但保存在命令历史中.
  • 使用重定向到标准输出和标准错误 some-command >logfile 2>&1
  • 在bash输入一个Tab字符, 可以先按Ctrl-V,再按Tab键,就可以。当然,也可以使用$’\t’
  • Expect 自动输密码Expect
  • 查找满足两个关键字A与B(有先后)的上下十行 grep -E 'A.*B' -C 10
  • 查找A或B grep -E 'A|B'
  • 统计tcp连接数
    netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
  • 查看端口监听 ss -ln
  • 查看监听进程 netstat -lntp
  • 防火墙 iptables
  • 临时修改DNS, 实时生效:
     sudo vim /etc/resolv.conf
    # 然后按以下方式添加或修改
    nameserver 8.8.8.8
  • 永久修改DNS:
     sudo vim /etc/resolvconf/resolv.conf.d/base
    # 然后按以下方式添加或修改
    nameserver 8.8.8.8
  • 重启网络:
    sudo service networking restart 
  • 路由操作命令: route, 具体 man 查看. 显示路由:route -n
  • arp 查看 arp -an
  • 查看对方是否开了端口 nc -v <ip> <port>
    • 批量查询 nc -zv 192.168.0.189 2000-4000
  • DNS linux下DNS解析(nslookup、dig、host) dig指令挺好用的
  •  # ┌───────────── min (0 - 59)
     # │ ┌────────────── hour (0 - 23)
     # │ │ ┌─────────────── day of month (1 - 31)
     # │ │ │ ┌──────────────── month (1 - 12)
     # │ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
     # │ │ │ │ │
     # │ │ │ │ │
     # * * * * *  user command to execute
  • */2,表示每2单位执行,比如 0 */2 * * *表示每两小时执行
  • 0 10-22/2 * * *表示在每天10-22时之间每2小时执行
  • 0 2,8-10,12 * * * 表示每天2点,8点,9点,10点,12点0分执行
  • 日志压缩/删除
  • 时间同步服务 RFC1305,RFC5905,RFC7822
  • stratum 层级最多到16(两比特), 所以客户端不会同步层级为16的时间。
  • 引用自鸟哥私房菜: ntpq -p可以列出目前我們的 NTP 與相關的上層 NTP 的狀態,
    [root@www ~]# ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
    *tock.stdtime.go 59.124.196.87    2 u   19  128  377   12.092   -0.953   0.942
    +59-124-196-83.H 59.124.196.86    2 u    8  128  377   14.154    7.616   1.533
    +59-124-196-84.H 59.124.196.86    2 u    2  128  377   14.524    4.354   1.079

    上頭的幾個欄位的意義為:

    • remote:亦即是 NTP 主機的 IP 或主機名稱囉~注意最左邊的符號
      • 如果有『 * 』代表目前正在作用當中的上層 NTP
      • 如果是『 + 』代表也有連上線,而且可作為下一個提供時間更新的候選者。
    • refid:參考的上一層 NTP 主機的位址
    • st:就是 stratum 階層囉!
    • when:幾秒鐘前曾經做過時間同步化更新的動作;
    • poll:下一次更新在幾秒鐘之後;
    • reach:已經向上層 NTP 伺服器要求更新的次數
    • delay:網路傳輸過程當中延遲的時間,單位為 10^(-3) 秒
    • offset:時間補償的結果,單位與 10^(-3) 秒
    • jitter:Linux 系統時間與 BIOS 硬體時間的差異時間, 單位為 10^(-3) 秒。
  • 可使用 lscpu 命令来查看;
    CPU(s)                 32  // 逻辑cpu个数,thread * core * socket
    Thread(s) per core:    2   // cpu 每核线程数
    Core(s) per socket:    8   // cpu 核数
    Socket(s):             2   // cpu 卡槽个数,即cpu硬件个数
  • cpu个数
    #查看物理CPU的个数
    cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l
    #查看逻辑CPU的个数
    cat /proc/cpuinfo | grep "processor" | wc -l
    #查看CPU是几核
    cat /proc/cpuinfo | grep "cores" | uniq
    #查看CPU的主频
    cat /proc/cpuinfo | grep MHz | uniq 
  • 负载警惕值: 负载值/逻辑CPU个数 > 0.7
  • 时区选择 (详见 timezone setting in linux:
    • tzselect 只是方便你查看时区选项,并不会设置。
    • 一般可手动修改 /etc/localtime, /etc/timezone。修改 /etc/localtime 的方法可直接 link 到指定时区文件,时区文件可在 /usr/share/zoneinfo/目录下查找。
      $ sudo unlink /etc/localtime 
      $ sudo ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    • ubuntu 可直接使用命令
      sudo dpkg-reconfigure tzdata 
  • unix timestamp转北京时间:date -d@1372654714 '+%Y-%m-%d %H:%M:%S'
  • 北京时间转unix timestamp:date -d '2013-07-01 12:58:34' '+%s'
  • 查看文件夹占用空间:du -s -h folder
  • 列出层数1的子文件夹及指定文件夹的空间占用:du -d 1 -h folder
  • 排序: sort 具体看man sort
  • 使用 man ascii 来查看 ASCII
  • 查看二进制文件,使用hd命令
  • 查看磁盘分配:df -ahT
  • 定时一次性任务: at命令, 具体看man at
  • 文件同步或备份, tar -zcf每次打的包的MD5都不同, 因为记录了当前压缩时间;不压缩直接tar -cf打包则OK, 但是文件内容一样但 mtime 改变仍然会改变打包后文件 md5.所以比较差异时最好是直接比较文件二进制差异;或者保证打包时不使用拷贝等操作影响文件 mtime.
  • export LC_ALL=C.UTF-8
  • public/it/linux/maintain.1612937561.txt.gz
  • 最后更改: 2021/02/10 14:12
  • oakfire