前言△
*.tar:普通打包
*.tar.gz:已压缩
配置IP地址时,需要将DHCP服务关掉(优先级可能会影响ip地址的配置)
=============================
练习△
一、系统信息类命令
(1)利用date命令显示系统当前时间,并修改系统的当前时间。
[root@localhost ~]# date
Mon Nov 30 11:48:56 CST 2020
[root@localhost ~]# date -s '2020-1-1 19:00:20'
Wed Jan 1 19:00:20 CST 2020
[root@localhost ~]# date
Wed Jan 1 19:00:25 CST 2020
(2)显示当前登录到系统的用户状态。
way1:用who命令查看(登录)用户名、所启动的进程
[root@localhost ~]# who
root tty1 Nov 30 10:18
root pts/0 Nov 30 10:18 (192.168.232.1)
root pts/1 Nov 30 11:46 (192.168.232.1)
输出为:用户名、tty号、时间日期、主机地址
way2:whoami
[root@localhost ~]# whoami
root
way3:w
[root@localhost ~]# w
14:16:47 up 3:17, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 Mon09 4:07 0.01s 0.01s -bash
root pts/0 192.168.232.1 14:13 7.00s 0.02s 0.00s w
root pts/1 192.168.232.1 Mon11 2days 0.01s 0.01s -bash
way4:last [用户名]
用本命令可以查看到:
- 用户名称
- tty设备号
- 历史登录时间日期
- 登出时间日期
- 总工作时间
[root@localhost ~]# last root
root pts/0 192.168.232.1 Wed Dec 2 14:13 still logged in
root pts/1 192.168.232.1 Mon Nov 30 11:57 still logged in
root pts/0 192.168.232.1 Mon Nov 30 10:06 - 14:07 (04:00)
root tty1 Mon Nov 30 09:47 still logged in
root pts/1 192.168.232.1 Mon Nov 23 06:53 - crash (7+02:52)
root pts/0 192.168.232.1 Sun Nov 22 21:16 - 08:59 (11:42)
root tty1 Sun Nov 22 21:16 - crash (7+12:29)
……
(3)利用free命令显示内存的使用情况。
参数解释:
不指定参数时,默认以KB为单位显示
-b,-k,-m,-g:以B、KB、MB、GB为单位显示
其余可参考:
Linux 使用 free 命令查看内存使用情况
[root@localhost ~]# free
total used free shared buff/cache available
Mem: 995684 176284 703392 7836 116008 686092
Swap: 1998844 0 1998844
(4)利用df命令显示系统的硬盘分区及使用状况。
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 487140 0 487140 0% /dev
tmpfs 497840 0 497840 0% /dev/shm
tmpfs 497840 7836 490004 2% /run
tmpfs 497840 0 497840 0% /sys/fs/cgroup
/dev/sda2 13882368 1368892 12513476 10% /
/dev/sr0 10032746 10032746 0 100% /mnt/cdrom
/dev/sda3 4184064 32992 4151072 1% /data
/dev/sda1 201380 111432 89948 56% /boot
tmpfs 99572 0 99572 0% /run/user/0
(5)显示当前目录下的各级子目录的硬盘占用情况。
du
命令:用于显示目录或文件的大小(disk usage)
[root@localhost ~]# du -h --max-depth=1
0 ./f2
8.0K ./ito
72K ./yum.repo
0 ./task
128K .
二、进程管理类命令
(1)使用ps命令查看和控制进程:
①显示本用户的进程。ps
[root@localhost ~]# ps
PID TTY TIME CMD
3630 pts/0 00:00:00 bash
3639 pts/0 00:00:00 ps
②显示所有用户的进程。ps a
[root@localhost ~]# ps a
PID TTY STAT TIME COMMAND
1194 tty1 Ss+ 0:00 -bash
1705 pts/1 Ss+ 0:00 -bash
3630 pts/0 Ss 0:00 -bash
3673 pts/0 R+ 0:00 ps a
③在后台运行cat命令。
way1:后台运行 [命令] &
进程会绑定终端,终端一关,进程结束
[root@localhost ~]# cat &
[1] 1273
way2:让命令在后台持久运行 nohup [命令] &
将命令放到/etc/rc.local中,系统启动时执行里面命令,因不是终端启动所以不受影响
④查看进程cat。
way1:ps
[root@localhost ~]# ps
PID TTY TIME CMD
1271 pts/0 00:00:00 bash
1273 pts/0 00:00:00 cat
1274 pts/0 00:00:00 ps
way2:ps -aux | grep [命令名]
a:显示当前终端下的所有进程信息,包括其他用户的进程。
u:使用以用户为主的格式输出进程信息。
x:显示当前用户在所有终端下的进程。
[root@localhost ~]# ps -aux | grep cat
root 1273 0.0 0.0 4384 352 pts/0 T 15:00 0:00 cat
root 1290 0.0 0.0 9092 676 pts/0 S+ 15:02 0:00 grep cat
⑤杀死进程cat。
1)查找cat进程的进程id:
[root@localhost ~]# ps aux | grep cat
root 1273 0.0 0.0 4384 352 pts/0 T 15:00 0:00 cat
root 1320 0.0 0.0 9092 672 pts/0 R+ 15:13 0:00 grep cat
2)杀死进程:kill -9 [进程ID]
[root@localhost ~]# kill -9 1273
另外可参考:杀死进程的三种方法
⑥再次查看进程cat,看其是否已被杀死。
再次查看cat正在运行的进程,可以看到cat进程已被杀死
[root@localhost ~]# ps aux | grep cat
root 1324 0.0 0.0 9092 676 pts/0 R+ 15:15 0:00 grep cat
[1]+ Killed cat
(2)使用top命令查看和控制进程:
①用top命令动态显示当前的进程。
[root@localhost ~]# top
top - 15:22:45 up 24 min, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 102 total, 2 running, 100 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 995684 total, 714072 free, 169568 used, 112044 buff/cache
KiB Swap: 1998844 total, 1998844 free, 0 used. 694792 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 128004 6588 4144 S 0.0 0.7 0:01.50 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
5 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kworker/u256:0
6 root 20 0 0 0 0 S 0.0 0.0 0:00.10 ksoftirqd/0
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 0:00.52 rcu_sched
10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-drain
11 root rt 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0
13 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
14 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 netns
15 root 20 0 0 0 0 S 0.0 0.0 0:00.00 khungtaskd
16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 writeback
17 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kintegrityd
18 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
19 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 bioset
[1]+ Stopped top
②只显示用户user01的进程(利用u键)。
[root@localhost ~]# top -u user01
③利用k键,杀死指定进程号的进程。
(3)挂起和恢复进程:
①执行命令cat。
[root@localhost ~]# cat
②按Ctrl+z组合键,挂起进程cat。
[root@localhost ~]# cat
^Z
[4]+ Stopped cat
③输入jobs命令,查看作业。
[root@localhost ~]# jobs
[1] Stopped top
[2] Stopped top -u root
[3]- Stopped cat
④输入bg,把cat切换到后台执行。
[root@localhost ~]# bg 4
[4]+ cat &
⑤输入fg,把cat切换到前台执行。
[root@localhost ~]# fg 4
cat
⑥按Ctrl+c组合键,结束进程cat。
【总结】
Ctrl+Z:挂起进程
Ctrl+C:结束进程
(4)find命令的使用:
①在/var/lib目录下查找其所有者是games用户的所有文件。
[root@localhost ~]# find /var/lib -user games
②在/var目录下查找其所有者是root用户的所有文件。
[root@localhost ~]# find /var -user root
③查找其所有者不是root、bin和student用户的所有文件并用长格式显示。
[root@localhost ~]# find / ! -user root -and ! -user bin -and ! -user yoh -exec ls -l {} \; 2> /dev/null
-exec
:执行
【问题解决】
途中报错:find: missing argument to `-exec’
出现这种错误时,先检查命令中的空格有没有少打(上面命令中的空格是不可或缺的)
④查找/usr/bin目录下所有大小超过1 000 000 B的文件并用长格式显示。
[root@localhost ~]# find /usr/bin -size +1000000c -exec ls -l {} \;
⑤查找/tmp目录下属于student的所有普通文件,这些文件的修改时间为120 min以前,查询结果用长格式显示。
[root@localhost ~]# find /tmp -user student -and -mmin +120 -and -type f -exec ls {} \; 2> /dev/null
linux中的3种时间time:atime、mtime、ctime
Linux - find命令搭配atime/ctime/mtime的写法
linux中find命令mtime参数为+、-和不加符号的意思
⑥对于查到的上述文件,用-ok选项删除。
(因为student下没有文件,图方便我直接拿root来测试)
-ok
:删除前会逐个询问是否需要删除
-exec
:直接执行删除操作
[root@localhost ~]# find /tmp -user root -and -mmin +120 -and -type f -ok rm {} \;
< rm ... /tmp/file1 > ? no
< rm ... /tmp/dir1/file1.bak > ? no
< rm ... /tmp/dir2/file2 > ? no
< rm ... /tmp/file2 > ? no
< rm ... /tmp/file3.bak > ? no
三、rpm软件包的管理
①查询系统是否安装了软件包squid。
②如果没有安装,则挂载Linux安装光盘,安装squid软件包。
③卸载刚刚安装的软件包。
④软件包的升级。
软件包的更新。
四、tar命令的使用
系统上的主硬盘在使用的时候有可怕的噪声,但是它上面有有价值的数据。该系统在两年半以前备份过,现在决定手动备份少数几个最紧要的文件。/tmp目录可以存储不同磁盘分区的数据,可以将文件临时备份到这个目录。
①在/home目录里,用find命令定位文件所有者是teacher的文件。然后将其压缩。
[root@localhost ~]# find /home -user teacher -exec tar czvf /tmp/backup.tar {} \;
tar: Removing leading `/' from member names
/home/teacher/
/home/teacher/.bash_logout
/home/teacher/.bash_profile
/home/teacher/.bashrc
/home/teacher/.bash_history
tar: Removing leading `/' from member names
/home/teacher/.bash_logout
tar: Removing leading `/' from member names
/home/teacher/.bash_profile
tar: Removing leading `/' from member names
/home/teacher/.bashrc
tar: Removing leading `/' from member names
/home/teacher/.bash_history
②保存/etc目录下的文件到/tmp目录下。
[root@localhost ~]# tar cvf /tmp/confbackup.tar /etc/
③列出两个文件的大小。
[root@localhost ~]# du -h /tmp/backup.tar
4.0K /tmp/backup.tar
[root@localhost ~]# du -h /tmp/confbackup.tar
29M /tmp/confbackup.tar
④使用gzip压缩文档。
压缩:
way1:
[root@localhost ~]# gzip /tmp/backup.tar
way2:
[root@localhost ~]# gzip -c /tmp/backup.tar > /tmp/backup.tar.gz
解压:
[root@localhost ~]# gzip -d /tmp/backup.tar.gz