本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/96854
總結
sed -n ? 關閉默認打印
? ? -r ? 擴展正則表達式
? ? -e ? 多點編輯
? ? -i ? 修改文件
? ? -f ? 后面跟文件,文件內容是地址定結和命令
d: 刪除模式空間匹配的行,并立即啟用下一輪循環
p:打印當前模式空間內容,追加到默認輸出之后
a [\]text:在指定行后面追加文本
支持使用\n實現多行追加
i [\]text:在行前面插入文本
c [\]text:替換行為單行或多行文本
w /path/somefile: 保存模式匹配的行至指定文件
r /path/somefile:讀取指定文件的文本至模式空間中
匹配到的行后
=: 為模式空間中的行打印行號
!:模式空間(pattern space)中匹配行取反處理
P:打印模式空間開端至\n內容,并追加到默認輸出之前
h: 把模式空間中的內容覆蓋至保持空間中
H:把模式空間中的內容追加至保持空間中
g: 從保持空間取出數據覆蓋至模式空間
G:從保持空間取出內容追加至模式空間
x: 把模式空間中的內容與保持空間中的內容進行互換
n: 讀取匹配到的行的下一行覆蓋至模式空間
N:讀取匹配到的行的下一行追加至模式空間
d: 刪除模式空間中的行
D:如果模式空間包含換行符,則刪除直到第一個換行符的模式空間中的文本,
并不會讀取新的輸入行,而使用合成的模式空間重新啟動循環。如果模式空間
不包含換行符,則會像發出d命令那樣啟動正常的新循環
sed -n ‘n;p’ FILE ? ?保留偶數行
[root@CENTOS7 ~]#sed -n ‘n;p’ fff
2
4
6
8
10
sed ‘1!G;h;$!d’ FILE ? ? 倒序排列
[root@CENTOS7 ~]#sed ‘1!G;h;$!d’ fff
10
9
8
7
6
5
4
3
2
1
sed ‘N;D‘ FILE ? ? 只保留最后一行
[root@CENTOS7 ~]#sed ‘N;D’ fff
10
sed ‘$!N;$!D’ FILE
[root@CENTOS7 ~]#sed ‘$!N;$!D’ fff
9
10
sed ‘$!d’ FILE
[root@CENTOS7 ~]#sed ‘$!d’ fff
10
sed ‘G’ FILE ? ? 將空白行追加
[root@CENTOS7 ~]#sed ‘G’ fff
1
2
3
4
5
6
7
8
9
10
sed ‘g’ FILE ? ? 覆蓋變成10個空白行
[root@CENTOS7 ~]#sed ‘g’ fff
sed ‘/^$/d;G’ FILE
[root@CENTOS7 ~]#sed ‘/^$/d;G’ fff
1
2
3
4
5
6
7
8
9
10
sed ‘n;d’ FILE
[root@CENTOS7 ~]#sed ‘n;d’ fff
1
3
5
7
9
sed -n ‘1!G;h;$p’ FILE
[root@CENTOS7 ~]#sed -n ‘1!G;h;$p’ fff
10
9
8
7
6
5
4
3
2
1
[root@CENTOS7 ~]#sed ‘n;g’ fff ? 從文本中覆蓋能打印,從保持空間覆蓋不能打印。
1
3
5
7
9
7、統計centos安裝光盤中Package目錄下的所有rpm文件的以.分隔倒數第二個
字段的重復次數
第一種做法
[root@CENTOS7 ~]#ls /run/media/root/CentOS\ 7\ x86_64/Packages/ | sed -r ‘s/.*\.([^.]+)\.rpm$/\1/’ | sort |uniq -c
? ?2141 i686
? ?3076 noarch
? ? ? 1 TRANS.TBL
? ?4374 x86_64
第二種做法
[root@CENTOS7 Packages]#ls *.rpm | rev | cut -d “.” -f2 | sort | uniq -c
? ?4374 46_68x
? ?2141 686i
? ?3076 hcraon
第三種做法
[root@CENTOS7 Packages]#ls *.rpm | egrep -o “[^.]+\.rpm$” | cut -d “.” -f1 |sort | uniq -c
? ?2141 i686
? ?3076 noarch
? ?4374 x86_64
將文本文件的n和n+1行合并為一行,n為奇數行
第一種
[root@CENTOS7 ~]#sed ‘N;s/\n/ /’ fff
1 2
3 4
5 6
7 8
9 10
第二種
[root@CENTOS7 ~]#cat fff | xargs -n2
1 2
3 4
5 6
7 8
9 10
ldd /bin/ls ? ?ldd ?查看ls調用的數據庫
linux-vdso.so.1 => ?(0x00007ffcd42c8000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f8c1bd84000)
libcap.so.2 => /lib64/libcap.so.2 (0x00007f8c1bb7f000)
libacl.so.1 => /lib64/libacl.so.1 (0x00007f8c1b975000)
libc.so.6 => /lib64/libc.so.6 (0x00007f8c1b5b2000)
libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f8c1b350000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f8c1b14b000)
/lib64/ld-linux-x86-64.so.2 (0x000055598db21000)
libattr.so.1 => /lib64/libattr.so.1 (0x00007f8c1af46000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f8c1ad2a000)
如果刪除了/lib64/libc.so.6 如何救援
1.重啟按EXC
2.CD-ROM Drive
3.Troubleshooting
4.Rescue a CentOS system
5.1continue
6.回車
7.cd /mnt/sysimage/lib64 ?文件夾
8.ln -s libc-2.17.so lib.so.6 ? 用相對路徑,不能用絕對路徑
源代碼命名方式 名字-版本號(主,次,補丁).壓縮方式
rpm包命名方式:
name-VERSION-release.arch.rpm
例:bash-4.(主)2.(次)46-19(打包了多少次).el7(rhel7).x86_64.rpm
VERSION: major.minor.release
release:release.OS
常見的arch:
x86: i386, i486, i586, i686
x86_64: x64, x86_64, amd64
powerpc: ppc
跟平臺無關:noarch
包:分類和拆包
Application-VERSION-ARCH.rpm: 主包
Application-devel-VERSION-ARCH.rpm 開發子包
Application-utils-VERSION-ARHC.rpm 其它子包
Application-libs-VERSION-ARHC.rpm 其它子包
包之間:可能存在依賴關系,甚至循環依賴
解決依賴包管理工具:
yum:rpm包管理器的前端工具
[root@CENTOS7 Packages]#ldconfig -p ? ? ?ldconfig查看所有系統調用的庫
904 libs found in cache `/etc/ld.so.cache’
p11-kit-trust.so (libc6,x86-64) => /lib64/p11-kit-trust.so
libz.so.1 (libc6,x86-64) => /lib64/libz.so.1
libyelp.so.0 (libc6,x86-64) => /lib64/libyelp.so.0
libyajl.so.2 (libc6,x86-64) => /lib64/libyajl.so.2
[root@CENTOS7 Packages]#ldd /bin/cp ? ? ?ldd只能查看一個程序調用的庫
linux-vdso.so.1 => ?(0x00007fff5cfab000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f2f83183000)
libacl.so.1 => /lib64/libacl.so.1 (0x00007f2f82f7a000)
libattr.so.1 => /lib64/libattr.so.1 (0x00007f2f82d74000)
libc.so.6 => /lib64/libc.so.6 (0x00007f2f829b1000)
libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f2f8274f000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f2f8254a000)
/lib64/ld-linux-x86-64.so.2 (0x00005617a7e40000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f2f8232e000)
查看二進制程序所依賴的庫文件
ldd /PATH/TO/BINARY_FILE ?列如 ldd /bin/ls
管理及查看本機裝載的庫文件
ldconfig 加載庫文件
/sbin/ldconfig -p: 顯示本機已經緩存的所有可用庫文件名及文件路徑
映射關系
配置文件:/etc/ld.so.conf(主配置文件), /etc/ld.so.conf.d/*.conf
緩存文件:/etc/ld.so.cache
包文件組成 (每個包獨有)
RPM包內的文件
RPM的元數據,如名稱,版本,依賴性,描述等
安裝或卸載時運行的腳本
數據庫(公共):/var/lib/rpm
程序包名稱及版本
依賴關系
功能說明
包安裝后生成的各文件路徑及校驗碼信息
[root@CENTOS7 Packages]#rpm -qp –scripts ?httpd-2.4.6-67.el7.centos.x86_64.rpm ? ? rpm -qp(-p是看文本) –script httpd
warning: httpd-2.4.6-67.el7.centos.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
preinstall scriptlet (using /bin/sh):
# Add the “apache” group and user
/usr/sbin/groupadd -g 48 -r apache 2> /dev/null || :
/usr/sbin/useradd -c “Apache” -u 48 -g apache \
-s /sbin/nologin -r -d /usr/share/httpd apache 2> /dev/null || :
postinstall scriptlet (using /bin/sh):
if [ $1 -eq 1 ] ; then
? ? ? ? # Initial installation
? ? ? ? systemctl preset httpd.service htcacheclean.service >/dev/null 2>&1 || :
fi
preuninstall scriptlet (using /bin/sh):
if [ $1 -eq 0 ] ; then
? ? ? ? # Package removal, not upgrade
? ? ? ? systemctl –no-reload disable httpd.service htcacheclean.service > /dev/null 2>&1 || :
? ? ? ? systemctl stop httpd.service htcacheclean.service > /dev/null 2>&1 || :
fi
postuninstall scriptlet (using /bin/sh):
systemctl daemon-reload >/dev/null 2>&1 || :
# Trigger for conversion from SysV, per guidelines at:
# https://fedoraproject.org/wiki/Packaging:ScriptletSnippets#Systemd
posttrans scriptlet (using /bin/sh):
test -f /etc/sysconfig/httpd-disable-posttrans || \
? /bin/systemctl try-restart httpd.service htcacheclean.service >/dev/null 2>&1 || :
[root@CENTOS7 Packages]#file /var/lib/rpm/* ? ?數據庫文件,大多數是2進制文件。不可刪除,否則無法安裝或者卸載軟件
/var/lib/rpm/Basenames: ? ?Berkeley DB (Btree, version 9, native byte-order)
/var/lib/rpm/Conflictname: Berkeley DB (Btree, version 9, native byte-order)
/var/lib/rpm/__db.001: ? ? Applesoft BASIC program data
/var/lib/rpm/__db.002: ? ? 386 pure executable
/var/lib/rpm/__db.003: ? ? 386 pure executable not stripped
/var/lib/rpm/Dirnames: ? ? Berkeley DB (Btree, version 9, native byte-order)
/var/lib/rpm/Group: ? ? ? ?Berkeley DB (Btree, version 9, native byte-order)
/var/lib/rpm/Installtid: ? Berkeley DB (Btree, version 9, native byte-order)
/var/lib/rpm/Name: ? ? ? ? Berkeley DB (Btree, version 9, native byte-order)
/var/lib/rpm/Obsoletename: Berkeley DB (Btree, version 9, native byte-order)
/var/lib/rpm/Packages: ? ? Berkeley DB (Hash, version 9, native byte-order)
/var/lib/rpm/Providename: ?Berkeley DB (Btree, version 9, native byte-order)
/var/lib/rpm/Requirename: ?Berkeley DB (Btree, version 9, native byte-order)
/var/lib/rpm/Sha1header: ? Berkeley DB (Btree, version 9, native byte-order)
/var/lib/rpm/Sigmd5: ? ? ? Berkeley DB (Btree, version 9, native byte-order)
/var/lib/rpm/Triggername: ?Berkeley DB (Btree, version 9, native byte-order)
獲取程序包的途徑:
(1) 系統發版的光盤或官方的服務器;
CentOS鏡像:
https://www.centos.org/download/
http://mirrors.aliyun.com
http://mirrors.sohu.com
http://mirrors.163.com
第三方組織:
Fedora-EPEL:
Extra Packages for Enterprise Linux
Rpmforge:RHEL推薦,包很全
搜索引擎:
http://pkgs.org
http://rpmfind.net
http://rpm.pbone.net
https://sourceforge.net/
rpm包管理
rpm {-i|–install} [install-options] PACKAGE_FILE…
-v: verbose
-vv:
-h: 以#顯示程序包管理執行進度
rpm -ivh PACKAGE_FILE …
[install-options]
–test: 測試安裝,但不真正執行安裝,即dry run模式
–nodeps:忽略依賴關系
–replacepkgs | replacefiles
–nosignature: 不檢查來源合法性
–nodigest:不檢查包完整性
–noscripts:不執行程序包腳本
%pre: 安裝前腳本; –nopre
%post: 安裝后腳本; –nopost
%preun: 卸載前腳本; –nopreun
%postun: 卸載后腳本; –nopostun
[root@centos Packages]#rpm -ivh httpd-2.2.15-59.el6.centos.x86_64.rpm ? 安裝需要跟文件名
[root@centos Packages]#rpm -e vsftpd ? ?卸載跟包名,就是軟件的名字
[root@CENTOS7 Packages]#rpm -ql tree ? ?查看tree包內的所有文件
/usr/bin/tree
/usr/share/doc/tree-1.6.0
/usr/share/doc/tree-1.6.0/LICENSE
/usr/share/doc/tree-1.6.0/README
/usr/share/man/man1/tree.1.gz
rm /usr/bin/tree ? 刪除后tree命令不能使用
rpm -ivh tree-1.6.0-10.el7.x86_64.rpm ? ?顯示已經安裝,因為/var/lib/rpm數據庫中存放了tree的數據
rpm -ivh –replacepkgs tree-1.6.0-10.el7.x86_64.rpm ?用–replacepkgs或者–force
[root@CENTOS7 Packages]#ll /var/lib/rpm ? ?從新安裝后數據庫時間更新
total 81016
-rw-r–r–. 1 root root ?3616768 Apr 20 10:02 Basenames
-rw-r–r–. 1 root root ? ?16384 Mar 27 18:02 Conflictname
-rw-r–r–. 1 root root ? 286720 Apr 20 10:02 __db.001
-rw-r–r–. 1 root root ? ?90112 Apr 20 10:02 __db.002
-rw-r–r–. 1 root root ?1318912 Apr 20 10:02 __db.003
-rw-r–r–. 1 root root ?1064960 Apr 20 10:02 Dirnames
-rw-r–r–. 1 root root ? ?32768 Apr 20 10:02 Group
-rw-r–r–. 1 root root ? ?20480 Apr 20 10:02 Installtid
-rw-r–r–. 1 root root ? ?69632 Apr 20 10:02 Name
-rw-r–r–. 1 root root ? ?32768 Mar 27 18:02 Obsoletename
-rw-r–r–. 1 root root 73494528 Apr 20 10:02 Packages
-rw-r–r–. 1 root root ?2355200 Apr 20 10:02 Providename
-rw-r–r–. 1 root root ? 483328 Apr 20 10:02 Requirename
-rw-r–r–. 1 root root ? 126976 Apr 20 10:02 Sha1header
-rw-r–r–. 1 root root ? ?73728 Apr 20 10:02 Sigmd5
-rw-r–r–. 1 root root ? ? 8192 Mar 27 18:00 Triggername
replacefiles ?A包中有file1,B包中也有file1,想安裝B包時使用
列如
rpm –import RPM-GPG-KEY-CentOS-6 ? ?導入公鑰安裝rpm包時就不會有NOKEY的報錯
[root@centos CentOS_6.9_Final]#rpm -ivh Packages/samba-3.6.23-41.el6.x86_64.rpm
Preparing… ? ? ? ? ? ? ? ?########################################### [100%]
? ?1:samba ? ? ? ? ? ? ? ? ?########################################### [100%]
公鑰只檢查簽名,不檢查文件內容的完整性
rpm包升級 ? ? 新版本會覆蓋舊版本
rpm {-U|–upgrade} [install-options] ?PACKAGE_FILE …
rpm {-F|–freshen} [install-options] ?PACKAGE_FILE …
upgrade:安裝有舊版程序包,則“升級”
如果不存在舊版程序包,則“安裝”
freshen:安裝有舊版程序包,則“升級”
如果不存在舊版程序包,則不執行升級操作
rpm -Uvh PACKAGE_FILE …
rpm -Fvh PACKAGE_FILE …
–oldpackage:降級
–force: 強制安裝
注意:
(1) 不要對內核做升級操作;Linux支持多內核版本并存,因此,對直接安裝新版
本內核
(2) 如果原程序包的配置文件安裝后曾被修改,升級時,新版本的提供的同一個配
置文件并不會直接覆蓋老版本的配置文件,而把新版本的文件重命名
(FILENAME.rpmnew)后保留,一般情況下2進制文件都會被覆蓋
[root@centos CentOS_6.9_Final]#uname -r ? 查看內核版本
2.6.32-696.el6.x86_64
[root@centos CentOS_6.9_Final]#rpm -q kernel ?查看內核版本
kernel-2.6.32-696.el6.x86_64
scp vsftpd-3.0.2-22.el7.x86_64.rpm 172.20.102.175:/root
[root@centos Packages]#rpm -ivh vsftpd-2.2.2-24.el6.x86_64.rpm
Preparing… ? ? ? ? ? ? ? ?########################################### [100%]
? ?1:vsftpd ? ? ? ? ? ? ? ? ########################################### [100%]
[root@centos Packages]#rpm -Fvh /root/vsftpd-3.0.2-22.el7.x86_64.rpm
warning: /root/vsftpd-3.0.2-22.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
error: Failed dependencies:
libc.so.6(GLIBC_2.14)(64bit) is needed by vsftpd-3.0.2-22.el7.x86_64
libc.so.6(GLIBC_2.15)(64bit) is needed by vsftpd-3.0.2-22.el7.x86_64
無法升級因為依賴的文件版本不同,可以下載2.7-2.9升級
包查詢
rpm {-q|–query} [select-options] [query-options]
[select-options]
-a: 所有包
-f: 查看指定的文件由哪個程序包安裝生成
-p rpmfile:針對尚未安裝的程序包文件做查詢操作
–whatprovides ?CAPABILITY :查詢指定的 CAPABILITY 由哪個包所提供
–whatrequires ?CAPABILITY :查詢指定的 CAPABILITY 被哪個包所依賴
rpm2cpio 包文件|cpio –itv 預覽包內文件
rpm2cpio 包文件|cpio –id “*.conf” 釋放包內文件
[root@centos ~]#rpm -q vsftpd ? 查詢包是否安裝,后面直接跟包名,包名必須準確,不然無法查詢
vsftpd-2.2.2-24.el6.x86_64
[root@centos ~]#rpm -e vsftpd
[root@centos ~]#rpm -q vsftpd || rpm -i /misc/cd/Packages/
Display all 3241 possibilities? (y or n)
[root@centos ~]#rpm -q vsftpd &> /dev/null || rpm -i /misc/cd/Packages/vsftpd-2.2.2-24.el6.x86_64.rpm
[root@centos ~]#rpm -q vsftpd
vsftpd-2.2.2-24.el6.x86_64
[root@centos ~]#rpm -qa | grep “vsf” ? -qa列出所有安裝過的包
vsftpd-2.2.2-24.el6.x86_64
[root@centos ~]#rpm -qf /bin/ls
coreutils-8.4-46.el6.x86_64
[root@centos ~]#rpm -ql tree
/usr/bin/tree
/usr/share/doc/tree-1.5.3
/usr/share/doc/tree-1.5.3/LICENSE
/usr/share/doc/tree-1.5.3/README
/usr/share/man/man1/tree.1.gz
[root@centos ~]#rm -f /usr/bin/tree
[root@centos ~]#rpm -qf /usr/bin/tree
tree-1.5.3-3.el6.x86_64
[root@centos ~]#rpm -ivh /misc/cd/Packages/tree-1.5.3-3.el6.x86_64.rpm –force
Preparing… ? ? ? ? ? ? ? ?########################################### [100%]
? ?1:tree ? ? ? ? ? ? ? ? ? ########################################### [100%]
刪除/usr/bin/tree 依然可以查詢因為/var/lib/rpm數據庫中可以查到
mv /var/lib/rpm/* /data
數據庫被移走 導致rpm命令失效,無法查詢,安裝,卸載。
雖然/var/bin/rpm被移走后生成了新的文件,但是文件內容是空的。
rpm -ql 無法查詢沒有安裝的包內的文件,如果想要查詢則需要rpm -qpl 可以查詢
[root@centos Packages]#rpm -ql zsh
package zsh is not installed
[root@centos Packages]#rpm -qlp zsh-4.3.11-4.el6.centos.2.x86_64.rpm
/bin/zsh
/etc/skel/.zshrc
/etc/zlogin
注意: -p 后面需要跟文件名
[root@centos Packages]#rpm -q –whatprovides bash ? 誰提供了bash功能
bash-4.1.2-48.el6.x86_64
root@centos Packages]#rpm -q –requires bash ? ? ? 依賴bash功能的庫有哪些
mysql-5.1.73-8.el6_8.x86_64
initscripts-9.03.58-1.el6.centos.x86_64
dracut-004-409.el6_8.2.noarch
rsyslog-5.8.10-10.el6_6.x86_64
cronie-1.4.4-16.el6_8.2.x86_64
autofs-5.0.5-132.el6.x86_64
lvm2-2.02.143-12.el6.x86_64
rpm2cpio命令用法
[root@centos Packages]#rpm -ql tree
/usr/bin/tree
/usr/share/doc/tree-1.5.3
/usr/share/doc/tree-1.5.3/LICENSE
/usr/share/doc/tree-1.5.3/README
/usr/share/man/man1/tree.1.gz
[root@centos Packages]#rm -f /usr/bin/tree
[root@centos Packages]#tree
-bash: /usr/bin/tree: No such file or directory
[root@centos Packages]#rpm2cpio /media/CentOS_6.9_Final/Packages/tree-1.5.3-3.el6.x86_64.rpm | cpio -tv
-rwxr-xr-x ? 1 root ? ? root ? ? ? ?41136 Jan 14 ?2015 ./usr/bin/tree
drwxr-xr-x ? 2 root ? ? root ? ? ? ? ? ?0 Jan 14 ?2015 ./usr/share/doc/tree-1.5.3
-rw-r–r– ? 1 root ? ? root ? ? ? ?18009 Aug 13 ?2004 ./usr/share/doc/tree-1.5.3/LICENSE
-rw-r–r– ? 1 root ? ? root ? ? ? ? 4167 Oct 20 ?2009 ./usr/share/doc/tree-1.5.3/README
-rw-r–r– ? 1 root ? ? root ? ? ? ? 3375 Jan 14 ?2015 ./usr/share/man/man1/tree.1.gz
132 blocks
[root@centos ~]#rpm2cpio /media/CentOS_6.9_Final/Packages/tree-1.5.3-3.el6.x86_64.rpm | cpio -idv ./usr/bin/tree
./usr/bin/tree
132 blocks
[root@centos ~]#mv usr/bin/tree /usr/bin/
缺點 可能導致文件所有者所屬組權限發生改變
[query-options]
–changelog:查詢rpm包的changelog
-c: 查詢程序的配置文件
-d: 查詢程序的文檔
-i: information
-l: 查看指定的程序包安裝后生成的所有文件
–scripts:程序包自帶的腳本
–provides: 列出指定程序包所提供的CAPABILITY
-R: 查詢指定的程序包所依賴的CAPABILITY
[root@centos ~]#rpm -qi bash ? 查詢bash的信息
Name ? ? ? ?: bash ? ? ? ? ? ? ? ? ? ? ? ? Relocations: (not relocatable)
Version ? ? : 4.1.2 ? ? ? ? ? ? ? ? ? ? ? ? ? ? Vendor: CentOS
Release ? ? : 48.el6 ? ? ? ? ? ? ? ? ? ? ? ?Build Date: Thu 23 Mar 2017 08:17:20 AM CST
Install Date: Tue 27 Mar 2018 05:00:33 PM CST ? ? ?Build Host: c1bm.rdu2.centos.org
Group ? ? ? : System Environment/Shells ? ? Source RPM: bash-4.1.2-48.el6.src.rpm
Size ? ? ? ?: 3142529 ? ? ? ? ? ? ? ? ? ? ? ? ?License: GPLv3+
Signature ? : RSA/SHA1, Thu 23 Mar 2017 10:59:39 PM CST, Key ID 0946fca2c105b9de
Packager ? ?: CentOS BuildSystem <http://bugs.centos.org>
URL ? ? ? ? : http://www.gnu.org/software/bash
Summary ? ? : The GNU Bourne Again shell
Description :
The GNU Bourne Again shell (Bash) is a shell or command language
interpreter that is compatible with the Bourne shell (sh). Bash
incorporates useful features from the Korn shell (ksh) and the C shell
(csh). Most sh scripts can be run by bash without modification.
常用查詢用法:
-qi PACKAGE, -qf FILE, -qc PACKAGE, -ql PACKAGE, -qd PACKAGE
-qpi PACKAGE_FILE, -qpl PACKAGE_FILE, …
-qa
包卸載:
rpm {-e|–erase} [–allmatches] [–nodeps] [–noscripts] [–notriggers]
[–test] PACKAGE_NAME …
rpm {-V|–verify} [select-options] [verify-options]
S file Size differs
M Mode differs (includes permissions and file type)
5 digest (formerly MD5 sum) differs
D Device major/minor number mismatch
L readLink(2) path mismatch
U User ownership differs
G Group ownership differs
T mTime differs
P capabilities differ
[root@centos ~]#rpm -V tree
…….T. ? ?/usr/bin/tree
[root@centos ~]#echo >> /usr/bin/tree
[root@centos ~]#rpm -V tree
S.5….T. ? ?/usr/bin/tree
[root@centos ~]#rpm -Va ? ?檢查所有
rpm -k 文件名 ? ? ? 檢查文件完整性
[root@centos Packages]#rpm -K samba-3.6.23-41.el6.x86_64.rpm
samba-3.6.23-41.el6.x86_64.rpm: rsa sha1 (md5) pgp md5 OK
[root@centos ~]#echo >> samba-3.6.23-41.el6.x86_64.rpm
[root@centos ~]#rpm -K samba-3.6.23-41.el6.x86_64.rpm
samba-3.6.23-41.el6.x86_64.rpm: rsa sha1 (MD5) PGP MD5 NOT OK
如果沒有導入公鑰則文件都不OK
公鑰的導入地點
[root@CENTOS7 ~]#ll /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
-rw-r–r–. 1 root root 1690 Aug 30 ?2017 /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[root@CENTOS7 ~]#ll /run/media/root/CentOS\ 7\ x86_64/RPM-GPG-KEY-CentOS-7
-rw-rw-r–. 3 root root 1690 Dec 10 ?2015 /run/media/root/CentOS 7 x86_64/RPM-GPG-KEY-CentOS-7
導入公鑰
[root@CENTOS7 ~]#rpm –import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[root@CENTOS7 ~]#rpm -K /run/media/root/CentOS\ 7\ x86_64/Packages/tree-1.6.0-10.el7.x86_64.rpm
/run/media/root/CentOS 7 x86_64/Packages/tree-1.6.0-10.el7.x86_64.rpm: rsa sha1 (md5) pgp md5 OK
[root@CENTOS7 ~]#rpm -qa “gpg*”
gpg-pubkey-f4a80eb5-53a7ff4b
rpm -e gpg-pubkey-f4a80eb5-53a7ff4b ? 刪除公鑰后
rpm -K /run/media/root/CentOS\ 7\ x86_64/Packages/tree-1.6.0-10.el7.x86_64.rpm
/run/media/root/CentOS 7 x86_64/Packages/tree-1.6.0-10.el7.x86_64.rpm: RSA sha1 ((MD5) PGP) md5 NOT OK (MISSING KEYS: (MD5) PGP#f4a80eb5)
rpm -e rpm –nodeps ? ? ?將rpm卸載
恢復
1.reboot
2.exc
3.進入CD-ROM Drive
4.rescue installed system
5.english
6.us
7.錯誤是否需要聯網
8.comtinue
9.mkdir /mnt/cdroom
10.mount /dev/sr0 /mnt/cdroom
11.rpm -ivh /mnt/cdroom/Packages/rpm-4.8.055.e16.x86_64.rpm –root=/mnt/sysimage
12.chroot /mnt/sysimage ? ?切回真正的/
yum
工作原理
需要使用yum首先要有c/s結構即client/server
server中存放rpm包,meta data。megadata中存放rpm的元數據
當client需要安裝rpm包時,首先要配置文件,文件內容是要訪問的server路徑。yum install tree 這時訪問server 從metadata中查找是否有tree包,如果有這將metadata緩存到client中。然后在到server的rpm中找到tree以及tree依賴的包下載到client中安裝,安裝完成后默認刪除下載的tree以及tree依賴的包
server 中可以有多個廠庫比如cdroom,epel源。當有多個廠庫時,下載時在多個廠庫中查找,找到則安裝,如果多個廠庫都有要安裝的rpm包則安裝版本高的。
如果已經緩存server中的metadata。改變配置文件中的地址但是緩存的地址沒有改變會導致出錯
yum使用是有錯誤一般2種情況
1.配置文件格式寫錯
2.緩存出問題.可以清除緩存
yum的配置文件
vim /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch(x86_64)/$releasever(7) ? ?緩存文件
keepcache=0 ?meta data會緩存 ? rpm安裝完成后會刪除。將keepcache該成1則不會刪除rpm包
debuglevel=2 ? 調試
logfile=/var/log/yum.log ?用yum安裝的日志
exactarch=1 ? 架構
obsoletes=1 ? 過時的
gpgcheck=1 ? ?檢查rpm包完整性及簽名
plugins=1 ? ? 插件啟用
installonly_limit=5 ? 并行安裝同時安裝5個包,但是在一個機器上不可運行多個yum。
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page
.php?category=yum
distroverpkg=centos-release
interupting your command line usage, it’s much better to have something
“/etc/yum.conf” 26L, 970C written
[root@CENTOS7 ~]#cd /etc/yum.repos.d/
[root@CENTOS7 yum.repos.d]#ls
CentOS-Base.repo ?CentOS-Debuginfo.repo ?CentOS-Media.repo ? ?CentOS-Vault.repo
CentOS-CR.repo ? ?CentOS-fasttrack.repo ?CentOS-Sources.repo
[root@CENTOS7 yum.repos.d]#mkdir bak
[root@CENTOS7 yum.repos.d]#mv *.repo bak/
[root@CENTOS7 yum.repos.d]#ls
bak
配置yum的配置文件
[root@CENTOS7 yum.repos.d]#vim cdrom.repo
[base] ? 不要加空格
baseurl=file:///mnt/cdrom
gpgcheck=0
元數據存放在repodata中當配置baseurl時路徑就是repodata的父路徑.
配置baseurl時路徑最好不要有空格,所以mkdir /mnt/cdrom,將光盤掛在到建立的文件夾上 mount /dev/sr0 /mnt/cdrom
yum install httpd ?就可以解決依賴性問題
[root@CENTOS7 ~]#yum repolist ? 查看有幾個廠庫
Loaded plugins: fastestmirror, langpacks
Repository ‘base’ is missing name in configuration, using id
Loading mirror speeds from cached hostfile
repo id ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?repo name ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?status
base ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? base ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 9,591
repolist: 9,591
[root@CENTOS7 ~]#vim /etc/yum.repos.d/cdrom.repo
[base]
name=cdrom
baseurl=file:///mnt/cdrom
gpgcheck=0
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
gpgcheck=0
[root@CENTOS7 ~]#cd /var/cache/yum/x86_64/7
[root@CENTOS7 7]#ll
total 16
drwxr-xr-x. 4 root root ?278 Apr 20 15:16 base
drwxr-xr-x. 4 root root 4096 Apr 20 15:16 epel
drwxr-xr-x. 4 root root ?183 Apr 19 13:56 extras
-rw-r–r–. 1 root root ? 70 Apr 20 15:16 timedhosts
-rw-r–r–. 1 root root ?440 Apr 19 13:57 timedhosts.txt
drwxr-xr-x. 4 root root 4096 Apr 19 13:57 updates
[root@CENTOS7 7]#du -sh /var/cache/yum/x86_64/7
482M /var/cache/yum/x86_64/7
[root@CENTOS7 7]#yum clean all ? ? ?清除緩存
Loaded plugins: fastestmirror, langpacks
Cleaning repos: base epel
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
[root@CENTOS7 7]#du -sh . ? 是原來配置的文件元數據的緩存
426M .
yum list ? 列出廠庫中的所有包帶@是已經裝過的包
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-CentOS-7 ? ?在配置文件中自動導入公鑰
enabled=0 ? ? ? 在配置文件中禁用廠庫
[root@CENTOS7 7]#yum repolist all ?顯示所有倉庫
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
repo id ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?repo name ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?status
base ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cdrom ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?enabled: 9,591
epel ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? epel ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? disabled ? ? ? ?被禁用
repolist: 9,591
[root@CENTOS7 7]#yum repolist disabled ? ? 查看被禁用的倉庫
Loaded plugins: fastestmirror, langpacks
repo id ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?repo name
epel ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? epel
repolist: 0
[root@CENTOS7 7]#ll /var/log/yum.log ? ?用yum安裝后log中已經有內容了
-rw——-. 1 root root 725 Apr 20 15:59 /var/log/yum.log
[root@CENTOS7 7]#yum remove sl ? ?卸載sl
[root@CENTOS7 7]#cat /var/log/yum.log
Apr 20 15:02:00 Installed: apr-1.4.8-3.el7.x86_64
Apr 20 15:02:00 Installed: apr-util-1.5.2-6.el7.x86_64
Apr 20 15:02:00 Installed: httpd-tools-2.4.6-67.el7.centos.x86_64
Apr 20 15:02:00 Installed: mailcap-2.1.41-2.el7.noarch
Apr 20 15:02:08 Installed: httpd-2.4.6-67.el7.centos.x86_64
Apr 20 15:44:57 Installed: libmpc-1.0.1-3.el7.x86_64
Apr 20 15:45:01 Installed: cpp-4.8.5-16.el7.x86_64
Apr 20 15:45:03 Installed: kernel-headers-3.10.0-693.el7.x86_64
Apr 20 15:45:04 Installed: glibc-headers-2.17-196.el7.x86_64
Apr 20 15:45:04 Installed: glibc-devel-2.17-196.el7.x86_64
Apr 20 15:45:10 Installed: gcc-4.8.5-16.el7.x86_64
Apr 20 15:50:29 Installed: sl-5.02-1.el7.x86_64
Apr 20 15:59:34 Installed: tree-1.6.0-10.el7.x86_64
Apr 20 16:07:19 Erased: sl-5.02-1.el7.x86_64
yum客戶端配置文件:
/etc/yum.conf:為所有倉庫提供公共配置
/etc/yum.repos.d/*.repo:為倉庫的指向提供配置
倉庫指向的定義:
[repositoryID]
name=Some name for this repository
baseurl=url://path/to/repository/
enabled={1|0}
gpgcheck={1|0}
gpgkey=URL
enablegroups={1|0}
failovermethod={roundrobin|priority} ?路徑優先級??梢詫aseurl=file:///mnt/cdrom
https://mirrors.aliyun.com/epel/7/x86_64/ 這兩個路徑寫入/root/file1中。配置文件中寫成mirrorlist=file:///root/file1
roundrobin:意為隨機挑選,默認值
priority:按順序訪問
cost= 默認為1000 ? ? ?值越小越優先
yum的repo配置文件中可用的變量:
$releasever: 當前OS的發行版的主版本號
$arch: 平臺,i386,i486,i586,x86_64等
$basearch:基礎平臺;i386, x86_64
$YUM0-$YUM9:自定義變量
http://server/centos/$releasever/$basearch/
http://server/centos/7/x86_64
http://server/centos/6/i384
[root@CENTOS7 7]#yum install autofs ? ?centos7的神奇目錄misc
[root@CENTOS7 7]#rpm -ql autofs
/usr/lib/systemd/system/autofs.service ? ?這是一個service文件需要啟動服務
[root@CENTOS7 7]#systemctl start autofs ? ?啟動服務
[root@CENTOS7 7]#systemctl enable autofs ? 開機時自動啟動此服務
Created symlink from /etc/systemd/system/multi-user.target.wants/autofs.service to /usr/lib/systemd/system/autofs.service.
[root@CENTOS7 7]#ls /misc
[root@CENTOS7 7]#ls /misc/cd ? 已經可用
CentOS_BuildTag ?EULA ?images ? ?LiveOS ? ?repodata ? ? ? ? ? ? ?RPM-GPG-KEY-CentOS-Testing-7
EFI ? ? ? ? ? ? ?GPL ? isolinux ?Packages ?RPM-GPG-KEY-CentOS-7 ?TRANS.TBL
修改配置文件vim /etc/yum.repos.d/cdrom.repo ?讓baseurl=file:///misc/cd
yum clean all 清除緩存 。 yum repolist ?后可以正常使用yum
阿里云repo文件:
http://mirrors.aliyun.com/repo/
CentOS系統的yum源
阿里云:https://mirrors.aliyun.com/centos/$releasever/os/x86_64/
EPEL的yum源:
阿里云: https://mirrors.aliyun.com/epel/$releasever/x86_64
如何直接使用阿里云的yum服務
[root@CENTOS7 7]#cd /etc/yum.repos.d/
[root@CENTOS7 yum.repos.d]#wget http://mirrors.aliyun.com/repo/Centos-7.repo ? 從網上復制的路徑
[root@CENTOS7 yum.repos.d]#yum repolist
Loaded plugins: fastestmirror, langpacks
Repository base is listed more than once in the configuration
Loading mirror speeds from cached hostfile
?* base: mirrors.aliyun.com
?* extras: mirrors.aliyun.com
?* updates: mirrors.aliyun.com
repo id ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?repo name ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?status
base/7/x86_64 ? ? ? ? ? ? ? ? ? ? ? ? ? ?CentOS-7 – Base – mirrors.aliyun.com ? ? ? ? ? ? ? ? ? ? ? ? ? 9,591
!extras/7/x86_64 ? ? ? ? ? ? ? ? ? ? ? ? CentOS-7 – Extras – mirrors.aliyun.com ? ? ? ? ? ? ? ? ? ? ? ? ? 448
!updates/7/x86_64 ? ? ? ? ? ? ? ? ? ? ? ?CentOS-7 – Updates – mirrors.aliyun.com ? ? ? ? ? ? ? ? ? ? ? ?2,416
repolist: 12,455
用命令直接生成配置文件
yum-config-manager –add-repo= http://172.16.0.1/cobbler/ks_mirror/7/
yum-config-manager –disable “倉庫名” 禁用倉庫
yum-config-manager –enable “倉庫名” 啟用倉庫
yum命令
顯示倉庫列表:
yum repolist [all|enabled|disabled]
顯示程序包:
yum list
yum list [all | glob_exp1] [glob_exp2] […]
yum list {available|installed|updates} [glob_exp1] […]
[root@CENTOS7 yum.repos.d]#yum list *ftp
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Available Packages
ftp.x86_64 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0.17-67.el7 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?base
lftp.i686 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?4.4.8-8.el7_3.2 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?base
lftp.x86_64 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?4.4.8-8.el7_3.2 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?base
tftp.x86_64 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?5.2-13.el7 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? base
升級程序包:
yum update [package1] [package2] […] ? ? 一般不建議使用
yum downgrade package1 [package2] […] (降級)
檢查可用升級:
yum check-update
卸載程序包:
yum remove | erase package1 [package2] […]
查看程序包information: ? 類似于 rpm -qi ?但是沒有rpm -qi詳細
yum info […]
查看指定的特性(可以是某文件)是由哪個程序包所提供: ?類似于rpm -qf
yum provides | whatprovides feature1 [feature2] […]
[root@CENTOS7 yum.repos.d]#yum provides tree
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
tree-1.6.0-10.el7.x86_64 : File system tree viewer
Repo ? ? ? ?: base
tree-1.6.0-10.el7.x86_64 : File system tree viewer
Repo ? ? ? ?: @base
[root@CENTOS7 yum]#yum install mariadb-server ?安裝時有依賴所以同時安裝了下面兩個包
mariadb ? ? ? ? ? ? ? ? ? ? ? x86_64 ? ? ? ? ? ? ? ?1:5.5.56-2.el7 ? ? ? ? ? ? ? ? base ? ? ? ? ? ? ? ?8.7 M
?perl-DBD-MySQL ? ? ? ? ? ? ? ?x86_64 ? ? ? ? ? ? ? ?4.023-5.el7 ? ? ? ? ? ? ? ? ? ?base ? ? ? ? ? ? ? ?140 k
[root@CENTOS7 yum]#yum remove mariadb-server ? 卸載的時候不會卸載這兩個包。
清理本地緩存:
清除/var/cache/yum/$basearch/$releasever緩存
yum clean [ packages | metadata | expire-cache | rpmdb | plugins |
all ]
構建緩存: ? 一幫情況下不需要因為安裝的時候會自動緩存
yum makecache
搜索:yum search string1 [string2] […]
以指定的關鍵字搜索程序包名及summary信息
[root@CENTOS7 yum]#yum search “ftp” ? ?搜索關鍵字忽略大小寫
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
============================================== N/S matched: ftp ==============================================
ftp.x86_64 : The standard UNIX FTP (File Transfer Protocol) client
lftp-scripts.noarch : Scripts for lftp
syslinux-tftpboot.x86_64 : SYSLINUX modules in /var/lib/tftpboot, available for network booting
tftp.x86_64 : The client for the Trivial File Transfer Protocol (TFTP)
tftp-server.x86_64 : The server for the Trivial File Transfer Protocol (TFTP)
vsftpd.x86_64 : Very Secure Ftp Daemon
vsftpd-sysvinit.x86_64 : SysV initscript for vsftpd daemon
curl.x86_64 : A utility for getting files from remote servers (FTP, HTTP, and others)
lftp.i686 : A sophisticated file transfer program
lftp.x86_64 : A sophisticated file transfer program
wget.x86_64 : A utility for retrieving files using the HTTP or FTP protocols
? Name and summary matches only, use “search all” for everything.
查看指定包所依賴的capabilities:
yum deplist package1 [package2] […]
[root@CENTOS7 yum]#yum deplist httpd.x86_64
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
package: httpd.x86_64 2.4.6-67.el7.centos
? dependency: /bin/sh
查看yum事務歷史:
yum history [info|list|packages-list|packages-info|
summary|addon-info|redo|undo|
rollback|new|sync|stats]
yum history
yum history info 6
yum history undo 6
[root@CENTOS7 yum]#yum history
Loaded plugins: fastestmirror, langpacks
ID ? ? | Login user ? ? ? ? ? ? ? | Date and time ? ?| Action(s) ? ? ?| Altered
——————————————————————————-
? ? 12 | wang <wang> ? ? ? ? ? ? ?| 2018-04-20 17:22 | Install ? ? ? ?| ? ?1
? ? 11 | wang <wang> ? ? ? ? ? ? ?| 2018-04-20 17:15 | Erase ? ? ? ? ?| ? ?1
? ? 10 | wang <wang> ? ? ? ? ? ? ?| 2018-04-20 17:13 | Install ? ? ? ?| ? ?3
? ? ?9 | wang <wang> ? ? ? ? ? ? ?| 2018-04-20 16:59 | Reinstall ? ? ?| ? ?1
? ? ?8 | wang <wang> ? ? ? ? ? ? ?| 2018-04-20 16:32 | Install ? ? ? ?| ? ?1
? ? ?7 | wang <wang> ? ? ? ? ? ? ?| 2018-04-20 16:25 | Install ? ? ? ?| ? ?2
? ? ?6 | wang <wang> ? ? ? ? ? ? ?| 2018-04-20 16:07 | Erase ? ? ? ? ?| ? ?1
? ? ?5 | wang <wang> ? ? ? ? ? ? ?| 2018-04-20 15:59 | Install ? ? ? ?| ? ?1 ?<
? ? ?4 | wang <wang> ? ? ? ? ? ? ?| 2018-04-20 15:50 | Install ? ? ? ?| ? ?1 >
? ? ?3 | wang <wang> ? ? ? ? ? ? ?| 2018-04-20 15:44 | Install ? ? ? ?| ? ?6
? ? ?2 | wang <wang> ? ? ? ? ? ? ?| 2018-04-20 15:01 | Install ? ? ? ?| ? ?5 ?<
? ? ?1 | System <unset> ? ? ? ? ? | 2018-03-27 17:54 | Install ? ? ? ?| 1255 >
history list
[root@CENTOS7 yum]#yum history list 12
Loaded plugins: fastestmirror, langpacks
ID ? ? | Command line ? ? ? ? ? ? | Date and time ? ?| Action(s) ? ? ?| Altered
——————————————————————————-
? ? 12 | install mariadb-server ? | 2018-04-20 17:22 | Install ? ? ? ?| ? ?1
history list
[root@CENTOS7 yum]#yum history info 12
Loaded plugins: fastestmirror, langpacks
Transaction ID : 12
Begin time ? ? : Fri Apr 20 17:22:48 2018
Begin rpmdb ? ?: 1274:20aafd54d5e9eb69d75961878f85f43291214f03
End time ? ? ? : ? ? ? ? ? ?17:22:51 2018 (3 seconds)
End rpmdb ? ? ?: 1275:3accf9c7969dd14e1a2dd5b33fed02a300d70ef2
User ? ? ? ? ? : wang <wang>
Return-Code ? ?: Success
Command Line ? : install mariadb-server
Transaction performed with:
? ? Installed ? ? rpm-4.11.3-25.el7.x86_64 ? ? ? ? ? ? ? ? ? ? ?@anaconda
? ? Installed ? ? yum-3.4.3-154.el7.centos.noarch ? ? ? ? ? ? ? @anaconda
? ? Installed ? ? yum-plugin-fastestmirror-1.1.31-42.el7.noarch @anaconda
Packages Altered:
? ? Install mariadb-server-1:5.5.56-2.el7.x86_64 @base
history info
[root@CENTOS7 yum]#yum history undo 10 ? ? 解決卸載殘留問題
Loaded plugins: fastestmirror, langpacks
Undoing transaction 10, from Fri Apr 20 17:13:27 2018
? ? Dep-Install mariadb-1:5.5.56-2.el7.x86_64 ? ? ? ?@base
? ? Install ? ? mariadb-server-1:5.5.56-2.el7.x86_64 @base
? ? Dep-Install perl-DBD-MySQL-4.023-5.el7.x86_64 ? ?@base
Resolving Dependencies
–> Running transaction check
—> Package mariadb.x86_64 1:5.5.56-2.el7 will be erased
—> Package mariadb-server.x86_64 1:5.5.56-2.el7 will be erased
—> Package perl-DBD-MySQL.x86_64 0:4.023-5.el7 will be erased
–> Finished Dependency Resolution
[root@CENTOS7 yum]#yum history redo 10 ? ? ?從新安裝
Loaded plugins: fastestmirror, langpacks
Repeating transaction 10, from Fri Apr 20 17:13:27 2018
? ? Dep-Install mariadb-1:5.5.56-2.el7.x86_64 ? ? ? ?@base
? ? Install ? ? mariadb-server-1:5.5.56-2.el7.x86_64 @base
? ? Dep-Install perl-DBD-MySQL-4.023-5.el7.x86_64 ? ?@base
[root@CENTOS7 yum]#yum install /misc/cd/Packages/httpd-2.4.6-67.el7.centos.x86_64.rpm ? ? 也可以寫路徑安裝同時可以解決依賴性問題
包組管理的相關命令:
yum groupinstall group1 [group2] […]
yum groupupdate group1 [group2] […]
yum grouplist [hidden] [groupwildcard] […]
yum groupremove group1 [group2] […]
yum groupinfo group1 […]
[root@centos yum.repos.d]#cat > base.repo
[base]
name=cdroom
baseurl=file:///misc/cd
gpgcheck=0
[root@centos yum.repos.d]#yum grouplist
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Group Process
Loading mirror speeds from cached hostfile
base ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | 4.0 kB ? ? 00:00 …
base/primary_db ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| 4.7 MB ? ? 00:00 …
Installed Groups:
? ?Additional Development
[root@CENTOS7 yum]#yum groupinfo “Development Tools” ? ?查看包組信息
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Group: Development Tools
?Group-Id: development
?Description: A basic development environment.
?Mandatory Packages:
? ?=autoconf
? ?=automake
? ? binutils
[root@CENTOS7 yum]#yum grouplist ? ?查看能夠安裝的包組
[root@CENTOS7 yum]#yum groupinstall “Development Tools” ?安裝包組
Group: Development Tools
?Group-Id: development
?Description: A basic development environment.
?Mandatory Packages:
? ?=autoconf
? ?=automake
? ? binutils
? ?=bison
? ?=flex
? ? gcc
? ?=gcc-c++
? ? gettext
? ?=libtool
前面什么符號都不帶的表示包已經裝上,而且不是隨著包組裝好的可能是以前裝的或者裝系統的時候裝的
=表示包時隨著包組裝上的
+表示隨著包組升級的時候會安裝
-表示不會隨著包組升級而安裝
[root@CENTOS7 yum]#yum groupremove “Development Tools” ? ?卸載包組但是沒有卸載干凈只卸載了26個還有19個沒有卸載
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
No environment named Development Tools exists
Resolving Dependencies
[root@CENTOS7 yum]#yum history undo 15 ? ? ?卸載剩下的19個包
Loaded plugins: fastestmirror, langpacks
Undoing transaction 15, from Fri Apr 20 17:53:17 2018
? ? Install ? ? autoconf-2.69-11.el7.noarch
yum的命令行選項:
–nogpgcheck:禁止進行gpg check
-y: 自動回答為“yes”
-q:靜默模式
–disablerepo=repoidglob:臨時禁用此處指定的repo
–enablerepo=repoidglob:臨時啟用此處指定的repo
–noplugins:禁用所有插件
[root@CENTOS7 yum]#yum -y -q install httpd ? 自動回答yes并且不顯示安裝過程,-y 與-q 不能寫成-yq
[root@CENTOS7 yum]#rpm -q httpd
httpd-2.4.6-67.el7.centos.x86_64
創建yum倉庫:
createrepo [options] <directory>
如果手動編譯了一個rpm包創建這個rpm包的repodata
creatrepo后面跟上這個rpm包所在的文件夾即可
搭建yum服務器
[root@CENTOS7 ~]#systemctl start httpd ? 啟動
echo welcome to magedu > index.html ? ?頁面顯示magedu
添加一個光盤,光盤是6.9everthing
[root@CENTOS7 html]#echo ‘- – -‘ > /sys/class/scsi_host/host2/scan ? 在不重啟的情況下啟動添加的硬件設備
mount /dev/sr1 centos/6/os/x86_64
在centos6上修改配置文件vim /etc/yum.repos.d/base.repo
修改baseurl=http://172.18.116.150/centos/6/os/x86_64/
保存退出
yum clean all
即可使用這個server了
程序包編譯
程序包編譯安裝:
Application-VERSION-release.src.rpm –> 安裝后,使用rpmbuild命令制作
成二進制格式的rpm包,而后再安裝
源代碼–>預處理–>編譯–>匯編–>鏈接–>執行
源代碼組織格式:
多文件:文件中的代碼之間,很可能存在跨文件依賴關系
C、C++:make 項目管理器
configure腳本 –> Makefile.in –> Makefile
java: maven
configure 后面加路徑和啟用禁用的功能
C語言源代碼編譯安裝三步驟:
1、./configure
(1) 通過選項傳遞參數,指定啟用特性、安裝路徑等;執行時會參考用戶的
指定以及Makefile.in文件生成Makefile
(2) 檢查依賴到的外部環境,如依賴的軟件包
2、make 根據Makefile文件,構建應用程序
3、make install 復制文件到相應路徑
開發工具:
autoconf: 生成configure腳本
automake:生成Makefile.in
注意:安裝前查看INSTALL,README
開源程序源代碼的獲?。?/div>
官方自建站點:
apache.org (ASF:Apache Software Foundation)
mariadb.org
…
代碼托管:
SourceForge.net
Github.com
code.google.com
c/c++編譯器: gcc (GNU C Complier)
準備工作:
1 關閉firwalld
centos6: service iptables stop; chkconfig iptables off
centos7: systemctl stop firewalld; systemctl disable firewalld
2 關閉SElinux
setenforce 0
vim /etc/selinux/config
[root@centos man]#cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# ? ? enforcing – SELinux security policy is enforced.
# ? ? permissive – SELinux prints warnings instead of enforcing.
# ? ? disabled – No SELinux policy is loaded.
SELINUX=enforcing ? ?永久關閉將enforcing 換成disabled
# SELINUXTYPE= can take one of these two values:
# ? ? targeted – Targeted processes are protected,
# ? ? mls – Multi Level Security protection.
SELINUXTYPE=targeted
SELINUX=disabled
先配置好安裝需要的工具yum grouplist “development tool”
然后導入需要安裝的軟件源碼,解壓縮。在進行./configure之前需要進入解壓后的文件夾
cat README 和INSTALL.
[root@centos httpd-2.2.34]#./configure -h
–prefix=PREFIX ? ?指定總文件夾
?–sysconfdir=DIR ? ? ? ?read-only single-machine data [PREFIX/etc] ? 分離出的文件此條是etc
? 最好寫在文檔里不容易出錯
./configure \
–prefix=/app \
–sysconfdir=/etc/httpd22 \
–disable-env \
–enable-file-cache
運行./configure 腳本時,有可能缺少相關的包需要安裝
no OpenSSL headers found
[root@centos httpd-2.2.34]#yum list openssl* ? ?查詢一下
openssl-devel.x86_64 ? ? 一般都是devel的包
?安裝完openssl-devel.x86_64
再次執行
[root@centos httpd-2.2.34]#./configure –prefix=/app –sysconfdir=/etc/httpd22 –enable-ssl
如果沒有問題echo $? = 0
第一步完成
make 需要時間可能很長所以添加cpu個數
[root@centos httpd-2.2.34]#make -j 4 && echo -e “\a” && sleep 1 && echo -e “\a” && sleep 1 && echo -e “\a” && sleep 1
[root@centos httpd-2.2.34]#ll Makefile ?這個文件是運行 ./configure 生成的
-rw-r–r–. 1 root root 8929 Apr 21 20:02 Makefile
第二步完成
[root@centos httpd-2.2.34]#make install
運行程序,直接路徑或者添加PATH變量里的路徑
[root@centos httpd-2.2.34]#^C
[root@centos httpd-2.2.34]#vim /etc/profile.d/env.sh
[root@centos httpd-2.2.34]#. /etc/profile.d/env.sh
[root@centos httpd-2.2.34]#echo $PATH
/app/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos ~]#apachectl start
httpd: apr_sockaddr_info_get() failed for centos.localdomain
httpd: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName
雖然報錯了但是服務已經啟動。80 服務就是APACHE
[root@centos ~]#ss -ntl
State ? ? ?Recv-Q Send-Q ? ? ? ? ? ? ? ? ? ? Local Address:Port ? ? ? ? ? ? ? ? ? ? ? Peer Address:Port
LISTEN ? ? 0 ? ? ?128 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?*:53548 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *:*
LISTEN ? ? 0 ? ? ?128 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? :::42029 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?:::*
LISTEN ? ? 0 ? ? ?128 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? :::111 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?:::*
LISTEN ? ? 0 ? ? ?128 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?*:111 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *:*
LISTEN ? ? 0 ? ? ?128 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? :::80 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? :::*
LISTEN ? ? 0 ? ? ?128 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? :::22 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? :::*
LISTEN ? ? 0 ? ? ?128 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?*:22 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?*:*
LISTEN ? ? 0 ? ? ?128 ? ? ? ? ? ? ? ? ? ? ? ? ? ?127.0.0.1:631 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? *:*
LISTEN ? ? 0 ? ? ?128 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?::1:631 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?:::*
LISTEN ? ? 0 ? ? ?100 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?::1:25 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? :::*
LISTEN ? ? 0 ? ? ?100 ? ? ? ? ? ? ? ? ? ? ? ? ? ?127.0.0.1:25 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?*:*
[root@centos ~]#cd /app
[root@centos app]#ls
bin ?build ?cgi-bin ?error ?htdocs ?icons ?include ?lib ?logs ?man ?manual ?modules
[root@centos app]#cd htdocs/ ? ? 顯示頁面內容在這里
[root@centos htdocs]#ls
index.html
[root@centos httpd22]#ls ? ? 配置文件所在地
extra ?httpd.conf ?magic ?mime.types ?origina
[root@centos app]#ls ? ?man幫助的地方
bin ?build ?cgi-bin ?error ?htdocs ?icons ?include ?lib ?logs ?man ?manual ?modules
[root@centos app]#cd man
[root@centos man]#ls
man1 ?man8
遠程使用寫好的腳本
yum install httpd
/var/www/html/install.sh
systemctl start httpd
curl http://testsrv/install.sh |bash
系統默認的man幫助都在 /usr/share/man
centos7 ?添加man幫助文件編輯/etc/man_db.conf文件
centos6 ?添加man幫助文件編輯/etc/man.config
磁盤存儲和文件系統
LVM ? 邏輯卷
設備類型的文件都在/dev 下放著
設備類型:
塊設備:block,存取單位“塊”,磁盤
字符設備:char,存取單位“字符”,鍵盤
典型的字符文件 /dev/zero 和/dev/null ? ?存取單位是一個字符
設備文件:關聯至一個設備驅動程序,進而能夠跟與之對應硬件設備進行通信
設備號碼:
主設備號:major number, 標識設備類型
次設備號:minor number, 標識同一類型下的不同設備
[root@CENTOS7 ~]#ll /dev/zero
crw-rw-rw-. 1 root root 1主設備號, 5次設備號 Apr 22 08:47 /dev/zero
主號就是這個文件屬于的類別編號,次號這個類別里面有幾個不同的設備
復制設備文件時不可以 cp /dev/sda ?而是 cp -a /dev/sda
創建設備文件 mknod /data/zerofile c 1 5,這個文件和/dev/zero一樣
硬盤接口類型
并行:
IDE:133MB/s
SCSI:640MB/s
串口:
SATA:6Gbps ? B是字節的意思b是位,一個B=8b
SAS:6Gbps
USB:480MB/s
rpm: rotations
per minute
在centos5版本中將IDE接口的硬盤叫做hd
在centos6以上的版本中所有接口的硬盤都叫sd
在centos6中添加一塊IDE接口的硬盤后會變成sda而原來的硬盤會變成sdb,會導致以前寫的配置文件中的sda失效,嚴重的后果可能導致啟動失敗。在配置文件中不要寫設備名
機械硬盤和固態硬盤
?機械硬盤(HDD):Hard Disk Drive,即是傳統普通硬盤,主要由:盤片,磁頭,盤片轉軸及
控制電機,磁頭控制器,數據轉換器,接口,緩存等幾個部分組成。機械硬盤中所有的盤片都
裝在一個旋轉軸上,每張盤片之間是平行的,在每個盤片的存儲面上有一個磁頭,磁頭與盤片
之間的距離比頭發絲的直徑還小,所有的磁頭聯在一個磁頭控制器上,由磁頭控制器負責各個
磁頭的運動。磁頭可沿盤片的半徑方向運動,加上盤片每分鐘幾千轉的高速旋轉,磁頭就可以
定位在盤片的指定位置上進行數據的讀寫操作。數據通過磁頭由電磁流來改變極性方式被電磁
流寫到磁盤上,也可以通過相反方式讀取。硬盤為精密設備,進入硬盤的空氣必須過濾
固態硬盤(SSD):Solid State Drive,用固態電子存儲芯片陣列而制成的硬盤,由控制單元和
存儲單元(FLASH芯片、DRAM芯片)組成。固態硬盤在接口的規范和定義、功能及使用方法
上與普通硬盤的完全相同,在產品外形和尺寸上也與普通硬盤一致
?相較于HDD,SSD在防震抗摔、傳輸速率、功耗、重量、噪音上有明顯優勢,SSD傳輸速率性
能是HDD的2倍
相較于SSD,HDD在價格、容量、使用壽命上占有絕對優勢
?硬盤有價,數據無價,目前SSD不能完全取代HHD ? ? ?機械硬盤損壞數據可能恢復,固態硬盤一但損壞數據恢復的可能性不大。
設備文件
磁盤設備的設備文件命名:/dev/DEV_FILE
SCSI, SATA, SAS, IDE,USB: /dev/sd
虛擬磁盤:/dev/vd
不同磁盤標識:a-z,aa,ab…
/dev/sda, /dev/sdb, …
同一設備上的不同分區:1,2, …
/dev/sda1, /dev/sda5
硬盤存儲術語
head:磁頭 ? ? 有幾個磁頭就有幾個盤面
track:磁道 ? ?最外圈的磁道是0向內圈依次增大
cylinder: 柱面 ?所有的0磁道綜合在一起叫做0柱面
sector: 扇區,512bytes
chs ? ? 磁盤的三維
區位記錄磁盤扇區結構
ZBR(Zoned Bit Recording) ? ?外圈扇區多內圈扇區少
[root@centos ~]#fdisk -l
255 heads, 63 sectors/track, 26108 cylinders
這些數據都是理論上的
真正的硬盤已將不這么使用
沒個磁道有多少個扇區,占用6bit 來存放扇區的個數 ?最多可以存放64個扇區
有多少個磁頭占用8bit ?2的8次方 ?256個
10bit 來存放磁道數 1024
這個存放數據的大?。?12*63*1024*255/1024/1024/1024=8G
CHS和LBA
采用24bit位尋址
其中前10位表示cylinder,中間8位表示head,后面6位表示sector
?最大尋址空間8GB
LBA(logical block addressing
LBA是一個整數,通過轉換成CHS格式完成磁盤具體尋址
LBA采用48個bit位尋址
最大尋址空間128PB
由于CHS尋址方式的尋址空間在大概8GB以內,所以在磁盤容量小于大概8GB
時,可以使用CHS尋址方式或是LBA尋址方式;在磁盤容量大于大概8GB時,則
只能使用LBA尋址方式
硬盤的使用
1.分區
2.創建文件系統(格式化)
3.掛載:分配目錄名
可以直接往硬盤上存數據,但是存放的是2進制
磁盤分區
?為什么分區
?優化I/O性能
?實現磁盤空間配額限制
?提高修復速度
?隔離系統和程序
?安裝多個OS
?采用不同文件系統
分區 ? ? ?分區必須是連續的空間
? 兩種分區方式:MBR,GPT
? MBR: Master Boot Record,1982年,使用32位表示扇區數,分區不超過2T
? 如何分區:按柱面 ? 一個柱面8M,整柱面分區,每個分區都是8M的整數倍
? 0磁道0扇區:512bytes
446bytes: boot loader
64bytes:分區表
16bytes: 標識一個分區
? 2bytes: 55AA
? 4個主分區;3主分區+1擴展(N個邏輯分區)
MBR分區結構
[root@centos ~]#hexdump -C /dev/sda -n 512
00000000 ?eb 48 90 10 8e d0 bc 00 ?b0 b8 00 00 8e d8 8e c0 ?|.H…………..|
00000010 ?fb be 00 7c bf 00 06 b9 ?00 02 f3 a4 ea 21 06 00 ?|…|………!..|
00000020 ?00 be be 07 38 04 75 0b ?83 c6 10 81 fe fe 07 75 ?|….8.u……..u|
00000030 ?f3 eb 16 b4 02 b0 01 bb ?00 7c b2 80 8a 74 03 02 ?|………|…t..|
00000040 ?80 00 00 80 80 13 05 00 ?00 08 fa 90 90 f6 c2 80 ?|…………….|
00000050 ?75 02 b2 80 ea 59 7c 00 ?00 31 c0 8e d8 8e d0 bc ?|u….Y|..1……|
00000060 ?00 20 fb a0 40 7c 3c ff ?74 02 88 c2 52 f6 c2 80 ?|. ..@|<.t…R…|
00000070 ?74 54 b4 41 bb aa 55 cd ?13 5a 52 72 49 81 fb 55 ?|tT.A..U..ZRrI..U|
00000080 ?aa 75 43 a0 41 7c 84 c0 ?75 05 83 e1 01 74 37 66 ?|.uC.A|..u….t7f|
00000090 ?8b 4c 10 be 05 7c c6 44 ?ff 01 66 8b 1e 44 7c c7 ?|.L…|.D..f..D|.|
000000a0 ?04 10 00 c7 44 02 01 00 ?66 89 5c 08 c7 44 06 00 ?|….D…f.\..D..|
000000b0 ?70 66 31 c0 89 44 04 66 ?89 44 0c b4 42 cd 13 72 ?|pf1..D.f.D..B..r|
000000c0 ?05 bb 00 70 eb 7d b4 08 ?cd 13 73 0a f6 c2 80 0f ?|…p.}….s…..|
000000d0 ?84 f0 00 e9 8d 00 be 05 ?7c c6 44 ff 00 66 31 c0 ?|……..|.D..f1.|
000000e0 ?88 f0 40 66 89 44 04 31 ?d2 88 ca c1 e2 02 88 e8 ?|..@f.D.1……..|
000000f0 ?88 f4 40 89 44 08 31 c0 ?88 d0 c0 e8 02 66 89 04 ?|..@.D.1……f..|
00000100 ?66 a1 44 7c 66 31 d2 66 ?f7 34 88 54 0a 66 31 d2 ?|f.D|f1.f.4.T.f1.|
00000110 ?66 f7 74 04 88 54 0b 89 ?44 0c 3b 44 08 7d 3c 8a ?|f.t..T..D.;D.}<.|
00000120 ?54 0d c0 e2 06 8a 4c 0a ?fe c1 08 d1 8a 6c 0c 5a ?|T…..L……l.Z|
00000130 ?8a 74 0b bb 00 70 8e c3 ?31 db b8 01 02 cd 13 72 ?|.t…p..1……r|
00000140 ?2a 8c c3 8e 06 48 7c 60 ?1e b9 00 01 8e db 31 f6 ?|*….H|`……1.|
00000150 ?31 ff fc f3 a5 1f 61 ff ?26 42 7c be 7f 7d e8 40 ?|1…..a.&B|..}.@|
00000160 ?00 eb 0e be 84 7d e8 38 ?00 eb 06 be 8e 7d e8 30 ?|…..}.8…..}.0|
00000170 ?00 be 93 7d e8 2a 00 eb ?fe 47 52 55 42 20 00 47 ?|…}.*…GRUB .G|
00000180 ?65 6f 6d 00 48 61 72 64 ?20 44 69 73 6b 00 52 65 ?|eom.Hard Disk.Re|
00000190 ?61 64 00 20 45 72 72 6f ?72 00 bb 01 00 b4 0e cd ?|ad. Error…….|
000001a0 ?10 ac 3c 00 75 f4 c3 00 ?00 00 00 00 00 00 00 00 ?|..<.u………..|
000001b0 ?00 00 00 00 00 00 00 00 ?6c e7 04 00 00 00 80 20 ?|……..l…… |
000001c0 ?21 00 83 aa 28 82 00 08 ?00 00 00 00 20 00 00 aa ?|!…(……. …|
000001d0 ?29 82 83 fe ff ff 00 08 ?20 00 00 80 1a 06 00 fe ?|)……. …….|
000001e0 ?ff ff 83 fe ff ff 00 88 ?3a 06 00 80 a9 03 00 fe ?|……..:…….|
000001f0 ?ff ff 05 fe ff ff 00 08 ?e4 09 00 f8 1b 0f 55 aa ?|…………..U.|
00000200
一個16進制數占4位,兩個16進制數就是一個字節
這些就是分區表 ? ? 80 20 ?|……..l…… |
000001c0 ?21 00 83 aa 28 82 00 08 ?00 00 00 00 20 00 00 aa ?|!…(……. …|
000001d0 ?29 82 83 fe ff ff 00 08 ?20 00 00 80 1a 06 00 fe ?|)……. …….|
000001e0 ?ff ff 83 fe ff ff 00 88 ?3a 06 00 80 a9 03 00 fe ?|……..:…….|
000001f0 ?ff ff 05 fe ff ff 00 08 ?e4 09 00 f8 1b 0f
如果刪除這64個字節這導致系統無法識別分區,分區上的數據也無法訪問 ? 最好做備份
如果破壞前面的446個字節導致系統無法啟動
[root@centos data]#dd if=/dev/sda of=/data/mbr_backup bs=1 count=521 ? ?備份
521+0 records in
521+0 records out
521 bytes (521 B) copied, 0.00136284 s, 382 kB/s
備份到別的硬盤上
[root@centos ~]#scp /data/mbr_backup ?172.18.116.150:/data
[root@centos ~]#scp /data/mbr_backup ?172.18.116.150:/data
The authenticity of host ‘172.18.116.150 (172.18.116.150)’ can’t be established.
RSA key fingerprint is b0:69:a3:d1:6f:85:a0:48:64:69:6f:36:02:73:51:2c.
Are you sure you want to continue connecting (yes/no)? y
Please type ‘yes’ or ‘no’: yes
Warning: Permanently added ‘172.18.116.150’ (RSA) to the list of known hosts.
root@172.18.116.150’s password:
mbr_backup ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?100% ?521 ? ? 0.5KB/s ? 00:00
[root@centos ~]#dd if=/dev/zero of=/dev/sda bs=1 count=512 ? 破壞硬盤分區數據
512+0 records in
512+0 records out
512 bytes (512 B) copied, 0.000873945 s, 586 kB/s
[root@centos ~]#fdisk -l
fdisk -l 查看的是磁盤的分區情況
Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
lsblk查看的是內存的中存放的分區情況
[root@centos ~]#lsblk
NAME ? MAJ:MIN RM ?SIZE RO TYPE MOUNTPOINT
sr0 ? ? 11:0 ? ?1 ?3.7G ?0 rom ?/media/CentOS_6.9_Final
sda ? ? ?8:0 ? ?0 ?200G ?0 disk
├─sda1 ? 8:1 ? ?0 ? ?1G ?0 part /boot
├─sda2 ? 8:2 ? ?0 48.8G ?0 part /
├─sda3 ? 8:3 ? ?0 29.3G ?0 part /data
├─sda4 ? 8:4 ? ?0 ? ?1K ?0 part
└─sda5 ? 8:5 ? ?0 ? ?2G ?0 part [SWAP]
在沒有關機的情況下恢復
?dd if=/data/mbr_backup of=/dev/sda
如果關機了
機器無法啟動
進入救援模式
進入救援模式后沒有ip地址,需要手動配置
ifconfig ens33 192.168.30.101 ? ?centos7
ping 192.168.30.103 ? ?centos6
scp 192.168.30.103:/data/mbr_backup . ?將centos6中備份的文件拷貝到centos7中, .代表當前文件夾
只要硬盤大小一樣,分區策略一樣。如果沒有備份,也可以拷貝硬盤大小一樣,分區策略一樣的別的機器上的硬盤
可以用下面的命令復制sda的主分區到sdb上,前提sdb要不小于sda,而且只能復制主分區和擴展分區,邏輯分區存放的分區情況不在sda里,所以無法復制邏輯分區
dd if=/dev/sda of=/dev/sdb bs=1 count=512
只想刪除512個字節的最后2個字節,無法使用vim
刪除的方法是 dd if=/dev/zero of=/dev/sda bs=1 count=2 skip=510 seek=510 ? skip 跳過的意思 ? seek尋找的意思
將最后兩個字節刪除后 linux就認為這款硬盤是沒有分區的硬盤
GPT分區
EFI部分又可以分為4個區域:EFI信息區(GPT頭)、分區表、GPT分區、備份區域
GPT:GUID(Globals Unique Identifiers) partition table 支持128個分區,
使用64位,支持8Z( 512Byte/block )64Z ( 4096Byte/block)
使用128位UUID(Universally Unique Identifier) 表示磁盤和分區 GPT分區表
自動備份在頭和尾兩份,并有CRC校驗位
UEFI (統一擴展固件接口)硬件支持GPT,使操作系統啟動
[root@CENTOS7 ~]#cat /etc/fstab ? ?UUID=是16進制數一共128位
#
# /etc/fstab
# Created by anaconda on Tue Mar 27 17:53:47 2018
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=0a1bc23e-06e5-4210-9b32-0edbff09ca1a / ? ? ? ? ? ? ? ? ? ? ? xfs ? ? defaults ? ? ? ?0 0
UUID=4d8d9214-eeed-4758-8c34-f05492b9ea73 /boot ? ? ? ? ? ? ? ? ? xfs ? ? defaults ? ? ? ?0 0
UUID=fb669d84-551f-4a70-a11e-f61deec0fd86 /data ? ? ? ? ? ? ? ? ? xfs ? ? defaults ? ? ? ?0 0
UUID=906330a5-2af1-4bf4-8b2d-9337eaf92250 swap ? ? ? ? ? ? ? ? ? ?swap ? ?defaults ? ? ? ?0 0
列出可用的磁盤設備
? ? 圖形化磁盤管理功能工具:點擊“應用程序”->“系統工
具”->“磁盤”或執行命令gnome-disks
BIOS+MBR與UEFI+GPT ? ? ? ? 機器啟動搭配
開機 →bios初始化→bios自檢→引導操作系統→進入系統
開機 →UEFI初始化→引導操作系統→進入系統
管理分區
列出塊設備
? lsblk
? 創建分區使用:
? fdisk 創建MBR分區
? gdisk 創建GPT分區
? parted 高級分區操作
? partprobe-重新設置內存中的內核分區表版本
parted命令
? parted的操作都是實時生效的,小心使用
? 用法:parted [選項]… [設備 [命令 [參數]…]…]
parted /dev/sdb mklabel gpt|msdos
parted /dev/sdb print
parted /dev/sdb mkpart primary 1 200 (默認M)
parted /dev/sdb rm 1
parted –l 列出分區信息
root@CENTOS7 ~]#parted /dev/sdb mklabel msdos ? ?創建什么形式的分區
Information: You may need to update /etc/fstab.
[root@CENTOS7 ~]#parted /dev/sdb mklabel gpt ? ? ?可以直接修改但是會格式化硬盤
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do
you want to continue?
Yes/No? yes
Information: You may need to update /etc/fstab.
[root@CENTOS7 ~]#parted /dev/sdb mkpart primary 1 1000 ? ? 創建一個主分區
Information: You may need to update /etc/fstab.
[root@CENTOS7 ~]#lsblk
NAME ? MAJ:MIN RM ?SIZE RO TYPE MOUNTPOINT
sda ? ? ?8:0 ? ?0 ?200G ?0 disk
├─sda1 ? 8:1 ? ?0 ? ?1G ?0 part /boot
├─sda2 ? 8:2 ? ?0 ? 50G ?0 part /
├─sda3 ? 8:3 ? ?0 ? 30G ?0 part /data
├─sda4 ? 8:4 ? ?0 ? ?1K ?0 part
└─sda5 ? 8:5 ? ?0 ? ?2G ?0 part [SWAP]
sdb ? ? ?8:16 ? 0 ?100G ?0 disk
└─sdb1 ? 8:17 ? 0 ?953M ?0 part
sdc ? ? ?8:32 ? 0 ? 80G ?0 disk
sdd ? ? ?8:48 ? 0 ? 60G ?0 disk
sr0 ? ? 11:0 ? ?1 ?8.1G ?0 rom ?/run/media/root/CentOS 7 x86_64
[root@CENTOS7 ~]#parted /dev/sdb print ? ?查看當前分區情況
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number ?Start ? End ? ? Size ? File system ?Name ? ? Flags
?1 ? ? ?1049kB ?1000MB ?999MB ? ? ? ? ? ? ? primary
[root@CENTOS7 ~]#parted /dev/sdb mkpart primary 1001 1500
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I
Information: You may need to update /etc/fstab.
[root@CENTOS7 ~]#lsblk
NAME ? MAJ:MIN RM ? SIZE RO TYPE MOUNTPOINT
sda ? ? ?8:0 ? ?0 ? 200G ?0 disk
├─sda1 ? 8:1 ? ?0 ? ? 1G ?0 part /boot
├─sda2 ? 8:2 ? ?0 ? ?50G ?0 part /
├─sda3 ? 8:3 ? ?0 ? ?30G ?0 part /data
├─sda4 ? 8:4 ? ?0 ? ? 1K ?0 part
└─sda5 ? 8:5 ? ?0 ? ? 2G ?0 part [SWAP]
sdb ? ? ?8:16 ? 0 ? 100G ?0 disk
├─sdb1 ? 8:17 ? 0 ? 953M ?0 part
└─sdb2 ? 8:18 ? 0 475.9M ?0 part
sdc ? ? ?8:32 ? 0 ? ?80G ?0 disk
sdd ? ? ?8:48 ? 0 ? ?60G ?0 disk
sr0 ? ? 11:0 ? ?1 ? 8.1G ?0 rom ?/run/media/root/CentOS 7 x86_64
root@CENTOS7 ~]#parted /dev/sdb rm 1
Information: You may need to update /etc/fstab.
[root@CENTOS7 ~]#lsblk
NAME ? MAJ:MIN RM ? SIZE RO TYPE MOUNTPOINT
sda ? ? ?8:0 ? ?0 ? 200G ?0 disk
├─sda1 ? 8:1 ? ?0 ? ? 1G ?0 part /boot
├─sda2 ? 8:2 ? ?0 ? ?50G ?0 part /
├─sda3 ? 8:3 ? ?0 ? ?30G ?0 part /data
├─sda4 ? 8:4 ? ?0 ? ? 1K ?0 part
└─sda5 ? 8:5 ? ?0 ? ? 2G ?0 part [SWAP]
sdb ? ? ?8:16 ? 0 ? 100G ?0 disk
└─sdb2 ? 8:18 ? 0 475.9M ?0 part
sdc ? ? ?8:32 ? 0 ? ?80G ?0 disk
sdd ? ? ?8:48 ? 0 ? ?60G ?0 disk
sr0 ? ? 11:0 ? ?1 ? 8.1G ?0 rom ?/run/media/root/CentOS 7 x86_64
parted 命令是直接生效的,一但執行立即生效,沒有后悔的機會
分區工具fdisk和gdisk
?gdisk /dev/sdb 類fdisk 的GPT分區工具
?fdisk -l [-u] [device…] 查看分區
?fdisk /dev/sdb 管理分區
?子命令:
?p 分區列表
?t 更改分區類型
?n 創建新分區
?d 刪除分區
?v 校驗分區
?u 轉換單位
?w 保存并退出
?q 不保存并退出
[root@CENTOS7 ~]#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.
Command (m for help): m
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 ? ? ? ? ? 給分區加一個標簽,告訴自己這個分區是干什么用的。有8300,8e00等
? ?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): n
Partition type:
? ?p ? primary (0 primary, 0 extended, 4 free)
? ?e ? extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-209715199, default 2048):
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +1G
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): p
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x0003d393
? ?Device Boot ? ? ?Start ? ? ? ? End ? ? ?Blocks ? Id ?System
/dev/sdb1 ? ? ? ? ? ?2048 ? ? 2099199 ? ? 1048576 ? 83 ?Linux
Command (m for help): n
Partition type:
? ?p ? primary (1 primary, 0 extended, 3 free)
? ?e ? extended
Select (default p): e
Partition number (2-4, default 2):
First sector (2099200-209715199, default 2099200): 2099999
Last sector, +sectors or +size{K,M,G} (2099999-209715199, default 209715199): +2G
Partition 2 of type Extended and of size 2 GiB is set
Command (m for help): p
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x0003d393
? ?Device Boot ? ? ?Start ? ? ? ? End ? ? ?Blocks ? Id ?System
/dev/sdb1 ? ? ? ? ? ?2048 ? ? 2099199 ? ? 1048576 ? 83 ?Linux
/dev/sdb2 ? ? ? ? 2099999 ? ? 6293503 ? ? 2096752+ ? 5 ?Extended
Command (m for help): n
Partition type:
? ?p ? primary (1 primary, 1 extended, 2 free)
? ?l ? logical (numbered from 5)
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (2099200-209715199, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-2099998, default 2099998):
Using default value 2099998
Partition 3 of type Linux and of size 399.5 KiB is set
Command (m for help): p
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x0003d393
? ?Device Boot ? ? ?Start ? ? ? ? End ? ? ?Blocks ? Id ?System
/dev/sdb1 ? ? ? ? ? ?2048 ? ? 2099199 ? ? 1048576 ? 83 ?Linux
/dev/sdb2 ? ? ? ? 2099999 ? ? 6293503 ? ? 2096752+ ? 5 ?Extended
/dev/sdb3 ? ? ? ? 2099200 ? ? 2099998 ? ? ? ? 399+ ?83 ?Linux
Partition table entries are not in disk order
Command (m for help): d
Partition number (1-3, default 3): 3
Partition 3 is deleted
Command (m for help): n
Partition type:
? ?p ? primary (1 primary, 1 extended, 2 free)
? ?l ? logical (numbered from 5)
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (2099200-209715199, default 2099200): 6293504
Last sector, +sectors or +size{K,M,G} (6293504-209715199, default 209715199): +2G
Partition 3 of type Linux and of size 2 GiB is set
Command (m for help): p
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x0003d393
? ?Device Boot ? ? ?Start ? ? ? ? End ? ? ?Blocks ? Id ?System
/dev/sdb1 ? ? ? ? ? ?2048 ? ? 2099199 ? ? 1048576 ? 83 ?Linux
/dev/sdb2 ? ? ? ? 2099999 ? ? 6293503 ? ? 2096752+ ? 5 ?Extended
/dev/sdb3 ? ? ? ? 6293504 ? ?10487807 ? ? 2097152 ? 83 ?Linux
Command (m for help): n
Partition type:
? ?p ? primary (2 primary, 1 extended, 1 free)
? ?l ? logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (2102047-6293503, default 2103296):
Using default value 2103296
Last sector, +sectors or +size{K,M,G} (2103296-6293503, default 6293503): 500M
Value out of range.
Last sector, +sectors or +size{K,M,G} (2103296-6293503, default 6293503): +500M
Partition 5 of type Linux and of size 500 MiB is set
Command (m for help): p
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x0003d393
? ?Device Boot ? ? ?Start ? ? ? ? End ? ? ?Blocks ? Id ?System
/dev/sdb1 ? ? ? ? ? ?2048 ? ? 2099199 ? ? 1048576 ? 83 ?Linux
/dev/sdb2 ? ? ? ? 2099999 ? ? 6293503 ? ? 2096752+ ? 5 ?Extended
/dev/sdb3 ? ? ? ? 6293504 ? ?10487807 ? ? 2097152 ? 83 ?Linux
/dev/sdb5 ? ? ? ? 2103296 ? ? 3127295 ? ? ?512000 ? 83 ?Linux
? Device Boot ? ? ?Start ? ? ? ? End ? ? ?Blocks ? Id ?System
/dev/sdb1 ? ? ? ? ? ?2048 ? ? 2099199 ? ? 1048576 ? 83 ?Linux
/dev/sdb2 ? ? ? ? 2099200 ? ? 6293503 ? ? 2097152 ? ?5 ?Extended
/dev/sdb3 ? ? ? ? 6293504 ? ?10487807 ? ? 2097152 ? 83 ?Linux
/dev/sdb5 ? ? ? ? 2101248 ? ? 3125247 ? ? ?512000 ? 83 ?Linux
/dev/sdb6 ? ? ? ? 3127296 ? ? 3536895 ? ? ?204800 ? 83 ?Linux
Command (m for help): d
Partition number (1-3,5,6, default 6): 5
Partition 5 is deleted
Command (m for help): p
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x0003d393
? ?Device Boot ? ? ?Start ? ? ? ? End ? ? ?Blocks ? Id ?System
/dev/sdb1 ? ? ? ? ? ?2048 ? ? 2099199 ? ? 1048576 ? 83 ?Linux
/dev/sdb2 ? ? ? ? 2099200 ? ? 6293503 ? ? 2097152 ? ?5 ?Extended
/dev/sdb3 ? ? ? ? 6293504 ? ?10487807 ? ? 2097152 ? 83 ?Linux
/dev/sdb5 ? ? ? ? 3127296 ? ? 3536895 ? ? ?204800 ? 83 ?Linux
Command (m for help): w ? ?保存退出 ?, ?re-read重新讀part表寫入內存
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
在使用了一段時間的硬盤上添加或者刪除分區很可能導致磁盤和內存不同步
查看lsblk 和fdisk -l 的結果是否一樣,如果不一樣則需要
在centos7上輸入命令 partprobe
在centos6上添加分區同步命令是partx -a /dev/sda
刪除分區同步命令式 ?partx -d –nr 6-8 /dev/sda
如果又增加又刪除了分區最好在增加完時同步一次后再刪除分區后再同步
從新啟動也可以解決不同步問題
[root@CENTOS7 ~]#fdisk /dev/sda
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.
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (174069760-419430399, default 174069760):
Using default value 174069760
Last sector, +sectors or +size{K,M,G} (174069760-419430399, default 419430399): +1G
Partition 6 of type Linux and of size 1 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@CENTOS7 ~]#fdisk -l /dev/sda
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000b3918
? ?Device Boot ? ? ?Start ? ? ? ? End ? ? ?Blocks ? Id ?System
/dev/sda1 ? * ? ? ? ?2048 ? ? 2099199 ? ? 1048576 ? 83 ?Linux
/dev/sda2 ? ? ? ? 2099200 ? 106956799 ? ?52428800 ? 83 ?Linux
/dev/sda3 ? ? ? 106956800 ? 169871359 ? ?31457280 ? 83 ?Linux
/dev/sda4 ? ? ? 169871360 ? 419430399 ? 124779520 ? ?5 ?Extended
/dev/sda5 ? ? ? 169873408 ? 174067711 ? ? 2097152 ? 82 ?Linux swap / Solaris
/dev/sda6 ? ? ? 174069760 ? 176166911 ? ? 1048576 ? 83 ?Linux
[root@CENTOS7 ~]#lsblk
NAME ? MAJ:MIN RM ?SIZE RO TYPE MOUNTPOINT
sda ? ? ?8:0 ? ?0 ?200G ?0 disk
├─sda1 ? 8:1 ? ?0 ? ?1G ?0 part /boot
├─sda2 ? 8:2 ? ?0 ? 50G ?0 part /
├─sda3 ? 8:3 ? ?0 ? 30G ?0 part /data
├─sda4 ? 8:4 ? ?0 ?512B ?0 part
└─sda5 ? 8:5 ? ?0 ? ?2G ?0 part [SWAP]
sdb ? ? ?8:16 ? 0 ?100G ?0 disk
├─sdb1 ? 8:17 ? 0 ? ?1G ?0 part
├─sdb2 ? 8:18 ? 0 ? ?1K ?0 part
├─sdb3 ? 8:19 ? 0 ? ?2G ?0 part
└─sdb5 ? 8:21 ? 0 ?200M ?0 part
sdc ? ? ?8:32 ? 0 ? 80G ?0 disk
sdd ? ? ?8:48 ? 0 ? 60G ?0 disk
sr0 ? ? 11:0 ? ?1 ?8.1G ?0 rom ?/run/media/root/CentOS 7 x86_64
[root@CENTOS7 ~]#cat /proc/partitions
major minor ?#blocks ?name
? 11 ? ? ? ?0 ? ?8491008 sr0
? ?8 ? ? ? ?0 ?209715200 sda
? ?8 ? ? ? ?1 ? ?1048576 sda1
? ?8 ? ? ? ?2 ? 52428800 sda2
? ?8 ? ? ? ?3 ? 31457280 sda3
? ?8 ? ? ? ?4 ? ? ? ? ?0 sda4
? ?8 ? ? ? ?5 ? ?2097152 sda5
? ?8 ? ? ? 32 ? 83886080 sdc
? ?8 ? ? ? 16 ?104857600 sdb
? ?8 ? ? ? 17 ? ?1048576 sdb1
? ?8 ? ? ? 18 ? ? ? ? ?1 sdb2
? ?8 ? ? ? 19 ? ?2097152 sdb3
? ?8 ? ? ? 21 ? ? 204800 sdb5
? ?8 ? ? ? 48 ? 62914560 sdd
[root@CENTOS7 ~]#ls /dev/sda*
/dev/sda ?/dev/sda1 ?/dev/sda2 ?/dev/sda3 ?/dev/sda4 ?/dev/sda5
[root@CENTOS7 ~]#partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). ?/dev/sr0 has been opened read-only.
[root@CENTOS7 ~]#ls /dev/sda*
/dev/sda ?/dev/sda1 ?/dev/sda2 ?/dev/sda3 ?/dev/sda4 ?/dev/sda5 ?/dev/sda6
文件系統
文件系統是操作系統用于明確存儲設備或分區上的文件的方法和數據結構;即
? ?在存儲設備上組織文件的方法。操作系統中負責管理和存儲文件信息的軟件結
? ?構稱為文件管理系統,簡稱文件系統
從系統角度來看,文件系統是對文件存儲設備的空間進行組織和分配,負責文
? ?件存儲并對存入的文件進行保護和檢索的系統。具體地說,它負責為用戶建立
? ?文件,存入、讀出、修改、轉儲文件,控制文件的存取,安全控制,日志,壓
? ?縮,加密等
? ?支持的文件系統:/lib/modules/`uname –r`/kernel/fs
[root@centos ~]#ls /lib/modules/2.6.32-696.el6.x86_64/kernel/fs ?系統支持的文件類型
autofs4 ? ? cifs ? ? ?dlm ? ? ? ext2 ?fat ? ? ?gfs2 ?jffs2 ? ? ? nfs ? ? ? ? nls ? ? ? udf
btrfs ? ? ? configfs ?ecryptfs ?ext3 ?fscache ?jbd ? lockd ? ? ? nfs_common ?squashfs ?xfs
cachefiles ?cramfs ? ?exportfs ?ext4 ?fuse ? ? jbd2 ?mbcache.ko ?nfsd ? ? ? ?ubifs
[root@centos ~]#df -T ? ? centos6
Filesystem ? ? Type ? ?1K-blocks ? ?Used Available Use% Mounted on
/dev/sda2 ? ? ?ext4 ? ? 50264772 5514216 ?42190556 ?12% /
tmpfs ? ? ? ? ?tmpfs ? ? ?502056 ? ? ? 8 ? ?502048 ? 1% /dev/shm
/dev/sda1 ? ? ?ext4 ? ? ? 999320 ? 34944 ? ?911948 ? 4% /boot
/dev/sda3 ? ? ?ext4 ? ? 30106576 ? 44988 ?28525588 ? 1% /data
/dev/sr0 ? ? ? iso9660 ? 3878870 3878870 ? ? ? ? 0 100% /media/CentOS_6.9_Final
[root@CENTOS7 ~]#df -T ?centos7
Filesystem ? ? Type ? ? 1K-blocks ? ?Used Available Use% Mounted on
/dev/sda2 ? ? ?xfs ? ? ? 52403200 3636732 ?48766468 ? 7% /
devtmpfs ? ? ? devtmpfs ? ?535296 ? ? ? 0 ? ?535296 ? 0% /dev
tmpfs ? ? ? ? ?tmpfs ? ? ? 550036 ? ? ? 0 ? ?550036 ? 0% /dev/shm
tmpfs ? ? ? ? ?tmpfs ? ? ? 550036 ? ?7948 ? ?542088 ? 2% /run
tmpfs ? ? ? ? ?tmpfs ? ? ? 550036 ? ? ? 0 ? ?550036 ? 0% /sys/fs/cgroup
/dev/sda3 ? ? ?xfs ? ? ? 31441920 ? 33084 ?31408836 ? 1% /data
/dev/sda1 ? ? ?xfs ? ? ? ?1038336 ?161620 ? ?876716 ?16% /boot
tmpfs ? ? ? ? ?tmpfs ? ? ? 110008 ? ? ?24 ? ?109984 ? 1% /run/user/0
/dev/sr0 ? ? ? iso9660 ? ?8490330 8490330 ? ? ? ? 0 100% /run/media/root/CentOS 7 x86_64
tmpfs ? ? ? ? ?tmpfs ? ? ? 110008 ? ? ? 0 ? ?110008 ? 0% /run/user/1000
文件系統類型
? Linux文件系統:ext2(Extended file system), ext3, ext4, xfs(SGI), btrfs
?(Oracle), reiserfs, jfs(AIX), swap
? 光盤:iso9660
? Windows:FAT32不支持4G以上的文件, exFAT,NTFS
? Unix: FFS(fast), UFS(unix), JFS2
? 網絡文件系統:NFS, CIFS
? 集群文件系統:GFS2, OCFS2(oracle)
? 分布式文件系統: fastdfs,ceph, moosefs, mogilefs, glusterfs, Lustre
? RAW:未經處理或者未經格式化產生的文件系統
? 文件系統分類
? ? 根據其是否支持”journal”功能: ?日志的作用,修改一個文件的時候,先讀到內存中在內存中修改,修改完成后再傳到磁盤中,在這個過程中可能斷電導致文件損壞.有日志就是在內存中修改完后先傳給日志,之后在傳給內存
? ? 日志型文件系統: ext3, ext4, xfs, …
? ? 非日志型文件系統: ext2, vfat
? ? 文件系統的組成部分:
? ? 內核中的模塊:ext4, xfs, vfat
? ? 用戶空間的管理工具:mkfs.ext4, mkfs.xfs,mkfs.vfat
? ? Linux的虛擬文件系統:VFS ? 平時使用命令時是命令與VFS打交道然后再由VFS和真正的文件系統打交道
? ? 查前支持的文件系統:cat /proc/filesystems
root@CENTOS7 ~]#ls /lib/modules/3.10.0-693.el7.x86_64/kernel/fs
binfmt_misc.ko.xz ?ceph ? ?dlm ? ?fat ? ? ?gfs2 ? lockd ? ? ? ? ?nfs_common ?overlayfs ?udf
btrfs ? ? ? ? ? ? ?cifs ? ?exofs ?fscache ?isofs ?mbcache.ko.xz ?nfsd ? ? ? ?pstore ? ? xfs
cachefiles ? ? ? ? cramfs ?ext4 ? fuse ? ? jbd2 ? nfs ? ? ? ? ? ?nls ? ? ? ? squashfs
[root@CENTOS7 ~]#cd /lib/modules/3.10.0-693.el7.x86_64/kernel/fs
[root@CENTOS7 fs]#cd ext4
[root@CENTOS7 ext4]#ls
ext4.ko.xz ? ? 這個文件就是ext4的驅動
[root@centos ~]#free
? ? ? ? ? ? ?total ? ? ? used ? ? ? free ? ? shared ? ?buffers ? ? cached
Mem: ? ? ? 1004112 ? ? 517428 ? ? 486684 ? ? ? 1856 ? ? ?51480 ? ? 168932
-/+ buffers/cache: ? ? 297016 ? ? 707096
Swap: ? ? ?2097148 ? ? ? ? ?0 ? ?2097148
buffers ? ? 寫修改文件的時候先放到buffers中緩沖,等修改完成后再寫入到硬盤中
cached ? ? ?讀文件到內存中的時候是先將文件放在硬盤cache中緩存在讀取到內存中
如果一個文件系統時ext4當有多個服務器(節點)同時訪問時會導致文件損壞,這時候需要集群文件系統GFS2
創建文件系統
? mkfs命令:
? (1) mkfs.FS_TYPE /dev/DEVICE
? ext4
? xfs
? btrfs
? vfat
? (2) mkfs -t FS_TYPE /dev/DEVICE
? -L ‘LABEL’: 設定卷標
分文件系統前要先查詢次分區是否已經有文件系統
[root@CENTOS7 ~]#blkid /dev/sdb1 ? ?沒有
[root@CENTOS7 ~]#blkid /dev/sda1 ? ?有文件系統時xfs
/dev/sda1: UUID=”4d8d9214-eeed-4758-8c34-f05492b9ea73″ TYPE=”xfs”
UUID:當你創建一個文件系統之后,操作系統自動分配一個UUID
也可以手動生成 ?命令是 uuidgen ? ?128位重復的概率太小,所以可以用UUID代替設備名
[root@CENTOS7 ~]#mkfs -t ext4 /dev/sdb1 ? ?將/dev/sdb1創建成ext4的文件系統
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@CENTOS7 ~]#blkid /dev/sdb1 ? ?查看是否創建ext4成功
/dev/sdb1: UUID=”c837ab44-edcb-43af-b4fa-c26832a7af7e” TYPE=”ext4″
[root@CENTOS7 ~]#df -T ? ?目前無法查看的原因是還沒有掛載,df -T 只能查看掛載的文件系統
Filesystem ? ? Type ? ? 1K-blocks ? ?Used Available Use% Mounted on
/dev/sda2 ? ? ?xfs ? ? ? 52403200 3636716 ?48766484 ? 7% /
devtmpfs ? ? ? devtmpfs ? ?535296 ? ? ? 0 ? ?535296 ? 0% /dev
tmpfs ? ? ? ? ?tmpfs ? ? ? 550036 ? ? ? 0 ? ?550036 ? 0% /dev/shm
tmpfs ? ? ? ? ?tmpfs ? ? ? 550036 ? ?7956 ? ?542080 ? 2% /run
tmpfs ? ? ? ? ?tmpfs ? ? ? 550036 ? ? ? 0 ? ?550036 ? 0% /sys/fs/cgroup
/dev/sda3 ? ? ?xfs ? ? ? 31441920 ? 33084 ?31408836 ? 1% /data
/dev/sda1 ? ? ?xfs ? ? ? ?1038336 ?161620 ? ?876716 ?16% /boot
tmpfs ? ? ? ? ?tmpfs ? ? ? 110008 ? ? ?24 ? ?109984 ? 1% /run/user/0
/dev/sr0 ? ? ? iso9660 ? ?8490330 8490330 ? ? ? ? 0 100% /run/media/root/CentOS 7 x86_64
tmpfs ? ? ? ? ?tmpfs ? ? ? 110008 ? ? ? 0 ? ?110008 ? 0% /run/user/1000
[root@CENTOS7 ~]#blkid ? ? ?查看所有的文件系統 ? ?centos7上的會顯示光盤
/dev/sda1: UUID=”4d8d9214-eeed-4758-8c34-f05492b9ea73″ TYPE=”xfs”
/dev/sdb1: UUID=”c837ab44-edcb-43af-b4fa-c26832a7af7e” TYPE=”ext4″
/dev/sr0: UUID=”2017-09-06-10-53-42-00″ LABEL=”CentOS 7 x86_64″ TYPE=”iso9660″ PTTYPE=”dos”
/dev/sda2: UUID=”0a1bc23e-06e5-4210-9b32-0edbff09ca1a” TYPE=”xfs”
/dev/sda3: UUID=”fb669d84-551f-4a70-a11e-f61deec0fd86″ TYPE=”xfs”
/dev/sda5: UUID=”906330a5-2af1-4bf4-8b2d-9337eaf92250″ TYPE=”swap”
在centos6上如果用 blkid 是不顯示光盤的,想要查看命令是blkid /dev/sr0
[root@CENTOS7 ~]#mkfs.vfat /dev/sdb3 ? ?用mkfs.按兩次Tab鍵可以顯示系統支持的所有文件系統
mkfs.fat 3.0.20 (12 Jun 2013)
[root@CENTOS7 ~]#blkid
/dev/sda1: UUID=”4d8d9214-eeed-4758-8c34-f05492b9ea73″ TYPE=”xfs”
/dev/sdb1: UUID=”c837ab44-edcb-43af-b4fa-c26832a7af7e” TYPE=”ext4″
/dev/sr0: UUID=”2017-09-06-10-53-42-00″ LABEL=”CentOS 7 x86_64″ TYPE=”iso9660″ PTTYPE=”dos”
/dev/sda2: UUID=”0a1bc23e-06e5-4210-9b32-0edbff09ca1a” TYPE=”xfs”
/dev/sda3: UUID=”fb669d84-551f-4a70-a11e-f61deec0fd86″ TYPE=”xfs”
/dev/sda5: UUID=”906330a5-2af1-4bf4-8b2d-9337eaf92250″ TYPE=”swap”
/dev/sdb3: UUID=”79AD-713B” TYPE=”vfat” ? ?老舊 的文件系統所以uuid很短
[root@CENTOS7 ~]#mkfs.ext2 /dev/sdb3 ? ? 換成ext2的
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
[root@CENTOS7 ~]#blkid
/dev/sda1: UUID=”4d8d9214-eeed-4758-8c34-f05492b9ea73″ TYPE=”xfs”
/dev/sdb1: UUID=”c837ab44-edcb-43af-b4fa-c26832a7af7e” TYPE=”ext4″
/dev/sr0: UUID=”2017-09-06-10-53-42-00″ LABEL=”CentOS 7 x86_64″ TYPE=”iso9660″ PTTYPE=”dos”
/dev/sda2: UUID=”0a1bc23e-06e5-4210-9b32-0edbff09ca1a” TYPE=”xfs”
/dev/sda3: UUID=”fb669d84-551f-4a70-a11e-f61deec0fd86″ TYPE=”xfs”
/dev/sda5: UUID=”906330a5-2af1-4bf4-8b2d-9337eaf92250″ TYPE=”swap”
/dev/sdb3: UUID=”8eeacb99-2062-4604-89c5-6518d832bec8″ TYPE=”ext2″
tune2fs -l ? ?只能查看ext系列的文件系統
這是ext2的文件系統
[root@CENTOS7 ~]#tune2fs -l /dev/sdb3 ? ? 查看文件系統
tune2fs 1.42.9 (28-Dec-2013)
Filesystem volume name: ? <none>
Last mounted on: ? ? ? ? ?<not available>
Filesystem UUID: ? ? ? ? ?8eeacb99-2062-4604-89c5-6518d832bec8
Filesystem magic number: ?0xEF53
Filesystem revision #: ? ?1 (dynamic)
Filesystem features: ? ? ?ext_attr resize_inode dir_index filetype sparse_super large_file ? 不支持日志
Filesystem flags: ? ? ? ? signed_directory_hash
Default mount options: ? ?user_xattr acl
Filesystem state: ? ? ? ? clean
Errors behavior: ? ? ? ? ?Continue
Filesystem OS type: ? ? ? Linux
Inode count: ? ? ? ? ? ? ?131072
Block count: ? ? ? ? ? ? ?524288
Reserved block count: ? ? 26214
Free blocks: ? ? ? ? ? ? ?515284
Free inodes: ? ? ? ? ? ? ?131061
First block: ? ? ? ? ? ? ?0
Block size: ? ? ? ? ? ? ? 4096
Fragment size: ? ? ? ? ? ?4096
Reserved GDT blocks: ? ? ?127
Blocks per group: ? ? ? ? 32768
Fragments per group: ? ? ?32768
Inodes per group: ? ? ? ? 8192
Inode blocks per group: ? 512
Filesystem created: ? ? ? Sun Apr 22 17:38:34 2018
Last mount time: ? ? ? ? ?n/a
Last write time: ? ? ? ? ?Sun Apr 22 17:38:34 2018
Mount count: ? ? ? ? ? ? ?0
Maximum mount count: ? ? ?-1
Last checked: ? ? ? ? ? ? Sun Apr 22 17:38:34 2018
Check interval: ? ? ? ? ? 0 (<none>)
Reserved blocks uid: ? ? ?0 (user root)
Reserved blocks gid: ? ? ?0 (group root)
First inode: ? ? ? ? ? ? ?11
Inode size: ? ? ? ? ?256
Required extra isize: ? ? 28
Desired extra isize: ? ? ?28
Default directory hash: ? half_md4
Directory Hash Seed: ? ? ?b917dba2-cf04-412e-8c76-02bf6c2d27cd
?這是ext4的文件系統
[root@CENTOS7 ~]#tune2fs -l /dev/sdb1
tune2fs 1.42.9 (28-Dec-2013)
Filesystem volume name: ? <none>
Last mounted on: ? ? ? ? ?<not available>
Filesystem UUID: ? ? ? ? ?c837ab44-edcb-43af-b4fa-c26832a7af7e
Filesystem magic number: ?0xEF53
Filesystem revision #: ? ?1 (dynamic)
Filesystem features: ? ? ?has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: ? ? ? ? signed_directory_hash
Default mount options: ? ?user_xattr acl
Filesystem state: ? ? ? ? clean
Errors behavior: ? ? ? ? ?Continue
Filesystem OS type: ? ? ? Linux
Inode count: ? ? ? ? ? ? ?65536
Block count: ? ? ? ? ? ? ?262144
Reserved block count: ? ? 13107
Free blocks: ? ? ? ? ? ? ?249189
Free inodes: ? ? ? ? ? ? ?65525
First block: ? ? ? ? ? ? ?0
Block size: ? ? ? ? ? ? ? 4096
Fragment size: ? ? ? ? ? ?4096
Group descriptor size: ? ?64
Reserved GDT blocks: ? ? ?127
Blocks per group: ? ? ? ? 32768
Fragments per group: ? ? ?32768
Inodes per group: ? ? ? ? 8192
Inode blocks per group: ? 512
Flex block group size: ? ?16
Filesystem created: ? ? ? Sun Apr 22 17:29:14 2018
Last mount time: ? ? ? ? ?n/a
Last write time: ? ? ? ? ?Sun Apr 22 17:29:14 2018
Mount count: ? ? ? ? ? ? ?0
Maximum mount count: ? ? ?-1
Last checked: ? ? ? ? ? ? Sun Apr 22 17:29:14 2018
Check interval: ? ? ? ? ? 0 (<none>)
Lifetime writes: ? ? ? ? ?33 MB
Reserved blocks uid: ? ? ?0 (user root)
Reserved blocks gid: ? ? ?0 (group root)
First inode: ? ? ? ? ? ? ?11
Inode size: ? ? ? ? ?256
Required extra isize: ? ? 28
Desired extra isize: ? ? ?28
Journal inode: ? ? ? ? ? ?8
Default directory hash: ? half_md4
Directory Hash Seed: ? ? ?34c92d7d-7e86-42af-90d4-9664aa2daec9
Journal backup: ? ? ? ? ? inode blocks
在centos7上創建文件系統時默認是有acl權限的
Default mount options: ? ?user_xattr acl ? ?在centos6上手動創建的文件系統是Default mount options:<none>這時你對文件加acl權限會報錯
添加acl命令 ? ?tune2fs -o acl /dev/sdb1
Filesystem state: ? ? ? ? clean ? ? 說明文件系統沒有問題,如果有問題回顯是NO clean這時需要修復命令是fsck
[root@CENTOS7 ~]#fsck /dev/sdb1
fsck from util-linux 2.23.2
e2fsck 1.42.9 (28-Dec-2013)
/dev/sdb1: clean, 11/65536 files, 12955/262144 blocks
贊 (0)