【Linux】之常用命令

£神魔★判官ぃ 2023-02-22 05:13 123阅读 0赞

目录

  • 快捷键
  • 文件操作
  • Netstat 命令
  • Ps 命令
  • 时区设置
  • 软件安装和卸载

Bash 常用快捷键

  • Ctrl + L :清空终端页面
  • Ctrl + u :清空命令

文件操作


1、清空文件内容

  1. # 通过 shell 重定向 null
  2. [root@wang log] > access.log
  3. # 使用 : 符号
  4. [root@wang log] : > access.log
  5. [root@wang log] true > access.log
  6. # 使用 cat/cp/dd 实用工具及 /dev/null 设备
  7. [root@wang log] cat /dev/null > access.log
  8. [root@wang log] cp/dev/null > access.log
  9. [root@wang log] dd /dev/null > access.log
  10. # 使用 echo 命令
  11. [root@wang log] "" > access.log
  12. [root@wang log] echo > access.log

Netstat 命令


Netstat 是一个查看网络状态的命令,是一个监控 TCP/IP 网络的非常有用的工具,它可以显示路由表、实际的网络连接以及每一个网络接口设备的状态信息。Netstat 用于显示与 IP、TCP、UDPICMP 协议相关的统计数据,一般用于检验本机各端口的网络连接情况。

使用 netstat -V 命令检查是否已安装该命令,如果没有安装的话使用以下命令进行安装:

  1. $ yum install net-tools
  2. $ netstat -V
  3. net-tools 2.10-alpha

netstat 的命令分为两大块:

  • Active Internet connections:有源TCP连接,其中 Recv-QSend-Q 指接收队列和发送队列。这些字段的值一般都应该是0,如果不是则表示软件包正在队列中堆积,这种情况只能在非常少的情况见到。
  • Active UNIX domain sockets:有源 Unix 域套接口(和网络套接字一样,但是只能用于本机通信,性能可以提高一倍)。字段名解释如下:

    • Proto:显示连接使用的协议;
    • RefCnt:表示连接到本套接口上的进程号;
    • Types:显示套接口的类型;
    • State:显示套接口当前的状态;
    • Path:表示连接到套接口的其它进程使用的路径名。

命令常见参数

  • -a(all) :显示所有选项,默认不显示LISTEN相关;
  • -t(tcp): 仅显示 tcp 相关选项;
  • -u (udp) :仅显示 udp 相关选项;
  • -n(numeric):拒绝显示别名,能显示数字的全部转化成数字;
  • -l(listening):仅列出有在 Listen (监听) 的服务状态;
  • -p(program):显示建立相关链接的 PID 和程序名;
  • -r(route):显示路由信息,路由表;
  • -e(extend):显示扩展信息,例如 uid 等;
  • -s(statistics):按各个协议进行统计;

查看所有使用 tcpudp 协议的服务状况:

  1. $ netstat -nlptu
  2. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  3. tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 322/rpcbind
  4. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 583/sshd
  5. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 893/master
  6. tcp6 0 0 :::3306 :::* LISTEN 982/docker-proxy
  7. tcp6 0 0 :::6379 :::* LISTEN 993/docker-proxy
  8. tcp6 0 0 :::111 :::* LISTEN 322/rpcbind
  9. tcp6 0 0 :::8848 :::* LISTEN 1396/java
  10. tcp6 0 0 :::22 :::* LISTEN 583/sshd
  11. tcp6 0 0 ::1:25 :::* LISTEN 893/master
  12. udp6 0 0 :::49830 :::* 1396/java

Ps 命令


ps(process status) 是一个用于查看进程状态信息的命令,使用该命令可以确定有哪些进程正在运行和运行的状态、进程是否结束、进程有没有僵尸、哪些进程占用了过多的资源等等。

常用参数:

  • -A :所有的进程均显示出来,与 -e 具有同样的效用;
  • -a : 显示现行终端机下的所有进程,包括其他用户的进程;
  • -u :以用户为主的进程状态 ;
  • x :通常与 a 这个参数一起使用,可列出较完整信息。

常用命令:

列出目前所有的正在内存当中的程序:

  1. $ ps -aux
  2. USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
  3. root 1 0.0 0.7 128032 3620 ? Ss 04:21 0:01 /usr/lib/systemd/systemd
  4. root 2 0.0 0.0 0 0 ? S 04:21 0:00 [kthreadd]
  5. root 4 0.0 0.0 0 0 ? S< 04:21 0:00 [kworker/0:0H]
  6. root 5 0.0 0.0 0 0 ? S 04:21 0:00 [kworker/u2:0]
  7. root 6 0.0 0.0 0 0 ? S 04:21 0:00 [ksoftirqd/0]
  8. root 7 0.0 0.0 0 0 ? S 04:21 0:00 [migration/0]
  9. root 8 0.0 0.0 0 0 ? S 04:21 0:00 [rcu_bh]
  10. root 9 0.0 0.0 0 0 ? R 04:21 0:01 [rcu_sched]
  11. root 10 0.0 0.0 0 0 ? S< 04:21 0:00 [lru-add-drain]
  12. root 11 0.0 0.0 0 0 ? S 04:21 0:00 [watchdog/0]
  13. root 13 0.0 0.0 0 0 ? S 04:21 0:00 [kdevtmpfs]
  14. root 14 0.0 0.0 0 0 ? S< 04:21 0:00 [netns]

字段名解释:

  • USER:该进程属于那个使用者账号的?
  • PID :该进程的进程ID号。
  • %CPU:该进程使用掉的 CPU 资源百分比;
  • %MEM:该进程所占用的物理内存百分比;
  • VSZ :该进程使用掉的虚拟内存量 (Kbytes)
  • RSS :该进程占用的固定的内存量 (Kbytes)
  • TTY :该进程是在那个终端机上面运作,若与终端机无关,则显示 ?,另外, tty1-tty6 是本机上面的登入者程序,若为 pts/0 等等的,则表示为由网络连接进主机的程序。
  • STAT:该程序目前的状态,主要的状态有:

    • R(running):该程序目前正在运作,或者是可被运作;
    • S(sleep):该程序目前正在睡眠当中 (可说是 idle 状态啦!),但可被某些讯号(signal) 唤醒。
    • T(terminate):该程序目前正在侦测或者是停止了;
    • Z(zombie):该程序应该已经终止,但是其父程序却无法正常的终止他,造成 zombie (疆尸) 程序的状态
  • START:该进程被触发启动的时间;
  • TIME :该进程实际使用 CPU 运作的时间;
  • COMMAND:该程序的实际指令为什么;

注意:由于 ps 能够支持的系统类型相当的多,所以他的参数多的离谱!而且有没有加上 - 差很多!详细的用法应该参考 man ps

grep 命令结合过滤指定程序运行状态:

  1. $ ps -aux | grep java
  2. root 1973 0.0 0.1 12528 972 pts/0 R+ 05:21 0:00 grep --color=auto java

时区设置


1、查看当前时区

  1. $ date -R
  2. Fri, 15 Jan 2021 06:50:14 +0000

2、修改时区

1)、使用 tzselect 命令

  1. $ tzselect
  2. # 输入 5 选择亚洲
  3. Please identify a location so that time zone rules can be set correctly.
  4. Please select a continent or ocean.
  5. 1) Africa
  6. 2) Americas
  7. 3) Antarctica
  8. 4) Arctic Ocean
  9. 5) Asia
  10. 6) Atlantic Ocean
  11. 7) Australia
  12. 8) Europe
  13. 9) Indian Ocean
  14. 10) Pacific Ocean
  15. 11) none - I want to specify the time zone using the Posix TZ format.
  16. #? 5
  17. # 输入9选择中国
  18. Please select a country.
  19. 1) Afghanistan 18) Israel 35) Palestine
  20. 2) Armenia 19) Japan 36) Philippines
  21. 3) Azerbaijan 20) Jordan 37) Qatar
  22. 4) Bahrain 21) Kazakhstan 38) Russia
  23. 5) Bangladesh 22) Korea (North) 39) Saudi Arabia
  24. 6) Bhutan 23) Korea (South) 40) Singapore
  25. 7) Brunei 24) Kuwait 41) Sri Lanka
  26. 8) Cambodia 25) Kyrgyzstan 42) Syria
  27. 9) China 26) Laos 43) Taiwan
  28. 10) Cyprus 27) Lebanon 44) Tajikistan
  29. 11) East Timor 28) Macau 45) Thailand
  30. 12) Georgia 29) Malaysia 46) Turkmenistan
  31. 13) Hong Kong 30) Mongolia 47) United Arab Emirates
  32. 14) India 31) Myanmar (Burma) 48) Uzbekistan
  33. 15) Indonesia 32) Nepal 49) Vietnam
  34. 16) Iran 33) Oman 50) Yemen
  35. 17) Iraq 34) Pakistan
  36. #? 9
  37. # 输入 1 选择北京时间
  38. Please select one of the following time zone regions.
  39. 1) Beijing Time
  40. 2) Xinjiang Time
  41. #? 1
  42. # 输入 1 确认
  43. The following information has been given:
  44. China
  45. Beijing Time
  46. Therefore TZ='Asia/Shanghai' will be used.
  47. Local time is now: Fri Jan 15 14:56:34 CST 2021.
  48. Universal Time is now: Fri Jan 15 06:56:34 UTC 2021.
  49. Is the above information OK?
  50. 1) Yes
  51. 2) No
  52. #? 1
  53. # 提示信息
  54. You can make this change permanent for yourself by appending the line
  55. TZ='Asia/Shanghai'; export TZ
  56. to the file '.profile' in your home directory; then log out and log in again.
  57. Here is that TZ value again, this time on standard output so that you
  58. can use the /usr/bin/tzselect command in shell scripts:
  59. Asia/Shanghai

从上面的结果中可以看出我们已经成功设置了时区,但是根据提示信息可以知道想要生效时区,我们需要在用户的家目录的 .bash_profile 文件中添加 TZ='Asia/Shanghai'; export TZ 这句脚本内容,然后重新登录。

写进脚本可以永久生效,如果想要临时生效,我们可以直接执行该命令即可:

  1. $ TZ='Asia/Shanghai'; export TZ
  2. $ date -R
  3. Fri, 15 Jan 2021 15:02:58 +0800

可以看到已经成功设置为了东八区(+0800)了。

2)、使用 timedatectl 命令

  1. # 查看当前时区
  2. $ timedatectl status
  3. Warning: Ignoring the TZ variable. Reading the system's time zone setting only. Local time: Fri 2021-01-15 07:08:27 UTC Universal time: Fri 2021-01-15 07:08:27 UTC RTC time: Fri 2021-01-15 07:08:23 Time zone: UTC (UTC, +0000) NTP enabled: yes NTP synchronized: yes RTC in local TZ: no DST active: n/a # 查看亚洲时区列表 $ timedatectl list-timezones | grep "Asia/S" Asia/Sakhalin Asia/Samarkand Asia/Seoul Asia/Shanghai Asia/Singapore Asia/Srednekolymsk # 设置时区 $ timedatectl set-timezone "Asia/Shanghai" # 查看设置结果 $ timedatectl status Warning: Ignoring the TZ variable. Reading the system's time zone setting only.
  4. Local time: Fri 2021-01-15 15:17:44 CST
  5. Universal time: Fri 2021-01-15 07:17:44 UTC
  6. RTC time: Fri 2021-01-15 07:17:40
  7. Time zone: Asia/Shanghai (CST, +0800)
  8. NTP enabled: yes
  9. NTP synchronized: yes
  10. RTC in local TZ: no
  11. DST active: n/a

3)、设置时间

  1. $ date -s "2021-01-15 15:20:30"

日志查看


1、动态跟踪日志输出

有时候,我们需要跟踪一下打包部署的日志输出内容,这时我们可以使用 tail 命令来进行跟踪:

  1. $ tail -f xxx.log

软件安装和卸载


1、Redhat/CentOS

安装软件包:

  1. $ yum install -y [packageName]

查看已安装软件包:

  1. $ yum list installed | grep [packageName]

列出所有可更新的软件:

  1. $ yum check-update

更新所有可更新软件:

  1. $ yum update

更新指定软件:

  1. $ yum update [packageName]

卸载软件包:

  1. $ yum remove [packageName]

发表评论

表情:
评论列表 (有 0 条评论,123人围观)

还没有评论,来说两句吧...

相关阅读

    相关 linux命令seq

    seq命令用于以指定增量从首数开始打印数字到尾数,即产生从某个数到另外一个数之间的所有整数,并且可以对整数的格式、宽度、分割符号进行控制。 语法:   

    相关 Linux命令

    文件目录基本操作 ls 命令用来显示目标列表,在Linux中是使用率较高的命令。ls命令的输出信息可以进行彩色加亮显示,以分区不同类型的文件。常用选项: