今天学习的命令都是运维工作中经常要用到的,非常实用,必须要用心学习,争取把这些命令烂熟于心,具体内容如下: 一、系统状态监测命令1、ifconfig命令用于获取网卡配置与网络状态等信息。 [root@linuxprobe Desktop]# ifconfig eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.134.128 netmask 255.255.255.0 broadcast 192.168.134.255 inet6 fe80::20c:29ff:feff:6b1f prefixlen 64 scopeid 0x20<link> ether 00:0c:29:ff:6b:1f txqueuelen 1000 (Ethernet) RX packets 32 bytes 3653 (3.5 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 58 bytes 7267 (7.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 6 bytes 560 (560.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6 bytes 560 (560.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eno16777728 : 网卡名称
inet 192.168.134.128 : IP地址 ether 00:0c:29:ff:6b:1f : MAC地址
RX packets 32 bytes 3653 (3.5 KiB) : 接收数据包的个数以及累计流量,重启系统则流量清零 2、uname命令 用于查看系统内核与系统版本等信息,一般会固定搭配-a参数 [root@linuxprobe ~]# uname -a Linux linuxprobe.com 3.10.0-123.el7.x86_64 #1 SMP Mon May 5 11:16:57 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux 3、uptime命令 用于查看系统的负载信息,系统在1分钟、5分钟、15分钟内的平均负载值(加粗信息)尽量不要长期超过1,在生产环境中不要超过5。 [root@linuxprobe ~]# uptime 11:12:49 up 23 min, 2 users, load average: 0.08, 0.04, 0.05 4、free命令 用于显示当前系统中内存的使用量信息,一般结合-h参数使用增加可读性。 [root@linuxprobe ~]# free -h total used free shared buffers cached Mem: 1.9G 910M 1.1G 9.6M 924K 295M -/+ buffers/cache: 614M 1.3G Swap: 2.0G 0B 2.0G 5、who命令 用于查看当前登录主机的用户终端信息,下面输出结果显示当前打开一个终端。 [root@linuxprobe ~]# who root :0 2020-02-17 10:49 (:0) root pts/0 2020-02-17 10:50 (:0) 6、last命令 用于查看所有本机的登录记录,要注意的是,登录记录可以被人为篡改。 [root@linuxprobe ~]# last root pts/0 :0 Mon Feb 17 10:50 still logged in root :0 :0 Mon Feb 17 10:49 still logged in (unknown :0 :0 Mon Feb 17 10:49 - 10:49 (00:00) reboot system boot 3.10.0-123.el7.x Mon Feb 17 10:49 - 11:32 (00:42) root pts/2 192.168.134.1 Sun Feb 16 19:58 - 20:05 (00:07) root pts/1 192.168.134.1 Sun Feb 16 16:51 - down (04:48) 7、history命令 用于显示历史执行过的命令,还可以使用“!数字”的方式来重复执行某一次的命令。 [root@linuxprobe ~]# history 1 cat /etc/redhat-release 2 reboot 3 ls 4 passwd root 5 su - linuxprobe 6 shutdown 7 shutdown -r 8、sosreport命令 用于收集系统配置与架构信息并输出诊断文档。 [root@linuxprobe ~]# 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]: //此处按下回车键来确认主机编号
Running 1/69: abrt... Your sosreport has been generated and saved in: The checksum is: fd53340877a57c34803cccbcc9d15676 Please send this file to your support representative. 二、工作目录切换命令1、pwd命令用于显示用户当前所在的工作目录。 [root@linuxprobe ~]# pwd
/root
2、cd命令 用于切换工作路径。“cd -”命令返回上一次所处的目录,“cd ..”命令返回上级目录,“cd ~”命令切换至当前用户的家目录。 [root@linuxprobe ~]# cd /etc
[root@linuxprobe etc]#
3、ls命令 用于显示目录中的文件信息,-a参数可以显示隐藏文件,-l参数可以显示文件的属性、大小等详细信息。 [root@linuxprobe ~]# ls -al total 64 dr-xr-x---. 14 root root 4096 Feb 17 16:28 . drwxr-xr-x. 17 root root 4096 Feb 17 16:28 .. -rw-------. 1 root root 1032 Feb 15 20:24 anaconda-ks.cfg -rw-------. 1 root root 3299 Feb 17 16:28 .bash_history -rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout -rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile -rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc drwx------. 9 root root 4096 Feb 15 19:46 .cache drwx------. 15 root root 4096 Feb 15 19:46 .config -rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc drwx------. 3 root root 24 Feb 15 12:25 .dbus drwxr-xr-x. 2 root root 6 Feb 15 19:41 Desktop drwxr-xr-x. 2 root root 6 Feb 15 19:41 Documents drwxr-xr-x. 2 root root 6 Feb 15 19:41 Downloads -rw-------. 1 root root 16 Feb 15 19:41 .esd_auth -rw-------. 1 root root 1240 Feb 17 16:28 .ICEauthority -rw-r--r--. 1 root root 1083 Feb 15 12:26 initial-setup-ks.cfg drwxr-xr-x. 3 root root 18 Feb 15 19:41 .local drwxr-xr-x. 2 root root 6 Feb 15 19:41 Music drwxr-xr-x. 2 root root 6 Feb 15 19:41 Pictures drwxr-xr-x. 2 root root 6 Feb 15 19:41 Public -rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc drwxr-xr-x. 2 root root 6 Feb 15 19:41 Templates drwxr-xr-x. 2 root root 6 Feb 15 19:41 Videos -rw-------. 1 root root 1150 Feb 15 20:11 .viminfo -rw-------. 1 root root 120 Feb 16 19:58 .Xauthority 三、文本文件编辑命令1、cat命令用于查看纯文本文件(内容较少的),-n参数可以显示行号。 [root@linuxprobe ~]# cat -n initial-setup-ks.cfg 1 #version=RHEL7 2 # X Window System configuration information 3 xconfig --startxonboot 4 5 # License agreement 6 eula --agreed 7 # System authorization information 8 auth --enableshadow --passalgo=sha512 9 # Use CDROM installation media 10 cdrom 11 # Run the Setup Agent on first boot 12 firstboot --enable 2、more命令 用于查看纯文本文件(内容较多的),可以使用空格健或回车健向下翻页。 [root@linuxprobe ~]# more initial-setup-ks.cfg #version=RHEL7 # X Window System configuration information xconfig --startxonboot # License agreement eula --agreed # System authorization information auth --enableshadow --passalgo=sha512 # Use CDROM installation media cdrom 3、head命令 用于查看纯文本文档的前N行,一般配合-n参数使用,下面命令表示查看文档前10行。 [root@linuxprobe ~]# head -n 10 initial-setup-ks.cfg #version=RHEL7 # X Window System configuration information xconfig --startxonboot # License agreement eula --agreed # System authorization information auth --enableshadow --passalgo=sha512 # Use CDROM installation media cdrom 4、tail命令 用于查看纯文本文档的后N行或持续刷新内容,前者的命令形式为“tail -n 20 文件名”,后者为“tail -f 文件名”。 [root@linuxprobe ~]# tail -f /var/log/messages Feb 17 17:40:36 linuxprobe systemd: Starting Hostname Service... Feb 17 17:40:36 linuxprobe dbus-daemon: dbus[1042]: [system] Successfully activated service 'org.freedesktop.hostname1' Feb 17 17:40:36 linuxprobe dbus[1042]: [system] Successfully activated service 'org.freedesktop.hostname1' Feb 17 17:40:36 linuxprobe systemd: Started Hostname Service. Feb 17 17:40:37 linuxprobe chronyd[1013]: Selected source 162.159.200.123 Feb 17 17:40:37 linuxprobe kernel: TCP: lp registered Feb 17 17:40:51 linuxprobe systemd-logind: Removed session c1. Feb 17 17:41:07 linuxprobe systemd: Starting Session 2 of user root. Feb 17 17:41:07 linuxprobe systemd: Started Session 2 of user root. Feb 17 17:41:07 linuxprobe systemd-logind: New session 2 of user root. Feb 17 17:41:25 linuxprobe chronyd[1013]: Selected source 202.112.29.82 5、tr命令 用于替换文本中的字符,例如把文本中的英文全部替换为大写。 [root@linuxprobe ~]# cat anaconda-ks.cfg | tr [a-z] [A-Z] #VERSION=RHEL7 # SYSTEM AUTHORIZATION INFORMATION AUTH --ENABLESHADOW --PASSALGO=SHA512 # USE CDROM INSTALLATION MEDIA CDROM # RUN THE SETUP AGENT ON FIRST BOOT FIRSTBOOT --ENABLE IGNOREDISK --ONLY-USE=SDA # KEYBOARD LAYOUTS KEYBOARD --VCKEYMAP=US --XLAYOUTS='US' # SYSTEM LANGUAGE LANG EN_US.UTF-8 6、wc命令 用于统计指定文本的行数、字数、字节数,-l参数显示文本行数。 [root@linuxprobe ~]# wc -l /etc/passwd 38 /etc/passwd 7、stat命令 用于查看文件的具体存储信息和时间等信息。 [root@linuxprobe ~]# stat anaconda-ks.cfg File: 'anaconda-ks.cfg’ Size: 1032 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 68860812 Links: 1 Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root) Context: system_u:object_r:admin_home_t:s0 Access: 2020-02-18 14:55:53.095941142 +0800 //最后一次查看文件内容的时间 Modify: 2020-02-15 20:24:56.710988547 +0800 //最后一次修改文件内容的时间 Change: 2020-02-15 20:24:56.710988547 +0800 //最后一次变更文件权限或属性的时间 Birth: - 8、cut命令 用于按“列”提取文本字符,-d参数设置间隔符号,-f参数设置提取第几列字符。下面命令表示以“:”作为分隔符,提取第一列数据。 [root@linuxprobe ~]# cut -d : -f 1 /etc/passwd root bin daemon adm lp sync shutdown halt mail operator games ftp 9、diff命令 用于比较多个文本文件的差异,--brief参数仅返回比较结果,-c参数详细比较多个文件之间的差异之处。 [root@linuxprobe ~]# diff -c anaconda-ks.cfg initial-setup-ks.cfg *** anaconda-ks.cfg 2020-02-15 20:24:56.710988547 +0800 --- initial-setup-ks.cfg 2020-02-15 12:26:03.533009017 +0800 *************** *** 1,26 **** #version=RHEL7 # System authorization information auth --enableshadow --passalgo=sha512 - # Use CDROM installation media cdrom # Run the Setup Agent on first boot firstboot --enable - ignoredisk --only-use=sda # Keyboard layouts keyboard --vckeymap=us --xlayouts='us' # System language lang en_US.UTF-8 四、文件目录管理命令1、touch命令用于创建空白文件或设置文件的时间,可以设置文件的查看时间(atime)、权限或属性的变更时间(ctime)以及文件的修改时间(mtime),分别对应stat命令里的三个时间。-d参数表示同时修改atime和mtime。 [root@linuxprobe ~]# ls -l anaconda-ks.cfg -rw-------. 1 root root 1032 Feb 15 20:24 anaconda-ks.cfg [root@linuxprobe ~]# [root@linuxprobe ~]# touch -d "2019-2-18 15:20" anaconda-ks.cfg //修改anaconda-ks.cfg文件的atime和mtime [root@linuxprobe ~]# ls -l anaconda-ks.cfg 2、mkdir命令 用于创建空白的目录,结合-p参数来创建具有嵌套层关系的文件目录。 [root@linuxprobe ~]# mkdir -p a/b/c/d [root@linuxprobe ~]# [root@linuxprobe ~]# cd a/ //进入a目录 [root@linuxprobe a]# cd b/ //进入b目录 [root@linuxprobe b]# 3、cp命令 用于复制文件和目录,格式为“cp 参数 源文件 目标文件”,复制操作分为4种情况:
[root@linuxprobe ~]# cp anaconda-ks.cfg anaconda-ks.cfg_bak //备份anaconda-ks.cfg文件
[root@linuxprobe ~]# ls 4、mv命令 用于剪切文件或将文件重命名,在同一目录下对一个文件进行剪切操作,就是对其进行重命名。 [root@linuxprobe ~]# mv anaconda-ks.cfg_bak anaconda-ks.cfg_cpoy //将anaconda-ks.cfg_bak文件重命名为anaconda-ks.cfg_copy [root@linuxprobe ~]# ls a anaconda-ks.cfg_cpoy Documents initial-setup-ks.cfg Pictures Templates anaconda-ks.cfg Desktop Downloads Music Public Videos [root@linuxprobe ~]# 5、rm命令 用于删除文件或目录,-f参数表示强制删除,不提示确认信息,删除目录需要-r参数。 [root@linuxprobe ~]# rm -f anaconda-ks.cfg_cpoy //强制删除anaconda-ks.cfg_copy文件 [root@linuxprobe ~]# [root@linuxprobe ~]# ls a anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos [root@linuxprobe ~]# 6、dd命令 用来按照指定大小和个数的数据块来复制文件或转换文件,相关参数及作用如下所示:
7、file命令 用于查看文件的类型。 [root@linuxprobe ~]# file anaconda-ks.cfg anaconda-ks.cfg: ASCII text //文本文件 [root@linuxprobe ~]# [root@linuxprobe ~]# file Downloads Downloads: directory //目录 [root@linuxprobe ~]# 五、打包压缩与搜索命令1、tar命令用于对文件进行打包压缩和解压,相关参数及其作用如下:
[root@linuxprobe ~]# ls 接下来将压缩文件解压到 /root/test 目录中,注意:无论压缩还是解压,-f参数都一定要放在参数的最后面。 [root@linuxprobe ~]# mkdir test //创建test目录 [root@linuxprobe ~]# [root@linuxprobe ~]# tar -xzvf test.tar.gz -C /root/test //解压 root/ root/.bash_logout root/.bash_profile root/.bashrc root/.cshrc root/.tcshrc root/anaconda-ks.cfg 2、grep命令 用于在文本中执行关键词搜索,并显示匹配结果,格式为“grep 参数 关键词 文件名”,相关参数及其作用如下所示:
3、find命令 用来按照指定的条件来查找文件,格式为“find [查找路径] 寻找条件 操作”。相关参数及其作用如下所示:
find命令进阶实验:在整个文件系统中找出所有归属于linuxprobe用户的文件并复制到/root/findresults目录。 [root@linuxprobe ~]# find / -user linuxprobe -exec cp -a {} /root/findresults/ \; //其中{}代表find命令搜索出的每一个文件,即假设find命令第一次搜索出了test.txt文件,则执行cp -a test.txt /root/findresults/ find: '/proc/10213/task/10213/fd/6’: No such file or directory find: '/proc/10213/task/10213/fdinfo/6’: No such file or directory find: '/proc/10213/fd/6’: No such file or directory find: '/proc/10213/fdinfo/6’: No such file or directory cp: cannot overwrite non-directory '/root/findresults/linuxprobe’ with directory '/home/linuxprobe’ [root@linuxprobe ~]# [root@linuxprobe findresults]# ls -l //查看命令执行结果 total 3868 srwxrwxrwx. 1 linuxprobe linuxprobe 0 Feb 15 12:37 1960 srwxrwxrwx. 1 linuxprobe linuxprobe 0 Feb 15 12:48 1967 srwxrwxrwx. 1 linuxprobe linuxprobe 0 Feb 15 12:45 1975 srwxrwxrwx. 1 linuxprobe linuxprobe 0 Feb 15 19:28 1979 srwxrwxrwx. 1 linuxprobe linuxprobe 0 Feb 15 15:22 1981 srwxrwxrwx. 1 linuxprobe linuxprobe 0 Feb 15 12:29 2031 -rw-r--r--. 1 linuxprobe linuxprobe 696 Feb 15 12:29 6c80566e9d01473b8952f753857b506f-card-database.tdb -rw-r--r--. 1 linuxprobe linuxprobe 10 Feb 15 19:28 6c80566e9d01473b8952f753857b506f-default-sink
|
|