Linux命令

echo 打印变量

1 2 3 4 5
echo Javahome.Cn Javahome.Cn echo $SHELL /bin/bash

date 显示、设置系统日期时间

date命令中的参数以及作用

参数作用
%t跳格[Tab键]
%H小时(00~23)
%I小时(00~12)
%M分钟(00~59)
%S秒(00~59)
%j今年中的第几天

 1  2  3  4  5  6  7  8  9 10 11
date Mon Aug 24 16:11:23 CST 2017 date "+%Y-%m-%d %H:%M:%S" 2017-08-24 16:29:12 date -s "20170901 8:30:00" Fri Sep 1 08:30:00 CST 2017 date "+%j" 244

reboot 重启

1
reboot

poweroff 关机

1
poweroff

wget 下载网络文件

wget命令的参数以及作用

参数作用
-b后台下载模式
-P下载到指定目录
-t最大尝试次数
-c断点续传
-p下载页面内所有资源,包括图片、视频等
-r递归下载

1 2 3 4
wget http://www.linuxprobe.com/docs/LinuxProbe.pdf #递归下载 wget -r -p http://www.linuxprobe.com

ps 查看进程状态

ps命令的参数以及作用

参数作用
-a显示所有进程(包括其他用户的进程)
-u用户以及其他详细信息
-x显示没有控制终端的进程

ps aux 进程状态

USER进程的所有者
PID进程ID号
%CPU运算器占用率
%MEM内存占用率
VSZ虚拟内存使用量(单位是KB)
RSS占用的固定内存量(单位是KB)
TTY所在终端
STAT进程状态
START被启动的时间
TIME实际使用CPU的时间
COMMAND命令名称与参数

top 动态监视进程

1 2 3 4 5 6 7 8 9
top - 19:19:51 up 11 min, 2 users, load average: 0.01, 0.17, 0.19 Tasks: 519 total, 2 running, 517 sleeping, 0 stopped, 0 zombie %Cpu(s): 1.1 us, 0.6 sy, 0.0 ni, 97.1 id, 1.1 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem: 3869044 total, 900604 used, 2968440 free, 924 buffers KiB Swap: 2097148 total, 0 used, 2097148 free, 278556 cached Mem

1、系统时间、运行时间、登录终端数、系统负载(三个数值分别为1分钟、5分钟、15分钟内的平均值,数值越小意味着负载越低)

2、进程总数、运行中的进程数、睡眠中的进程数、停止的进程数、僵死的进程数

3、用户占用资源百分比、系统内核占用资源百分比、改变过优先级的进程资源百分比、空闲的资源百分比(97.1 id意味着有97.1%的CPU处理器资源处于空闲)等

4、物理内存总量、内存使用量、内存空闲量、作为内核缓存的内存量

5、虚拟内存总量、虚拟内存使用量、虚拟内存空闲量、已被提前加载的内存量

pidof 查询进程pid

1 2
pidof sshd 2156

kill 终止进程

1
kill 2156

killall 终止服务全部进程

1 2 3 4 5
pidof httpd 13581 13580 13579 13578 13577 13576 killall httpd pidof httpd

ifconfig 查看网卡信息

1 2 3 4 5 6 7 8 9
ifconfig eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255 inet6 fe80::20c:29ff:fec4:a409 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c4:a4:09 txqueuelen 1000 (Ethernet) RX packets 36 bytes 3176 (3.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 38 bytes 4757 (4.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

网卡名称、inet参数后面的IP地址、ether参数后面的网卡物理地址(又称为MAC地址)

RX、TX的接收数据包与发送数据包的个数及累计流量

uname 查看系统内核

1 2
uname -a Linux linuxprobe.com 3.10.0-123.el7.x86_64 #1 SMP Mon May 5 11:16:57 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux

当前系统的内核名称、主机名、内核发行版本、节点名、系统时间、硬件名称、硬件平台、处理器类型以及操作系统名称

查看系统版本

1 2
cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.0 (Maipo)

uptime 查看系统负载

1 2
uptime 22:49:55 up 10 min, 2 users, load average: 0.01, 0.19, 0.18

当前系统时间、系统已运行时间、启用终端数量以及平均负载值等信息。平均负载值指的是系统在最近1分钟、5分钟、15分钟内的压力情况(下面加粗的信息部分)

负载值越低越好,尽量不要长期超过1,在生产环境中不要超过5

free 查看内存使用量

1
free -h

free -h

内存总量total
已用量used
可用量free
进程共享的内存量shared
磁盘缓存的内存量buffers
缓存的内存量cached

who 查看当前登录用户

1
who

登录的用户名root
终端设备:0
登录到系统的时间2017-08-24_17:52_(:0)

last 查看登录记录

1 2
last root pts/0 :0 Mon Aug 24 17:52 still logged in

history 查看历史命令

自定义/etc/profile文件中的HISTSIZE变量值

 1  2  3  4  5  6  7  8  9 10 11 12 13
history 1 tar xzvf VMwareTools-9.9.0-2304977.tar.gz 2 cd vmware-tools-distrib/ 3 ls !3 anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates #查看历史命令文件 cat ~/.bash_history #清空历史命令 history -c

sosreport 输出系统诊断报告

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
sosreport sosreport (version 3.0) This command will collect diagnostic and configuration information from this Red Hat Enterprise Linux system and installed applications. An archive containing the collected information will be generated in /var/tmp and may be provided to a Red Hat support representative. Any information provided to Red Hat will be treated in accordance with the published support policies at: https://access.redhat.com/support/ The generated archive may contain data considered sensitive and its content should be reviewed by the originating organization before being passed to any third party. No changes will be made to system configuration. Press ENTER to continue, or CTRL-C to quit. 此处敲击回车来确认收集信息 ``` ``` Please enter your first initial and last name [linuxprobe.com]:此处敲击回车来确认主机编号 Please enter the case number that you are generating this report for:此处敲击回车来确认主机编号 ``` ``` Running plugins. Please wait ... Running 70/70: yum... Creating compressed archive... Your sosreport has been generated and saved in: /var/tmp/sosreport-linuxprobe.com-20170905230631.tar.xz The checksum is: 79436cdf791327040efde48c452c6322 Please send this file to your support representative.

pwd 查看当前工作目录

1 2
pwd /etc

cd 切换工作路径

 1  2  3  4  5  6  7  8  9 10 11
cd /etc #进入上级目录 cd .. #切换到家目录 cd ~ cd ~username #返回上一次目录 cd -

ls 显示目录文件

 1  2  3  4  5  6  7  8  9 10
#-a 显示隐藏文件,-l 显示文件属性 ls -al total 60 dr-xr-x---. 14 root root 4096 May 4 07:56 . drwxr-xr-x. 17 root root 4096 May 4 15:55 .. -rw-------. 1 root root 1213 May 4 15:44 anaconda-ks.cfg #显示目录属性 ls -ld /etc drwxr-xr-x. 132 root root 8192 Jul 10 10:48 /etc

cat 查看小文件

1 2 3 4
#-n 显示行号 cat -n initial-setup-ks.cfg 1 #version=RHEL7 2 # X Window System configuration information

more 查看大文件

百分比显示进度,空格、回车翻页

1 2 3 4
more initial-setup-ks.cfg #version=RHEL7 # X Window System configuration information --More--(43%)

head 查看文件头

1 2 3 4
#查看前20行 head -n 20 initial-setup-ks.cfg #version=RHEL7 # X Window System configuration information

tail 查看文件尾

1 2 3 4 5 6 7 8
#查看最后20行 tail -n 20 initial-setup-ks.cfg #持续刷新 tail -f /var/log/messages May 4 07:56:38 localhost gnome-session: Window manager warning: Log level 16: tail -1000f /var/log/messages

tr 替换

1 2 3
cat anaconda-ks.cfg | tr [a-z] [A-Z] #VERSION=RHEL7 # SYSTEM AUTHORIZATION INFORMATION

wc 统计行数、字数

wc的参数以及作用

参数作用
-l只显示行数
-w只显示单词数
-c只显示字节数

1 2
wc -l /etc/passwd 38

stat 查看文件修改信息

 1  2  3  4  5  6  7  8  9 10
stat anaconda-ks.cfg File: ‘anaconda-ks.cfg’ Size: 1213 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 68912908 Links: 1 Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) Context: system_u:object_r:admin_home_t:s0 Access: 2017-07-14 01:46:18.721255659 -0400 Modify: 2017-05-04 15:44:36.916027026 -0400 Change: 2017-05-04 15:44:36.916027026 -0400 Birth: -

cut 提取

1 2 3 4 5 6 7
head -n 2 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin cut -d: -f1 /etc/passwd root bin

diff 比较

 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
cat diff_A.txt Welcome to linuxprobe.com Red Hat certified Free Linux Lessons Professional guidance Linux Course cat diff_B.txt Welcome tooo linuxprobe.com Red Hat certified Free Linux LeSSonS ////////.....//////// Professional guidance Linux Course #比较文件是否相同 diff --brief diff_A.txt diff_B.txt Files diff_A.txt and diff_B.txt differ #-c 比较内容 diff -c diff_A.txt diff_B.txt *** diff_A.txt 2017-08-30 18:07:45.230864626 +0800 --- diff_B.txt 2017-08-30 18:08:52.203860389 +0800 *************** *** 1,5 **** ! Welcome to linuxprobe.com Red Hat certified ! Free Linux Lessons Professional guidance Linux Course --- 1,7 ---- ! Welcome tooo linuxprobe.com ! Red Hat certified ! Free Linux LeSSonS ! ////////.....//////// Professional guidance Linux Course

touch 创建文件、设置文件时间

参数作用
-a仅修改“读取时间”(atime)
-m仅修改“修改时间”(mtime)
-d同时修改atime与mtime

 1  2  3  4  5  6  7  8  9 10 11 12
ls -l anaconda-ks.cfg -rw-------. 1 root root 1213 May 4 15:44 anaconda-ks.cfg echo "Visit the LinuxProbe.com to learn linux skills" >> anaconda-ks.cfg ls -l anaconda-ks.cfg -rw-------. 1 root root 1260 Aug 2 01:26 anaconda-ks.cfg touch -d "2017-05-04 15:44" anaconda-ks.cfg ls -l anaconda-ks.cfg -rw-------. 1 root root 1260 May 4 15:44 anaconda-ks.cfg

mkdir 创建目录

1 2 3 4 5 6 7 8
mkdir linuxprobe cd linuxprobe #-p 创建层级目录 mkdir -p a/b/c/d/e cd a cd b

cp 复制

1、如果目标文件是目录,则会把源文件复制到该目录中

2、如果目标文件也是普通文件,则会询问是否要覆盖它

3、如果目标文件不存在,则执行正常的复制操作

cp命令的参数及其作用

参数作用
-p保留原始文件的属性
-d若对象为“链接文件”,则保留该“链接文件”的属性
-r递归持续复制(用于目录)
-i若目标文件存在则询问是否覆盖
-a相当于-pdr(p、d、r为上述参数)

1 2 3 4 5
touch install.log cp install.log x.log ls install.log x.log

mv 剪切、重命名

1 2 3 4
mv x.log linux.log ls install.log linux.log

rm 删除

1 2 3 4 5 6 7 8
rm install.log rm: remove regular empty file ‘install.log’? y #-f 强制删除 rm -f linux.log #-r 删除目录 rm -rf linux.log

dd 块复制

dd命令的参数及其作用

参数作用
if输入的文件名称
of输出的文件名称
bs设置每个“块”的大小
count设置要复制“块”的个数

 1  2  3  4  5  6  7  8  9 10
dd if=/dev/zero of=560_file count=1 bs=560M 1+0 records in 1+0 records out 587202560 bytes (587 MB) copied, 27.1755 s, 21.6 MB/s #制作iso镜像 dd if=/dev/cdrom of=RHEL-server-7.0-x86_64-LinuxProbe.Com.iso 7311360+0 records in 7311360+0 records out 3743416320 bytes (3.7 GB) copied, 370.758 s, 10.1 MB/s

file 查看文件类型

1 2 3 4 5
file anaconda-ks.cfg anaconda-ks.cfg: ASCII text file /dev/sda /dev/sda: block special

tar 打包、压缩、解压

.tar、.tar.gz(gzip压缩)、.tar.bz2(bzip2压缩)

tar命令的参数及其作用

参数作用
-c创建压缩文件
-x解开压缩文件
-t查看压缩包内有哪些文件
-z用Gzip压缩或解压
-j用bzip2压缩或解压
-v显示压缩或解压的过程
-f目标文件名
-p保留原始的权限与属性
-P使用绝对路径来压缩
-C指定解压到的目录

 1  2  3  4  5  6  7  8  9 10 11
#打包压缩 tar -czvf etc.tar.gz /etc tar: Removing leading '/' from member names /etc/ /etc/fstab #解压 mkdir /root/etc tar xzvf etc.tar.gz -C /root/etc etc/ etc/fstab

grep 过滤

grep命令的参数及其作用

参数作用
-b将可执行文件(binary)当作文本文件(text)来搜索
-c仅显示找到的行数
-i忽略大小写
-n显示行号
-v反向选择——仅列出没有“关键词”的行

1 2 3
grep /sbin/nologin /etc/passwd bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin

find 查找

find命令中的参数以及作用

参数作用
-name匹配名称
-perm匹配权限(mode为完全匹配,-mode为包含即可)
-user匹配所有者
-group匹配所有组
-mtime_-n_+n匹配修改内容的时间(-n指n天以内,+n指n天以前)
-atime_-n_+n匹配访问文件的时间(-n指n天以内,+n指n天以前)
-ctime_-n_+n匹配修改文件权限的时间(-n指n天以内,+n指n天以前)
-nouser匹配无所有者的文件
-nogroup匹配无所有组的文件
-newer_f1_!f2匹配比文件f1新但比f2旧的文件
--type_b/d/c/p/l/f匹配文件类型(后面的字幕参数依次表示块设备、目录、字符设备、管道、链接文件、文本文件)
-size匹配文件的大小(+50KB为查找超过50KB的文件,而-50KB为查找小于50KB的文件)
-prune忽略某个目录
-exec_……_{}\;后面可跟用于进一步处理搜索结果的命令(下文会有演示)

 1  2  3  4  5  6  7  8  9 10 11
find /etc -name "host*" -print /etc/avahi/hosts /etc/host.conf #包含suid权限 find / -perm -4000 -print /usr/bin/fusermount /usr/bin/su #找出属于linuxprobe用户的文件,复制到/root/findresults find / -user linuxprobe -exec cp -a {} /root/findresults/ \;

浙ICP备11005866号-12