Linux-day06
打包壓縮
壓縮包工具
.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 #開啓緩存
源碼編譯
預編譯
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 </dev/zero >/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
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 #關閉開機自啓