【Linux】之常用命令
目录
- 快捷键
- 文件操作
- Netstat 命令
- Ps 命令
- 时区设置
- 软件安装和卸载
Bash 常用快捷键
- Ctrl + L :清空终端页面
- Ctrl + u :清空命令
文件操作
1、清空文件内容
# 通过 shell 重定向 null
[root@wang log] > access.log
# 使用 : 符号
[root@wang log] : > access.log
[root@wang log] true > access.log
# 使用 cat/cp/dd 实用工具及 /dev/null 设备
[root@wang log] cat /dev/null > access.log
[root@wang log] cp/dev/null > access.log
[root@wang log] dd /dev/null > access.log
# 使用 echo 命令
[root@wang log] "" > access.log
[root@wang log] echo > access.log
Netstat 命令
Netstat 是一个查看网络状态的命令,是一个监控 TCP/IP
网络的非常有用的工具,它可以显示路由表、实际的网络连接以及每一个网络接口设备的状态信息。Netstat 用于显示与 IP、TCP、UDP
和 ICMP
协议相关的统计数据,一般用于检验本机各端口的网络连接情况。
使用 netstat -V
命令检查是否已安装该命令,如果没有安装的话使用以下命令进行安装:
$ yum install net-tools
$ netstat -V
net-tools 2.10-alpha
netstat 的命令分为两大块:
- Active Internet connections:有源TCP连接,其中
Recv-Q
和Send-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):按各个协议进行统计;
查看所有使用 tcp
或 udp
协议的服务状况:
$ netstat -nlptu
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 322/rpcbind
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 583/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 893/master
tcp6 0 0 :::3306 :::* LISTEN 982/docker-proxy
tcp6 0 0 :::6379 :::* LISTEN 993/docker-proxy
tcp6 0 0 :::111 :::* LISTEN 322/rpcbind
tcp6 0 0 :::8848 :::* LISTEN 1396/java
tcp6 0 0 :::22 :::* LISTEN 583/sshd
tcp6 0 0 ::1:25 :::* LISTEN 893/master
udp6 0 0 :::49830 :::* 1396/java
Ps 命令
ps(process status) 是一个用于查看进程状态信息的命令,使用该命令可以确定有哪些进程正在运行和运行的状态、进程是否结束、进程有没有僵尸、哪些进程占用了过多的资源等等。
常用参数:
- -A :所有的进程均显示出来,与 -e 具有同样的效用;
- -a : 显示现行终端机下的所有进程,包括其他用户的进程;
- -u :以用户为主的进程状态 ;
- x :通常与 a 这个参数一起使用,可列出较完整信息。
常用命令:
列出目前所有的正在内存当中的程序:
$ ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.7 128032 3620 ? Ss 04:21 0:01 /usr/lib/systemd/systemd
root 2 0.0 0.0 0 0 ? S 04:21 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? S< 04:21 0:00 [kworker/0:0H]
root 5 0.0 0.0 0 0 ? S 04:21 0:00 [kworker/u2:0]
root 6 0.0 0.0 0 0 ? S 04:21 0:00 [ksoftirqd/0]
root 7 0.0 0.0 0 0 ? S 04:21 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S 04:21 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? R 04:21 0:01 [rcu_sched]
root 10 0.0 0.0 0 0 ? S< 04:21 0:00 [lru-add-drain]
root 11 0.0 0.0 0 0 ? S 04:21 0:00 [watchdog/0]
root 13 0.0 0.0 0 0 ? S 04:21 0:00 [kdevtmpfs]
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
命令结合过滤指定程序运行状态:
$ ps -aux | grep java
root 1973 0.0 0.1 12528 972 pts/0 R+ 05:21 0:00 grep --color=auto java
时区设置
1、查看当前时区
$ date -R
Fri, 15 Jan 2021 06:50:14 +0000
2、修改时区
1)、使用 tzselect
命令:
$ tzselect
# 输入 5 选择亚洲
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
1) Africa
2) Americas
3) Antarctica
4) Arctic Ocean
5) Asia
6) Atlantic Ocean
7) Australia
8) Europe
9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 5
# 输入9选择中国
Please select a country.
1) Afghanistan 18) Israel 35) Palestine
2) Armenia 19) Japan 36) Philippines
3) Azerbaijan 20) Jordan 37) Qatar
4) Bahrain 21) Kazakhstan 38) Russia
5) Bangladesh 22) Korea (North) 39) Saudi Arabia
6) Bhutan 23) Korea (South) 40) Singapore
7) Brunei 24) Kuwait 41) Sri Lanka
8) Cambodia 25) Kyrgyzstan 42) Syria
9) China 26) Laos 43) Taiwan
10) Cyprus 27) Lebanon 44) Tajikistan
11) East Timor 28) Macau 45) Thailand
12) Georgia 29) Malaysia 46) Turkmenistan
13) Hong Kong 30) Mongolia 47) United Arab Emirates
14) India 31) Myanmar (Burma) 48) Uzbekistan
15) Indonesia 32) Nepal 49) Vietnam
16) Iran 33) Oman 50) Yemen
17) Iraq 34) Pakistan
#? 9
# 输入 1 选择北京时间
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1
# 输入 1 确认
The following information has been given:
China
Beijing Time
Therefore TZ='Asia/Shanghai' will be used.
Local time is now: Fri Jan 15 14:56:34 CST 2021.
Universal Time is now: Fri Jan 15 06:56:34 UTC 2021.
Is the above information OK?
1) Yes
2) No
#? 1
# 提示信息
You can make this change permanent for yourself by appending the line
TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai
从上面的结果中可以看出我们已经成功设置了时区,但是根据提示信息可以知道想要生效时区,我们需要在用户的家目录的 .bash_profile
文件中添加 TZ='Asia/Shanghai'; export TZ
这句脚本内容,然后重新登录。
写进脚本可以永久生效,如果想要临时生效,我们可以直接执行该命令即可:
$ TZ='Asia/Shanghai'; export TZ
$ date -R
Fri, 15 Jan 2021 15:02:58 +0800
可以看到已经成功设置为了东八区(+0800)了。
2)、使用 timedatectl
命令:
# 查看当前时区
$ timedatectl status
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.
Local time: Fri 2021-01-15 15:17:44 CST
Universal time: Fri 2021-01-15 07:17:44 UTC
RTC time: Fri 2021-01-15 07:17:40
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
3)、设置时间
$ date -s "2021-01-15 15:20:30"
日志查看
1、动态跟踪日志输出
有时候,我们需要跟踪一下打包部署的日志输出内容,这时我们可以使用 tail
命令来进行跟踪:
$ tail -f xxx.log
软件安装和卸载
1、Redhat/CentOS
安装软件包:
$ yum install -y [packageName]
查看已安装软件包:
$ yum list installed | grep [packageName]
列出所有可更新的软件:
$ yum check-update
更新所有可更新软件:
$ yum update
更新指定软件:
$ yum update [packageName]
卸载软件包:
$ yum remove [packageName]
还没有评论,来说两句吧...