1、新手必须掌握的Linux常用命令
1.1、echo命令
[root@chenstudy ~]# echo chen
chen
[root@chenstudy ~]# echo $SHELL
/bin/bash
1.2、date命令
# 查看当前系统时间
[root@chenstudy ~]# date
2022年 04月 09日 星期六 22:32:21 CST
# 按照“年-月-日 小时:分钟:秒”的格式查看当前系统时间
[root@chenstudy ~]# date "+%Y-%m-%d %H:%M:%S"
2022-04-09 22:32:46
# 将当前系统的时间设置为2022年04月0922点36分
[root@chenstudy ~]# date -s "20220409 22:36:00"
2022年 04月 09日 星期六 22:36:00 CST
# 再次使用date查看系统当前时间
[root@chenstudy ~]# date
2022年 04月 09日 星期六 22:36:11 CST
# dtae "+j" 用来查看今天是当年中的第几天
[root@chenstudy ~]# date "+%j"
099
1.3、wget命令
# wget 命令用于在终端中下载网络文件,格式为“wget [参数] 下载地址”。
[root@chenstudy ~]# cd /home
[root@chenstudy home]# wget http://www.linuxprobe.com/docs/LinuxProbe.pdf
-b # 后台下载模式
-P # 下载到指定目录
-t # 最大尝试次数
-c # 断点续传
-p # 下载页面内所有资源,包括图片、视频等
-r # 递归下载
# wget 命令递归下载 www.linuxprobe.com 网站内的所有页面数据以及文件,下载完后会自动保存到当前路径下一个名为 www.linuxprobe.com 的目录中
[root@chenstudy home]# wget -r -p http://www.linuxprobe.com
1.4、ps命令
- ps 命令用于查看系统中的进程状态,格式为“ps [参数]”
- 参数
- -a 显示所有进程(包括其他用户的进程)
- -u 用户以及其他详细信息
- -x 显示没有控制终端的进程
- Linux的进程分为五个进程状态 运行、中断、不可中断、僵死与停止
- R(运行):进程正在运行或在运行队列中等待。
- S(中断):进程处于休眠中,当某个条件形成后或者接收到信号时,则脱离该状态。
- D(不可中断):进程不响应系统异步信号,即便用 kill 命令也不能将其中断。
- Z(僵死):进程已经终止,但进程描述符依然存在, 直到父进程调用 wait4()系统函数后将进程释放。
- T(停止):进程收到停止信号后停止运行。
[root@chenstudy home]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 125504 3440 ? Ss 3月24 0:15 /usr/lib/systemd/systemd --switched-root --system --de
root 2 0.0 0.0 0 0 ? S 3月24 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? S< 3月24 0:00 [kworker/0:0H]
root 6 0.0 0.0 0 0 ? S 3月24 0:18 [ksoftirqd/0]
root 7 0.0 0.0 0 0 ? S 3月24 0:00 [migration/0]
root 8 0.0 0.0 0 0 ? S 3月24 0:00 [rcu_bh]
# USER 进程的所有者
# PID 进程ID号
# %CPU 运算器占用率
# %MEM 内存占用率
# VSZ 虚拟内存使用量(单位是 KB)
# RSS 占用的固定内存量(单位是KB)
# TTY 所在终端
# STAT 进程状态
# START 被启动的时间
# TIME 实际使用CPU的时间
# COMMAND 命令名称与参数
1.5、top 命令
- top 命令用于动态地监视进程活动与系统负载等信息,其格式为 top
[root@chenstudy home]# top
- top 命令执行结果的前 5 行为系统整体的统计信息,其所代表的含义如下:
- 第 1 行:系统时间、运行时间、登录终端数、系统负载(三个数值分别为 1 分钟、5分钟、15 分钟内的平均值,数值越小意味着负载越低)
- 第 2 行:进程总数、运行中的进程数、睡眠中的进程数、停止的进程数、僵死的进程数
- 第 3 行:用户占用资源百分比、系统内核占用资源百分比、改变过优先级的进程资源百分比、空闲的资源百分比等
- 第 4 行:物理内存总量、内存使用量、内存空闲量、作为内核缓存的内存量
- 第 5 行:虚拟内存总量、虚拟内存使用量、虚拟内存空闲量、已被提前加载的内存量
1.6、pidof和kill命令
- pidof 命令用于查询某个指定服务进程的 PID 值,格式为“pidof [参数 服务名称]”
- kill 命令用于终止某个指定 PID 的服务进程,格式为“kill [参数 进程 PID]”
- killall 命令用于终止某个指定名称的服务所对应的全部进程,格式为:“killall [参数 进程名称]”
# 查询本机上 sshd 服务程序的 PID
[root@chenstudy ~]# pidof sshd
24486 1156
[root@chenstudy ~]# kill 1156
[root@chenstudy ~]# pidof httpd
[root@chenstudy ~]# killall httpd
1.7、ifconfig命令
- ifconfig 命令用于获取网卡配置与网络状态等信息,格式为“ifconfig [网络设备 参数]”
[root@chenstudy ~]# ifconfig
1.8、uname 命令
- uname 命令用于查看系统内核与系统版本等信息,格式为“uname [-a]”
- 查看当前系统版本的详细信息,则需要查看 redhat-release 文件,
[root@chenstudy ~]# uname -a
Linux chenstudy 3.10.0-1160.53.1.el7.x86_64 #1 SMP Fri Jan 14 13:59:45 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
[root@chenstudy etc]# cat centos-release
CentOS Linux release 7.9.2009 (Core)
[root@chenstudy etc]#
1.9、uptime和free命令
- uptime 用于查看系统的负载信息,格式为 uptime
- 它可以显示当前系统时间、系统已运行时间、启用终端数量以 及平均负载值等信息
- free 用于显示当前系统中内存的使用量信息,格式为“free [-h]”
[root@chenstudy ~]# uptime
23:11:18 up 17 days, 6:46, 2 users, load average: 0.05, 0.22, 0.28
[root@chenstudy ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 347M 192M 612K 1.3G 1.3G
Swap: 0B 0B 0B
# total 内存总量
# used 已用量
# free 可用量
# shared 进程共享的内存量
# buff/cache 磁盘缓存的内存量/缓存的内存量
# available 可利用的内存量
1.10、who和last命令
- who 用于查看当前登入主机的用户终端信息,格式为“who [参数]”
[root@chenstudy ~]# who
root pts/0 2022-04-10 20:07 (117.158.163.68)
root pts/1 2022-04-10 20:07 (117.158.163.68)
[root@chenstudy ~]#
执行 who 命令的结果
登录的用户名 终端设备 登录到系统的时间
root pts/0 2022-04-10 20:07 (117.158.163.68)
root pts/1 2022-04-10 20:07 (117.158.163.68)
- last 命令用于查看所有系统的登录记录,格式为“last [参数]”
# 使用 last 命令可以查看本机的登录记录由于这些信息都是以日志文件的形式保存在系统中,因此黑客可以很容易地对内容进行篡改
[root@chenstudy ~]# last
root pts/1 117.158.163.68 Sun Apr 10 20:07 still logged in
root pts/0 117.158.163.68 Sun Apr 10 20:07 still logged in
root pts/1 117.158.163.67 Sun Apr 10 15:12 - 19:27 (04:14)
root pts/0 117.158.163.67 Sun Apr 10 15:12 - 19:27 (04:14)
root pts/1 117.158.163.67 Sun Apr 10 08:29 - 12:27 (03:58)
root pts/0 117.158.163.67 Sun Apr 10 08:29 - 12:27 (03:58)
root pts/1 117.158.163.79 Sat Apr 9 22:25 - 23:49 (01:24)
root pts/0 117.158.163.79 Sat Apr 9 22:25 - 23:49 (01:24)
root pts/1 117.158.163.78 Thu Apr 7 08:39 - 08:42 (00:03)
root pts/0 117.158.163.78 Thu Apr 7 08:39 - 08:42 (00:03)
root pts/1 117.158.163.78 Wed Apr 6 16:22 - 20:14 (03:52)
root pts/3 117.158.163.78 Wed Apr 6 15:45 - 20:14 (04:28)
root pts/4 117.158.163.78 Wed Apr 6 15:38 - 15:40 (00:01)
1.11、history和sosreport 命令
- history 命令用于显示历史执行过的命令,格式为“history [-c]”
- -c会清空所有的命令历史记录
- sosreport 命令用于收集系统配置及架构信息并输出诊断文档,格式为sosreport
[root@chenstudy ~]# history
1 clear
2 cd /
3 uname -f
4 clear
5 uname -r
6 clear
7 cat /etc/os-release
# 历史命令会被保存到用户家目录中的.bash_history 文件中
[root@chenstudy ~]# cat ~/.bash_history
clear
cd /
uname -f
clear
uname -r
clear
cat /etc/os-release
# 清空当前用户在本机上执行的Linux命令历史记录
[root@chenstudy ~]# history -c