Linux-打包压缩-date-rpm-yum-磁盘管理(分区fdisk/gdisk)-systemd-修改root密码

打包压缩


压缩包工具

	.zip		#zip压缩工具	
	.bz2		#bzip2压缩工具,只能压缩文件,会删除源文件
	.gz			#gzip压缩工具,只能压缩文件,会删除源文件
	tar.gz		#gzip压缩,使用tar打包
	tar.bz2		#使用bzip2压缩,使用tar打包
	

zip


zip		#打包压缩
选项:
	-r		#递归压缩
	-q		#静默输出
	-T		#检查压缩包是完整
	
unzip	#解压zip格式的压缩包
选项:
	-d		#指定解压的目录
	-l		#查看压缩包的内容
	-t		#检查压缩包里面的内容是否完整
	-q		#静默输出
	
#下载安装
[root@qls ~]# yum  install -y  zip  unzip

[root@qls ~]# cp  /etc/services   ./
[root@qls ~]# ll
total 656
-rw-r--r--. 1 root root 670293 Aug 20 08:46 services
[root@qls ~]# zip  ser.zip   services 
  adding: services (deflated 80%)
[root@qls ~]# ll
total 792
-rw-r--r--. 1 root root 670293 Aug 20 08:46 services
-rw-r--r--. 1 root root 136227 Aug 20 08:46 ser.zip
[root@qls ~]# ll -h
total 792K
-rw-r--r--. 1 root root 655K Aug 20 08:46 services
-rw-r--r--. 1 root root 134K Aug 20 08:46 ser.zip
[root@qls ~]# zip -T ser
services  ser.zip   
[root@qls ~]# zip -T ser.zip 
test of ser.zip OK
[root@qls ~]# unzip  services 
Archive:  services
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of services or
        services.zip, and cannot find services.ZIP, period.
[root@qls ~]# ll
total 792
-rw-r--r--. 1 root root 670293 Aug 20 08:46 services
-rw-r--r--. 1 root root 136227 Aug 20 08:46 ser.zip
[root@qls ~]# unzip ser.zip   -d  /opt/
Archive:  ser.zip
  inflating: /opt/services           
[root@qls ~]# ll /opt/
total 656
-rw-r--r--. 1 root setgid      0 Aug 19 01:03 file9
-rw-r--r--. 1 root setgid 670293 Aug 20 08:46 services


gz格式,gzip


gzip	#只能压缩文件,会删除源文件
选项:
	-d		#解压
	-r		#压缩目录下的所有文件
	
zcat	#查看gzip格式压缩包

[root@qls ~]# gzip  services
[root@qls ~]# zcat  services.gz
[root@qls ~]# gzip  -d  services.gz

bzip2


#只能压缩文件,会删除源文件

[root@qls ~]# bzip2  services		#压缩
[root@qls ~]# bzcat  services.bz2	#查看压缩包的内容
[root@qls ~]# bzip2 -d services.bz2	#解压

tar


tar		#打包工具
选项
	z	#使用gzip格式压缩
	j	#使用bzip2格式压缩
	J	#使用xz格式压缩
	c	#创建压缩包
	f	#指定压缩包名
	v	#显示打包过程
	zcf
	t	#查看压缩包里面的内容
	tf
	x	#解压,根据压缩自动解压
	xf
	-C	#指定解压路径
	P	#使用绝对路径打包
	h	#打包软连接
	X	#指定排除列表
	--exclude	#排除
	--exclude-from=xxx  #排除列表
	
	
[root@qls ~]# tar  zcvf  services.tar.gz  services 
services

[root@qls ~]# tar  zcf  etc.tar.gz  /etc

[root@qls ~]# tar  xf  etc.tar.gz

[root@qls ~]# tar xf  etc.tar.gz   -C  /opt/

#解除压缩时,删根的操作
[root@qls ~]# cd  /  
[root@qls /]# tar  zcf  root/etc.tar.gz  etc/

[root@qls ~]# tar  zcPf  etc1.tar.gz  /etc

[root@qls ~]# tar  jcf  etc.bz2  /etc			#使用bz2格式打包
tar: Removing leading `/' from member names

[root@qls ~]# tar  Jcf  etc.xz   /etc

[root@qls ~]# tar zchf  rc1.tzr.gz  /etc/rc.local 

[root@qls ~]# tar  czf  etc.tar.gz  --exclude=/etc/hosts  /etc
tar: Removing leading `/' from member names
[root@qls ~]# ll
total 9980
-rw-r--r--. 1 root root 10216073 Aug 20 09:52 etc.tar.gz
[root@qls ~]# tar  tf etc.tar.gz  |  grep  hosts
etc/selinux/targeted/active/modules/100/denyhosts/
etc/selinux/targeted/active/modules/100/denyhosts/cil
etc/selinux/targeted/active/modules/100/denyhosts/hll
etc/selinux/targeted/active/modules/100/denyhosts/lang_ext
etc/hosts.allow
etc/hosts.deny

[root@qls ~]# tar  czf  etc.tar.gz  --exclude=/etc/{hosts,hostname}  /etc
tar: Removing leading `/' from member names
[root@qls ~]# ll
total 9980
-rw-r--r--. 1 root root 10215980 Aug 20 09:54 etc.tar.gz
[root@qls ~]# tar  tf etc.tar.gz  |  grep  -E 'hosts|hostname'
etc/selinux/targeted/active/modules/100/denyhosts/
etc/selinux/targeted/active/modules/100/denyhosts/cil
etc/selinux/targeted/active/modules/100/denyhosts/hll
etc/selinux/targeted/active/modules/100/denyhosts/lang_ext
etc/selinux/targeted/active/modules/100/hostname/
etc/selinux/targeted/active/modules/100/hostname/cil
etc/selinux/targeted/active/modules/100/hostname/hll
etc/selinux/targeted/active/modules/100/hostname/lang_ext
etc/hosts.allow
etc/hosts.deny
etc/dbus-1/system.d/org.freedesktop.hostname1.conf

[root@qls ~]# tar  czf  etc.tar.gz  --exclude=/etc/hosts --exclude=/etc/passwd  /etc
tar: Removing leading `/' from member names
[root@qls ~]# tar  tf etc.tar.gz  |  grep  -E 'hosts|passwd'
etc/selinux/targeted/active/modules/100/denyhosts/
etc/selinux/targeted/active/modules/100/denyhosts/cil
etc/selinux/targeted/active/modules/100/denyhosts/hll
etc/selinux/targeted/active/modules/100/denyhosts/lang_ext
etc/hosts.allow
etc/hosts.deny
etc/passwd-
etc/security/opasswd
etc/pam.d/passwd

[root@qls ~]# cat paichu.list
/etc/hosts
/etc/hostname
/etc/passwd
/etc/fstab
/etc/services
[root@qls ~]# ll
total 9984
-rw-r--r--. 1 root root 10215966 Aug 20 09:55 etc.tar.gz
-rw-r--r--. 1 root root       62 Aug 20 09:56 paichu.list
[root@qls ~]# tar  czf  etc1.tar.gz  -X paichu.list  /etc
tar: Removing leading `/' from member names
[root@qls ~]# ll
total 19828
-rw-r--r--. 1 root root 10079106 Aug 20 09:58 etc1.tar.gz
-rw-r--r--. 1 root root 10215966 Aug 20 09:55 etc.tar.gz
-rw-r--r--. 1 root root       62 Aug 20 09:56 paichu.list
[root@qls ~]# tar tf  etc1.tar.gz   |grep -E  'hosts|hostname|passwd|fstab|services'
etc/selinux/targeted/active/modules/100/denyhosts/
etc/selinux/targeted/active/modules/100/denyhosts/cil
etc/selinux/targeted/active/modules/100/denyhosts/hll
etc/selinux/targeted/active/modules/100/denyhosts/lang_ext
etc/selinux/targeted/active/modules/100/hostname/
etc/selinux/targeted/active/modules/100/hostname/cil
etc/selinux/targeted/active/modules/100/hostname/hll
etc/selinux/targeted/active/modules/100/hostname/lang_ext
etc/selinux/targeted/active/modules/100/updfstab/
etc/selinux/targeted/active/modules/100/updfstab/cil
etc/selinux/targeted/active/modules/100/updfstab/hll
etc/selinux/targeted/active/modules/100/updfstab/lang_ext
etc/hosts.allow
etc/hosts.deny
etc/passwd-
etc/dbus-1/system.d/org.freedesktop.hostname1.conf
etc/security/opasswd
etc/pam.d/passwd
etc/firewalld/services/

[root@qls ~]# tar  czf  etc2.tar.gz   --exclude-from=paichu.list  /etc
tar: Removing leading `/' from member names
[root@qls ~]# ll
total 29672
-rw-r--r--. 1 root root 10079106 Aug 20 09:58 etc1.tar.gz
-rw-r--r--. 1 root root 10079106 Aug 20 10:00 etc2.tar.gz
-rw-r--r--. 1 root root 10215966 Aug 20 09:55 etc.tar.gz
-rw-r--r--. 1 root root       62 Aug 20 09:56 paichu.list

tar与find的结合


[root@qls ~]# find /var/log/  -name '*.log'  | xargs  tar  log.tar.gz 

#错误的打包方式,不可取
[root@qls ~]# find /var/log/  -name '*.log'  -exec  tar czf  log1.tar.gz  {} \;

find跟du结合


[root@qls ~]# find  /etc  -type d  -maxdepth  1   | sed  -r  's#(.*)# du -sh \1#g'  |bash

[root@qls ~]# ls  -d  /* |xargs  du -sh

练习题


1.列出linux常用打包工具并写相应的压缩、解压缩参数

2.如何用gzip命令对文件进行压缩、解压缩,给出实践步骤。

	gzip  filename   #压缩
	gzip -d   filename.gz  #解压
	zcat	filename.tar	#查看压缩包内容

3.如何用zip命令对文件以及目录进行压缩、解压缩,给出实践步骤。

	zip	 filename.zip   filename   #压缩
	zip -r dir.zip  dir  #压缩目录
	unzip	filename.zip	#解压
	-d	#解压到指定的路径
	
4.创建一个自己名字的文件至/opt目录,给出实践步骤。
     
     touch   /opt/frank
     
5.打包opt/整个目录,并命名为opt_2019_08_19.tar.gz,给出实践步骤。

	tar  czf  opt_2019_08_19.tar.gz  /opt
	
	
[root@qls ~]# tar  czf  opt_$(date  +%Y_%m_%d).tar.gz  /opt
tar: Removing leading `/' from member names
[root@qls ~]# ll
total 40356
-rw-r--r--. 1 root root 10079106 Aug 20 09:58 etc1.tar.gz
-rw-r--r--. 1 root root 10079106 Aug 20 10:00 etc2.tar.gz
-rw-r--r--. 1 root root 10215966 Aug 20 09:55 etc.tar.gz
-rw-r--r--. 1 root root      353 Aug 20 10:05 log1.tar.gz
-rw-r--r--. 1 root root   288985 Aug 20 10:07 log2.tar.gz
-rw-r--r--. 1 root root   288985 Aug 20 10:03 log.tar.gz
-rw-r--r--. 1 root root 10352576 Aug 20 10:37 opt_2019_08_20.tar.gz

6.查看打包好的opt_2019_08_19.tar.gz里的文件,给出实践步骤。

tar  tf   opt_2019_08_19.tar.gz

7.将打包好的opt_2019_08_19.tar.gz内容指定解压至/tmp目录,并给出实践步骤。

tar  xf  opt_2019_08_19.tar.gz  -C  /tmp

8.打包以当前主机名+ip的命名方式的压缩包: 比如: www.oldboyedu.com_2019-08-19_10.0.0.99.tar.gz ,压缩/opt目录即可

主机名
时间
ip

[root@qls ~]# tar czf  opt_$(hostname)_$(date  +%F)_$(ifconfig  eth0 |awk 'NR==2{print $2}').tar.gz   /opt
tar: Removing leading `/' from member names
[root@qls ~]# ll
total 50468
-rw-r--r--. 1 root root 10079106 Aug 20 09:58 etc1.tar.gz
-rw-r--r--. 1 root root 10079106 Aug 20 10:00 etc2.tar.gz
-rw-r--r--. 1 root root 10215966 Aug 20 09:55 etc.tar.gz
-rw-r--r--. 1 root root      353 Aug 20 10:05 log1.tar.gz
-rw-r--r--. 1 root root   288985 Aug 20 10:07 log2.tar.gz
-rw-r--r--. 1 root root   288985 Aug 20 10:03 log.tar.gz
-rw-r--r--. 1 root root 10352576 Aug 20 10:37 opt_2019_08_20.tar.gz
-rw-r--r--. 1 root root 10352576 Aug 20 10:41 opt_qls_2019-08-20_10.0.0.100.tar.gz
-rw-r--r--. 1 root root       62 Aug 20 09:56 paichu.list

date


[root@qls ~]# date
Tue Aug 20 10:42:19 CST 2019
[root@qls ~]# date +%F
2019-08-20
[root@qls ~]# date +%Y-%m-%d
2019-08-20
[root@qls ~]# date  +%T
10:43:01
[root@qls ~]# date  +%H:%M:%S
10:43:27
[root@qls ~]# date +%w
2
[root@qls ~]# date +%W
33
[root@qls ~]# date +%d
20
[root@qls ~]# date +%s
1566269040
[root@qls ~]# date +%y-%m-%d
19-08-20
[root@qls ~]# date -d  '-1day'
Mon Aug 19 10:46:06 CST 2019
[root@qls ~]# date -d  '-1day' +%F
2019-08-19
[root@qls ~]# date -d  '+1day' +%F
2019-08-21
[root@qls ~]# date -d  '+1year' +%F
2020-08-20
[root@qls ~]# date -d  '-1year' +%F
2018-08-20
[root@qls ~]# date -s  '2019/08/30'
Fri Aug 30 00:00:00 CST 2019
[root@qls ~]# date +%F
2019-08-30
[root@qls ~]# date -s  '10:48:00'
Fri Aug 30 10:48:00 CST 2019
[root@qls ~]# date +%T
10:48:04
[root@qls ~]# yum install -y  ntpdate
[root@qls ~]# ntpdate  ntp.aliyun.com
20 Aug 10:49:27 ntpdate[10174]: step time server 203.107.6.88 offset -863992.416377 sec
[root@qls ~]# date 
Tue Aug 20 10:49:34 CST 2019

rpm包


tree-1.6.0-10.el7.x86_64.rpm
trang-20091111-14.el7.noarch.rpm 

依赖性太强

#挂载镜像
[root@qls ~]# mount  /dev/cdrom   /mnt

[root@qls ~]# rpm  -ivh /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:tree-1.6.0-10.el7                ################################# [100%]

[root@qls ~]# yum install -y samba


选项
	-e		#卸载软件包
	-i		#安装
	-v		#显示安装的过程
	-h		#查看安装的进度
	-qa		#查看软件包是否安装
	-qi		#显示软件包详细信息
	-qc		#查询软件包的配置文件
	-qf		#搜索命令属于哪个软件包,本地要有这个命令
	-U		#升级
	

[root@qls ~]# rpm -qf  /usr/sbin/ifconfig
net-tools-2.0-0.24.20131004git.el7.x86_64


[root@qls ~]# rpm -ivh https://mirrors.aliyun.com/centos/7.6.1810/os/x86_64/Packages/tree-1.6.0-10.el7.x86_64.rpm
Retrieving https://mirrors.aliyun.com/centos/7.6.1810/os/x86_64/Packages/tree-1.6.0-10.el7.x86_64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:tree-1.6.0-10.el7                ################################# [100%]

安装低版本
[root@qls ~]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/7/x86_64/zabbix-agent-3.0.0-1.el7.x86_64.rpm
Retrieving https://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/7/x86_64/zabbix-agent-3.0.0-1.el7.x86_64.rpm
warning: /var/tmp/rpm-tmp.t6JBzJ: Header V4 DSA/SHA1 Signature, key ID 79ea5ed4: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:zabbix-agent-3.0.0-1.el7         ################################# [100%]

升级
[root@qls ~]# rpm -Uvh  https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm
Retrieving https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.0-2.el7.x86_64.rpm
warning: /var/tmp/rpm-tmp.0nJhRu: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:zabbix-agent-4.0.0-2.el7         ################################# [ 50%]
Cleaning up / removing...
   2:zabbix-agent-3.0.0-1.el7         ################################# [100%]

yum


-y		#免交互

#本地安装
[root@qls ~]# yum  localinstall  /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm 

install			#安装
provides		#查询命令属于哪个软件包
search			#搜寻软件包
update			#更新
check-update	#检查系统可更新的包
repolist		#查看yum源镜像仓库
list			 #查看可安装的包
list installed	 #查看系统中已经安装的包
clean			#清空缓存
		all		#清空所有
		packages	#清空软件包
info			#查看软件包的详细信息
history			#yum命令的历史事物
remove			#卸载软件
reinstall		#遇到配置被删除,可以重新安装

[root@qls ~]# head  /etc/yum.conf 
[main]
cachedir=/var/cache/yum/$basearch/$releasever	#缓存生成路径
keepcache=1		#开启缓存


源码编译:以nginx为例


预编译
make
make  install

#下载源码包
[root@qls ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz

#解压
[root@qls ~]# tar  xf nginx-1.16.1.tar.gz

[root@qls ~]# cd nginx-1.16.1

#安装依赖
[root@qls nginx-1.16.1]# yum  install  gcc-devel  gcc pcre  pcre-devel openssl  openssl-devel -y

[root@qls nginx-1.16.1]# ./configure   --prefix=/app/nginx-1.16.1  --with-http_ssl_module

checking for OS
 + Linux 3.10.0-957.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found		#报错

解决:
[root@qls nginx-1.16.1]# yum  install  gcc-devel  gcc  -y

报错
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

解决
[root@qls nginx-1.16.1]# yum install  -y pcre  pcre-devel

报错
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.

解决
[root@qls nginx-1.16.1]# yum install openssl  openssl-devel  -y 

[root@qls nginx-1.16.1]# echo  $?
0

[root@qls nginx-1.16.1]#  make

[root@qls nginx-1.16.1]# echo  $?
0

[root@qls nginx-1.16.1]# make install

[root@qls nginx-1.16.1]# echo  $?
0

[root@qls nginx-1.16.1]# ll /app/nginx-1.16.1/
total 4
drwxr-xr-x. 2 root root 4096 Aug 20 12:36 conf
drwxr-xr-x. 2 root root   40 Aug 20 12:36 html
drwxr-xr-x. 2 root root    6 Aug 20 12:36 logs
drwxr-xr-x. 2 root root   19 Aug 20 12:36 sbin
[root@qls nginx-1.16.1]# ln  -s  /app/nginx-1.16.1/  /app/nginx
[root@qls nginx-1.16.1]# ll /app/
total 0
lrwxrwxrwx. 1 root root 18 Aug 20 12:37 nginx -> /app/nginx-1.16.1/
drwxr-xr-x. 6 root root 54 Aug 20 12:36 nginx-1.16.1
[root@qls nginx-1.16.1]# /app/nginx/sbin/nginx 
[root@qls nginx-1.16.1]# ss -lntp|grep 80
LISTEN     0      128          *:80                       *:*                   users:(("nginx",pid=12928,fd=6),("nginx",pid=12927,fd=6))

[root@qls nginx-1.16.1]# systemctl  status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-08-20 12:31:46 CST; 7min ago
     Docs: man:firewalld(1)
 Main PID: 6927 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─6927 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Aug 20 12:31:44 qls systemd[1]: Starting firewalld - dynamic firewall daemon...
Aug 20 12:31:46 qls systemd[1]: Started firewalld - dynamic firewall daemon.
[root@qls nginx-1.16.1]# systemctl  stop  firewalld
[root@qls nginx-1.16.1]# systemctl  disable   firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

磁盘管理


#使用fdisk进行分区,支持2TB下的分区,使用MBR分区表
1. 添加磁盘

2.进行分区
[root@qls ~]# fdisk   /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xe23878a4.

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe23878a4

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe23878a4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

3.格式化,创建文件系统
[root@qls ~]# mkfs.xfs     /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

4.创建挂载点,并进行挂载
[root@qls ~]# mkdir   /test_f
[root@qls ~]# 
[root@qls ~]# 
[root@qls ~]# mount  /dev/sdb1  /test_f
[root@qls ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        48G  1.8G   46G   4% /
devtmpfs        980M     0  980M   0% /dev
tmpfs           991M     0  991M   0% /dev/shm
tmpfs           991M  9.6M  981M   1% /run
tmpfs           991M     0  991M   0% /sys/fs/cgroup
/dev/sda1       497M  120M  378M  25% /boot
tmpfs           199M     0  199M   0% /run/user/0
/dev/sdb1       5.0G   33M  5.0G   1% /test_f
[root@qls ~]# cp  /etc/services  /test_f/
[root@qls ~]# ll /test_f/
total 656
-rw-r--r--. 1 root root 670293 Aug 20 15:58 services

[root@qls ~]# umount  /dev/sdb1
[root@qls ~]# ll /test_f/
total 0
[root@qls ~]# mkdir   /test_1
[root@qls ~]# 
[root@qls ~]# mount  /dev/sdb1  /test_1
[root@qls ~]# ll /test_1
total 656
-rw-r--r--. 1 root root 670293 Aug 20 15:58 services


5.永久挂载
[root@qls ~]# tail -1  /etc/fstab
/dev/sdb1                                 /test_1                 xfs     defaults        0 0

gdisk


GPT分区表,支持128分区

#安装gdisk
[root@qls ~]# yum  install  gdisk  -y

[root@qls ~]# gdisk  /dev/sdc
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.

Command (? for help): ?
b	back up GPT data to a file
c	change a partition's name
d	delete a partition
i	show detailed information on a partition
l	list known partition types
n	add a new partition
o	create a new empty GUID partition table (GPT)
p	print the partition table
q	quit without saving changes
r	recovery and transformation options (experts only)
s	sort partitions
t	change a partition's type code
v	verify disk
w	write table to disk and exit
x	extra functionality (experts only)
?	print this menu

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-6442450910, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-6442450910, default = 6442450910) or {+-}size{KMGTP}: 
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): L
0700 Microsoft basic data  0c01 Microsoft reserved    2700 Windows RE          
3000 ONIE boot             3001 ONIE config           4100 PowerPC PReP boot   
4200 Windows LDM data      4201 Windows LDM metadata  7501 IBM GPFS            
7f00 ChromeOS kernel       7f01 ChromeOS root         7f02 ChromeOS reserved   
8200 Linux swap            8300 Linux filesystem      8301 Linux reserved      
8302 Linux /home           8400 Intel Rapid Start     8e00 Linux LVM           
a500 FreeBSD disklabel     a501 FreeBSD boot          a502 FreeBSD swap        
a503 FreeBSD UFS           a504 FreeBSD ZFS           a505 FreeBSD Vinum/RAID  
a580 Midnight BSD data     a581 Midnight BSD boot     a582 Midnight BSD swap   
a583 Midnight BSD UFS      a584 Midnight BSD ZFS      a585 Midnight BSD Vinum  
a800 Apple UFS             a901 NetBSD swap           a902 NetBSD FFS          
a903 NetBSD LFS            a904 NetBSD concatenated   a905 NetBSD encrypted    
a906 NetBSD RAID           ab00 Apple boot            af00 Apple HFS/HFS+      
af01 Apple RAID            af02 Apple RAID offline    af03 Apple label         
af04 AppleTV recovery      af05 Apple Core Storage    be00 Solaris boot        
bf00 Solaris root          bf01 Solaris /usr & Mac Z  bf02 Solaris swap        
bf03 Solaris backup        bf04 Solaris /var          bf05 Solaris /home       
bf06 Solaris alternate se  bf07 Solaris Reserved 1    bf08 Solaris Reserved 2  
bf09 Solaris Reserved 3    bf0a Solaris Reserved 4    bf0b Solaris Reserved 5  
c001 HP-UX data            c002 HP-UX service         ea00 Freedesktop $BOOT   
eb00 Haiku BFS             ed00 Sony system partitio  ed01 Lenovo system partit
Press the <Enter> key to see more codes: 
ef00 EFI System            ef01 MBR partition scheme  ef02 BIOS boot partition 
fb00 VMWare VMFS           fb01 VMWare reserved       fc00 VMWare kcore crash p
fd00 Linux RAID            
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdc: 6442450944 sectors, 3.0 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): E2A6B39D-0ACA-4E24-AFEE-10AC07DFB9C9
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 6442450910
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048      6442450910   3.0 TiB     8300  Linux filesystem

Command (? for help): p
Disk /dev/sdc: 6442450944 sectors, 3.0 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): E2A6B39D-0ACA-4E24-AFEE-10AC07DFB9C9
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 6442450910
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048      6442450910   3.0 TiB     8300  Linux filesystem

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.
[root@qls ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   50G  0 disk 
├─sda1   8:1    0  500M  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0 47.5G  0 part /
sdb      8:16   0   20G  0 disk 
└─sdb1   8:17   0    5G  0 part /test_1
sdc      8:32   0    3T  0 disk 
└─sdc1   8:33   0    3T  0 part 
sr0     11:0    1  4.3G  0 rom  
[root@qls ~]# mkfs.xfs   /dev/sdc1  
meta-data=/dev/sdc1              isize=512    agcount=4, agsize=201326527 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=805306107, imaxpct=5
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=393215, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

[root@qls ~]# mkdir  /gpt
[root@qls ~]# 
[root@qls ~]# 
[root@qls ~]# mount  /dev/sdc1  /gpt
[root@qls ~]# 
[root@qls ~]# 
[root@qls ~]# cp /etc/services  /gpt
[root@qls ~]# 
[root@qls ~]# 
[root@qls ~]# ll /gpt
total 656
-rw-r--r--. 1 root root 670293 Aug 20 16:42 services
[root@qls ~]# umount  /gpt
[root@qls ~]# 
[root@qls ~]# ll /gpt
total 0
[root@qls ~]# ll /opt/
total 0
[root@qls ~]# mount   /dev/sdc1  /opt/
[root@qls ~]# ll /opt/
total 656
-rw-r--r--. 1 root root 670293 Aug 20 16:42 services
[root@qls ~]# umount /opt/
[root@qls ~]# 
[root@qls ~]# 
[root@qls ~]# cp /etc/hosts  /opt
[root@qls ~]# 
[root@qls ~]# 
[root@qls ~]# ll /opt
total 4
-rw-r--r--. 1 root root 158 Aug 20 16:43 hosts
[root@qls ~]# 
[root@qls ~]# 
[root@qls ~]# mount   /dev/sdc1  /opt/
[root@qls ~]# ll /opt/
total 656
-rw-r--r--. 1 root root 670293 Aug 20 16:42 services
[root@qls ~]# umount  /opt
[root@qls ~]# 
[root@qls ~]# 
[root@qls ~]# ll /opt
total 4
-rw-r--r--. 1 root root 158 Aug 20 16:43 hosts

磁盘的挂载方式


mount		#挂载磁盘
选项
	-o		#指定挂载的参数
	-a		#重新挂载/etc/fstab中的挂载列表
	-t		#指定挂载文件系统,xfs	是7系统默认的, ext4 6系统默认

[root@qls ~]# blkid
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/sda1: UUID="ffc53ebe-1964-4fe9-b761-01b1e0a0e2af" TYPE="xfs" 
  ev/sda2: UUID="e2ed12e8-15c2-4061-806e-a02f7847c0d0" TYPE="swap" 
▽dev/sda3: UUID="400a5819-809d-43b4-a323-30f4c3ac7bf0" TYPE="xfs" 
/dev/sdb1: UUID="905bb897-c545-4ee6-b8e9-53436eadaf18" TYPE="xfs" 
/dev/sdc1: UUID="9d441e76-17a1-48f9-a49b-28e3e9ea8d20" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="0bd340a4-52c0-46a0-9cb7-75e771d9be1b" 
[root@qls ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        48G  1.9G   46G   4% /
devtmpfs        980M     0  980M   0% /dev
tmpfs           991M     0  991M   0% /dev/shm
tmpfs           991M  9.7M  981M   1% /run
tmpfs           991M     0  991M   0% /sys/fs/cgroup
/dev/sdb1       5.0G   33M  5.0G   1% /test_1
/dev/sda1       497M  120M  378M  25% /boot
tmpfs           199M     0  199M   0% /run/user/0
[root@qls ~]# mount  UUID="9d441e76-17a1-48f9-a49b-28e3e9ea8d20"  /gpt

umount  	#卸载磁盘
选项
	-l		#强制卸载
	
出现卸载不掉的情况下

	1. 使用强制卸载
	2. 切换到其他目录进行卸载
	

/etc/fstab配置文件


[root@qls ~]# tail  -1  /etc/fstab 
UUID=9d441eb-28e3e9ea8d20 /gpt                    xfs     defaults        0 0

第一列:挂载的设备名,可以使用UUID,还可以网络地址

第二列:挂载点,挂载的入口

第三列:文件系统的类型

第四列:挂载的参数。defaults  

参数 含义
async/sync 是否同步方式运行,默认async(异步)。
user/nouser 是否允许普通用户使用mount命令挂载,默认nouser。
exec/noexec 是否允许可执行文件执行,默认exec。
suid/nosuid 是否允许存在suid属性的文件,默认suid。
auto/noauto 执行mount -a时,此文件系统是否被主动挂载,默认auto。
rw/ro 是否只读或者读写模式进行挂载。默认rw。
defaults 具有rw,suid,exec,auto,nouser,async等默认参数的设定。

第五列:是否通过dump进行备份,0 不备份  ,1 每天备份  2 不定期的备份

第六列:是否检查磁盘,0 不检查  ,1 检查,按照顺序检查,1只能给/

swap


oom  

临时添加虚拟内存

#1. 生成一个1G的大文件
[root@qls ~]# dd  /root/swap.txt  bs=100M  count=10
10+0 records in
10+0 records out
1048576000 bytes (1.0 GB) copied, 29.8756 s, 35.1 MB/s

#修改权限
[root@qls ~]# chmod  600  swap.txt 

#2.让这个成为swap文件
[root@qls ~]# mkswap   -f  swap.txt 
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=d849c5bf-b582-46f6-816e-301ab190bb3a
[root@qls ~]# file  swap.txt 
swap.txt: Linux/i386 swap file (new style), version 1 (4K pages), size 255999 pages, no label, UUID=d849c5bf-b582-46f6-816e-301ab190bb3a
[root@qls ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1980         106         561           9        1313        1677
Swap:          2047           0        2047

#3.添加swap大小
[root@qls ~]# swapon  swap.txt 
swapon: /root/swap.txt: insecure permissions 0644, 0600 suggested.
[root@qls ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1980         106         560           9        1313        1676
Swap:          3047           0        3047

[root@qls ~]# swapoff  swap.txt 	#缩减swap空间
[root@qls ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1980         106         560           9        1313        1677
Swap:          2047           0        2047

[root@qls ~]# swapon -s		#检查当前swap使用那些分区
Filename				Type		Size	Used	Priority
/dev/sda2                              	partition	2097148	0	-2
/root/swap.txt                         	file	1023996	0	-3

#禁用swap

[root@qls ~]# swapoff -a
[root@qls ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1980         106         562           9        1312        1677
Swap:             0           0           0

#开启swap
[root@qls ~]# swapon -a
[root@qls ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1980         106         562           9        1312        1677
Swap:          2047           0        2047

磁盘故障


block 慢
[root@qls ~]# cp Desktop.zip  /data/
cp: error writing ‘/data/Desktop.zip’: No space left on device
cp: failed to extend ‘/data/Desktop.zip’: No space left on device

[root@qls ~]# du  -sh  /* |grep  G
du: cannot access ‘/proc/7780/task/7780/fd/4’: No such file or directory
du: cannot access ‘/proc/7780/task/7780/fdinfo/4’: No such file or directory
du: cannot access ‘/proc/7780/fd/4’: No such file or directory
du: cannot access ‘/proc/7780/fdinfo/4’: No such file or directory
1.1G	/root
1.5G	/usr
5.1G	/var
[root@qls ~]# du  -sh  /var/* |grep  G
4.9G	/var/log
[root@qls ~]# du  -sh  /var/log/* |grep  G
4.9G	/var/log/test.log

#确认之后再删除

[root@qls ~]# rm -f /var/log/test.log

index慢了

[root@qls ~]# df -i
Filesystem       Inodes  IUsed    IFree IUse% Mounted on
/dev/sda3      24909312 263092 24646220    2% /
devtmpfs         250786    403   250383    1% /dev
tmpfs            253511      1   253510    1% /dev/shm
tmpfs            253511    766   252745    1% /run
tmpfs            253511     16   253495    1% /sys/fs/cgroup
/dev/sdb1       2621440      4  2621436    1% /test_1
/dev/sda1        256000    326   255674    1% /boot
tmpfs            253511      1   253510    1% /run/user/0

[root@qls ~]# find  / -type d  -size +3M   |xargs  ls -lhd
drwxr-xr-x. 2 1001 1001 4.7M Aug 20 17:34 /root/nginx-1.16.1/conf

#确认好在删除
[root@qls ~]# find  /root/nginx-1.16.1/conf  -type f  -name '*txt' |xargs   rm -f
[root@qls ~]# find  /root/nginx-1.16.1/conf  -type f  -name '*sh' |xargs   rm -f
[root@qls ~]# find  /root/nginx-1.16.1/conf  -type f  -name '*py' |xargs   rm -f
[root@qls ~]# find  /root/nginx-1.16.1/conf  -type f  -name '*log' |xargs   rm -f
[root@qls ~]# 
[root@qls ~]# 
[root@qls ~]# ll /root/nginx-1.16.1/conf |wc -l
10

开机启动流程


#简单说明CentOS-7系统启动过程:
​
01) 按下电源
​
02) 开机BIOS自检    
​
03) MBR引导系统   
​
04) GRUB菜单
​
05) 加载内核          
​
06) 启动系统进程(使用systemd进行管理)       
​
07) 读取运行级别
​
08) 进行系统初始化
​
09) 启动开机自动启动服务(并行启动)
​
10) 运行getty文件,进入登录页面

运行级别
[root@qls ~]# ll /usr/lib/systemd/system/runlevel*.target 
lrwxrwxrwx. 1 root root 15 Aug 14 15:11 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 Aug 14 15:11 /usr/lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 Aug 14 15:11 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Aug 14 15:11 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Aug 14 15:11 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 Aug 14 15:11 /usr/lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 Aug 14 15:11 /usr/lib/systemd/system/runlevel6.target -> reboot.target


System 运行级别 Systemd目标名称 作用
0 runlevel0.target,poweroff.target 关机
1 runlevel1.target,rescue.target 单用户模式
2 runlevel2.target,multi.target 多用户模式
3 runlevel3.target,multi.target 多用户模式
4 runlevel4.target,multi.target 多用户模式
5 runlevel5.target,graphical.target 图形界面或桌面模式
6 runlevel6.target,reboot.target 重启

[root@qls ~]# systemctl   get-default	#查看运行级别
multi-user.target
[root@qls ~]# runlevel
N 3
[root@qls ~]# systemctl   set-default  graphical.target	#切换运行级别

[root@qls ~]# init  5

主配置文件

ll /etc/systemd/system/default.target

systemd


#systemctl管理服务的启动、停止、重启、重载、状态等常用命令[root@qls ~]# systemctl start vsftpd        #启动[root@qls ~]# systemctl stop vsftpd         #停止[root@qls ~]# systemctl status vsftpd       #查看运行状态[root@qls ~]# systemctl restart vsftpd      #重启[root@qls ~]# systemctl reload  vsftpd      #重载[root@qls ~]# systemctl mask  vsftpd        #禁止服务运行[root@qls ~]# systemctl unmask  vsftpd      #取消禁止服务运行

[root@qls ~]# systemctl enable vsftpd 		#开机自启

[root@qls ~]# systemctl disable  vsftpd 	#关闭开机自启


单用户修改root密码

1、重新启动或开启CentOS-7系统,在选择进入系统的Grub菜单界面有提示按e时,按e进入编辑界面
2、找到Linux16这行,将磁盘改为rw读写模式,然后再在后面添加如下:
init=/bin/bash enforcing=0 #(前者作用让系统登录后加载bash解释器,后者是且关闭Selinux)
3、ctrl+x以单用户模式启动
4、在命令行执行:
	mount -o rw,remount /  # 重新挂载根目录,如果前面改了rw,这里可以不执行
	passwd root  # 设置root用户密码
5、执行 exec /sbin/init 命令重启系统

你可能感兴趣的:(linux基础)