Linux就该这么学

 

《linux就该这么学》

常用系统工作命令

echo

echo $JAVA_HOME

date

参数 作用
%t tab
%H 小时(00-23)
%I 小时(00-12)
%M 分钟(00-59)
%S 秒(00-59)
%j 今年中的第几天
以该格式查看时间
date "+%Y-%m-%d %H:%M:%S"

修改系统时间
date -s "20170901 8:30:00"

reboot

poweroff hlat shutdown

halt, poweroff, shutdown 有什么区别?

wget

参数 作用
-b 后台
-P 下载到执行目录
-t 最大尝试次数
-c 断点续传
-p 下载页面内所有资源
-r 递归下载

wget -r -p http://www.linuxprobe.com

ps

参数 作用
-A,-e all processes
-a all with tty, except session leaders
a all with tty, including other users
-d all except session leaders
-N,–deselect negate selection
r only running processes
T all processes on this terminal
-u 用户以及其他详细信息
-x processes without controlling ttys
-f 显示UID,PPIP,C与STIME栏位
进程状态 描述
R(运行) 进程正在运行或在运行队列中等待
S(中断) 进程正在休眠中,当某个条件形成后或者接收到信号后,则脱离该状态
D(不可中断) 进程不响应系统异步信号,即便用kill命令也不能将其中断
Z(僵死) 进程已经终止,但进程描述符依然存在,知道父进程调用wait4()系统函数后将进程释放
T(停止) 进程收到停止信号后停止运行
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
进程所有者 pid cpu使用 内存使用 虚拟内存使用量(KB) 占用的固定内存量(KB) 所在终端 进程状态 启动时间 实际使用的cpu时间 命令参数
root 1 0.0 0.4 53684 7628 ? Ss 07:22 0:02 /usr/lib/systemd/systemd
root 2 0.0 0.0 0 0 ? S 07:22 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 07:22 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 07:22 0:00 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S 07:22 0:00 [migration/0]

注意:linux系统中的命令参数有长短格式之分,长格式和长格式之间不能合并,长格式与短格式之间也不能合并,但短格式之间可以合并。ps命令也允许参数不加减号,可以直接写成ps aux

top

top - 17:27:12 up  9:13,  1 user,  load average: 1.34, 1.07, 1.01
Tasks: 295 total,   1 running, 230 sleeping,   0 stopped,   1 zombie
%Cpu(s):  4.1 us,  3.9 sy,  0.0 ni, 91.9 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16324172 total,  3618984 free,  4123052 used,  8582136 buff/cache
KiB Swap: 15728636 total, 15728636 free,        0 used. 10677768 avail Mem 

PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                          
6575 root    20   0 2742668 273596  39720 S  13.0  1.7   8:57.71 baidunetdisk.ex                  
5197 root    20   0 4789528 208632 148240 S   5.3  1.3   3:55.83 netease-cloud-m                  
6580 root    20   0   11528   8952   1984 S   4.7  0.1   2:53.57 wineserver.real                  
3989 root      20   0  967036 238716 185424 S   2.7  1.5  13:05.06 Xorg                             
4596 root    20   0 3320512 161256 103548 S   1.7  1.0   9:24.36 kwin_x11              
  • 第一行: 系统时间,运行时间,登录用户数,系统负载(1分钟,5分钟,1分钟)
  • 第二行:进程总数,运行中的,睡眠中的,停止的,僵死的。
  • 第三行:用户占用资源百分比,同内核占用资源百分比,改变过优先级的进程* 资源百分比,空闲的百分比等。
  • 第四行:物理内存总量,内存已使用量,内存空闲量,作为内核缓存的内存量
  • 第五行:虚拟内存总量,内存已使用量,内存空闲量,作为内核缓存的内存量

pidof

查询指定服务的pid。pidof sshd

kill

killall

终止某个指定名称的服务所对应的全部进程

系统状态检测命令

ifconfig

用户获取网卡配置与网络状态等信息

root@deepin:~/test# ifconfig
enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.75  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::a414:4439:16f3:457f  prefixlen 64  scopeid 0x20<link>
        ether 20:1a:06:be:31:f0  txqueuelen 1000  (Ethernet)
        RX packets 1075352  bytes 1296697676 (1.2 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 755115  bytes 145568300 (138.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 16  
value 含义
enp2s0 网卡名称
inet 192.168.10.75 ip地址
ether 20:1a:06:be:31:f0 MAC地址
RX packets 1075352 bytes 1296697676 (1.2 GiB) 入站流量
TX packets 755115 bytes 145568300 (138.8 MiB) 出站流量
netmask 255.255.255.0 子网掩码
broadcast 192.168.10.255 将要送往指定地址的数据包当成广播数据包来处理
mtu 1500 最大数据包大小

uname

root@deepin:~/test# uname -a
Linux deepin 4.15.0-30deepin-generic #31 SMP Fri Nov 30 04:29:02 UTC 2018 x86_64 GNU/Linux
value 含义
Linux 当前系统的内核名称
deepin 主机名
4.15.0-30deepin-generic 内核发行版本
#31 SMP Fri Nov 30 04:29:02 UTC 2018 内核编译日期
x86_64 操作系统版本、处理器类型、硬件平台
GNU/Linux 操作系统名称
# redhat
[root@linuxprobe ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.0 (Maipo)

# centos
[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

# debian
root@deepin:~/test# cat /etc/debian_version 
9.0

# deepin
root@deepin:~/test# cat /etc/deepin-version 
[Release]
Version=15.10.2
Type=Desktop
Type[zh_CN]=桌面版

[Addition]
Milestone=

uptime

root@deepin:~/test# uptime
 19:12:19 up 10:58,  1 user,  load average: 1.00, 0.82, 0.69

free

root@deepin:~/test# free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        3.9G        3.0G        1.2G        8.6G         10G
Swap:           14G          0B         14G

shared: 进程间共享内存量

who

用于查看当前登入主机的用户终端信息

root@deepin:~/test# who
root   tty1         2019-07-15 08:14 (:0)

last

查询系统的所有登录记录

[root@localhost ~]# last
root     pts/0        192.168.10.75    Mon Jul 15 07:28   still logged in   
root     pts/0        172.22.0.5       Mon Jul 15 05:01 - 05:01  (00:00)    
root     pts/0        172.22.0.5       Mon Jul 15 05:01 - 05:01  (00:00)    
root     pts/0        172.22.0.5       Mon Jul 15 05:01 - 05:01  (00:00)    
root     pts/0        172.22.0.5       Mon Jul 15 05:01 - 05:01  (00:00)    
root     pts/0        172.22.0.5       Mon Jul 15 05:00 - 05:01  (00:00)    
root     pts/0        172.22.0.5       Mon Jul 15 05:00 - 05:00  (00:00)    
root     pts/0        172.22.0.5       Mon Jul 15 05:00 - 05:00  (00:00) 

history

显示当前用户执行过的最近1000条命令记录。

可自定义/etc/profile中的HISTSIZE值来修改保存的条数

可通过cat ~/.bash_history查看文件内容

使用history -c清空记录

sosreport

用于收集系统配置及架构信息并输出诊断文档。

# redhat/centos下的安装
yum -y insatll sos
# ubuntu下的安装
sudo apt-get install sosreport

linux信息收集篇之sosreport

工作目录切换命令

pwd

cd

# 前往/etc目录
cd /etc

# 返回上一次的目录
cd -

# 返回用户目录
cd ~

ls

ls -a
ls -la
ls -lh

du

# 查看当前目录下的每个目录的大小
[root@localhost usr]# du -h --max-depth=1
304M	./bin
42M	./sbin
814M	./lib
147M	./lib64
244M	./share
0	./etc
0	./games
36K	./include
12M	./libexec
16M	./local
0	./src
1.6G	.

文本文件编辑命令

cat

用于查看纯文本文件(内容较少的)

cat a.txt,添加-n参数可以显示行号cat -n a.txt.

more

用于查看长篇的文本内容,空格键或者回车键向下翻页。

more a.txt

用于查看前20行的内容

head -n 20 a.txt

tail

用于查看纯文本的后n行或持续刷新内容

查看文本最后20行

tail -n 20 a.txt

持续观察文本

tail -f a.txt

tr

用于替换文本文件中的字符 “tr [原始字符] [目标字符]”