1. 磁碟管理
1.1 查看磁碟分割資訊(修改分區)
方法1: 查看 /proc/partitions 檔案
[root@localhost TestLabs]# cat /proc/partitions
major minor #blocks name
8 0 67108864 sda
8 1 512000 sda1
8 2 66595840 sda2
253 0 31985664 dm-0
253 1 2064384 dm-1
253 2 32542720 dm-2
方法2: 執行命令 fdisk -l
[root@localhost TestLabs]# fdisk -l
Disk /dev/sda: 68.7 GB, 68719476736 bytes
255 heads, 63 sectors/track, 8354 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00043e44
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 8355 66595840 8e Linux LVM
修改分區可以使用 fdisk [分區名稱] 進入互動介面進行磁碟分割管理。如下圖所示:
[root@localhost TestLabs]# fdisk /dev/sda
The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
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
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):
1.2 建立檔案系統
使用命令 mke2fs -t [要建立的檔案系統類型] [磁碟]
[root@localhost TestLabs]# mke2fs -t ext4 /dev/sda3
常用參數
-b blocksize 指定檔案系統塊大小
-c 建立的時候檢查壞塊
-L 指定卷標
-j 建立檔案系統日誌
1.3 磁碟掛載
掛載磁碟,使用mount [要掛載的分區] [掛在點]命令。
直接使用mount命令將會列出已經掛載的分區。
常用參數:
-t 指定檔案系統的類型
-o 指定掛載選項(ro[唯讀],rw[讀寫],sync[無緩衝,直寫],async[緩衝,預設],noatime[每次訪問不更新檔案訪問時間],atime[每次訪問更新檔案訪問時間],remount[重新掛載])
#mount -o remount,sync,noatime /dev/sdb1 /mnt/
使用命令 umount [檔案系統/掛載點] 卸載已經掛載的檔案系統。
使用命令 lsof 查看開啟了那些檔案,一般可以在無法卸載磁碟的時候用於查看已經開啟的檔案,後面跟上磁碟,可以列出該磁碟上哪些檔案開啟了。
或者 如果無法卸載,提示檔案正在被使用,可以使用fuser -m /mnt 查看檔案系統的進程。
如果要實現自動掛載,可以修改 /etc/fstab檔案實現。該檔案中新增一條記錄就可以了,檔案行結構如下所示:
/dev/sda3 /mnt ext4 defaults 0 0
需要掛載的裝置 掛載點 檔案系統 掛載選項 dump,fsck相關選項
使用mount -a 會掛載fstab檔案中的所有自動掛載項
2. 使用者及許可權基礎
2.1 使用者基礎
使用者分為三類:
root使用者,id為0的使用者
系統使用者,沒有shell(/sbin/nologin or /bin/false),id範圍為1-499
普通使用者,id為500+
使用id可以查看目前使用者的資訊
使用者設定檔為/etc/passwd,該檔案儲存了所有使用者的基本資料,該檔案所有使用者均可以訪問,因此,密碼並不在該檔案中存放,使用者的密碼存放在/etc/shadow檔案中,該檔案只有root使用者可以訪問。
另一個設定檔 /etc/group 為使用者組的設定檔,包含了組的資訊。
查看目前使用者whoami, who, w , 命令越長,顯示的內容越少。
localhost:bash mylxsw$ whoami
mylxsw
localhost:bash mylxsw$ who
mylxsw console May 27 20:50
mylxsw ttys000 May 28 21:39
localhost:bash mylxsw$ w
22:20 up 1 day, 1:31, 2 users, load averages: 2.22 1.82 1.68
USER TTY FROM LOGIN@ IDLE WHAT
mylxsw console - 二20 25:29 -
mylxsw s000 - 21:39 - w
2.1.1 添加新使用者
添加新使用者使用命令useradd,如下
[root@localhost mylxsw]# useradd aicode
[root@localhost mylxsw]# cat /etc/passwd
root:x:0:0:管宜堯:/root:/bin/bash
...
mylxsw:x:500:500::/home/mylxsw:/bin/bash
aicode:x:501:501::/home/aicode:/bin/bash
以上命令執行後會添加一個新使用者,使用者名稱為aicode,可以看到,該命令在/etc/passwd中添加了一條使用者資訊記錄。
該命令會為使用者建立一個home目錄,在/home/使用者名稱,還會為使用者建立一個使用者組,改組名與使用者名稱稱相同,同時,會把/etc/skel目錄下的檔案複製到使用者目錄中。(/etc/skel目錄為使用者初始化目錄,如果需在建立使用者時初始化一些使用者資訊,可以在該目錄中修改,類似於模板)
常用參數:
-d指定使用者home目錄
-s使用者登陸shell
-u指定使用者id
-g指定使用者的主組
-G指定使用者的附屬組(最多31個,用“,”分隔)
2.1.2 修改使用者資訊
通過命令usermod [參數] [使用者名稱] 修改使用者資訊。
常用參數:
-l新的使用者id
-d指定使用者home目錄
-s使用者登陸shell
-u指定使用者id
-g指定使用者的主組
-G指定使用者的附屬組(最多31個,用“,”分隔)
-L鎖定使用者,使其不能登陸
-U解除對使用者的鎖定
2.1.3 刪除使用者
使用命令userdel [使用者名稱]刪除使用者。
刪除使用者操作不會刪除使用者的home目錄,如果要刪除home目錄,需要增加-r參數。
2.1.4 組的建立、修改、刪除
建立組 groupadd [組名]
修改組名 groupmod -n [新組名] [舊組名]
修改組id groupmod -g [新組id] [舊組id]
刪除群組 groupdel [組名]
Linux操作技巧收集
使用pushd和popd命令快速切換目錄
經常會有這麼一種情況,我們會在不同目錄中進行頻繁的切換,如果目錄很深,那麼使用cd命令的工作量是不小的,這時可以使用pushd和popd命令快速切換目錄。
$ pwd
/Users/mylxsw/codes/php/lecloud/api
$ pushd .
~/codes/php/lecloud/api ~/codes/php/lecloud/api
$ cd ../album/
$ pwd
/Users/mylxsw/codes/php/lecloud/album
$ popd
~/codes/php/lecloud/api
$ pwd
/Users/mylxsw/codes/php/lecloud/api
建立軟串連的時候出現Too many levels of symbolic links的錯誤資訊
建立軟串連的時候使用絕對路徑就可以解決,不要使用相對路徑。
localhost:~ mylxsw$ ln -s /Users/mylxsw/其它/app /Users/mylxsw/app
使用SCP命令在伺服器和本機電腦之間傳遞檔案
usage: scp [-12346BCEpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program]
[[user@]host1:]file1 ... [[user@]host2:]file2
從伺服器下載檔案
scp username@伺服器位址:/path/檔案名稱 本地儲存路徑
上傳檔案到伺服器
scp 本地檔案路徑 username@伺服器位址:/儲存到伺服器的路徑
Tip: 如果要操作的對象是整個目錄的話,需要添加-t參數。
使用範例:
localhost:Downloads mylxsw$ scp guanyy@10.10.10.10:/home/guanyy/download.db ./
guanyy@10.10.10.10's password:
download.db 100% 25MB 24.7MB/s 00:01
Mac OS 清理DNS緩衝
dscacheutil -flushcache
Cent OS伺服器安裝PHP的pecl
想要安裝某個PHP擴充,但探索服務器上沒有pecl,因此需要安裝pecl。
$ sudo yum install php-pear
安裝pear之後,pecl就有了。
在目前的目錄下尋找大小超過100MB的檔案
find . -type f -size +100M
拷貝鏡像到隨身碟
dd if=yourimage.img of=/dev/sdb1
使用基於文本的圖形介面配置命令setup
安裝setup命令
$ sudo yum install setuptool
安裝之後,需要安裝一些常見的系統配置組件,也是使用yum:
$ sudo yum install system-config-services system-config-firewall system-config-network-tui
更多的配置組件可以使用yum search system-config命令查看,進入配置介面直接使用setup命令。