Linux入門操作總結


一、配置虛擬機NAT網絡連接

查看vmware的NAT網絡默認配置

vmwarestation軟件點擊 上方“編輯”,選擇“虛擬網絡編輯器”
找到vmnet10,是NAT模式,子網地址是172.25.2.0,子網掩碼是:255.255.255.0.
再點擊“NAT設置”,可看到它的網關:172.25.2.2

配置當前虛擬機的網絡配置

vmwarestation軟件點擊 上方“虛擬機”,選擇“設置”;
首先沒有NAT網卡的,點擊“添加”,添加一個新的網絡適配器。
已經創建號NAT網卡的,進行如下配置:
網絡連接模式改為:"自定義(U):特定虛擬網絡"
在下拉選項中,選中“VMnet10(NAT模式)”

配置linux網絡

# 進入網絡配置目錄
cd /etc/sysconfig/network-scripts
# 編輯nat網卡的配置
vi ifcfg-ens33
# 修改或添加如下信息:
BOOTPROTO=static
ONBOOT=yes
IPADDR=172.25.2.101
NETMASK=255.255.255.0
GATEWAY=172.25.2.2
DNS=8.8.8.8

# 在vi編輯器中,輸入:wq進行保存退出
vi /etc/resolv.conf
加入:
nameserver 8.8.8.8
輸入:wq進行保存退出

# 關閉防火牆
systemctl stop firewalld.service
systemctl disable firewalld.service

# 重啟網絡
systemctl restart network

# 測試
ping www.baidu.com

二、yum源配置

1、關閉網絡管理、selinux、防火牆

# 關閉網絡管理
systemctl disable NetworkManager
# 關閉selinux,修改文件為disabled
sed  -i 's/SELINUX=enforcing/SELINUX=disabled/g'  /etc/selinux/config
# selinux修改生效
setenforce 0

2、更新yum源

# 將官方的yum源備份到一個新目錄:
mkdir /etc/yum.repos.d/repo.bak/
mv /etc/yum.repos.d/*.repo repo.bak/

# 更新下載阿里源
curl -o /etc/yum.repos.d/CentOS-Base.repo  http://mirrors.aliyun.com/repo/Centos-7.repo

# 清理yum
yum clean all
yum makecache

# 更新yum信息
yum update -y

3、安裝lrzsz上傳下載

# 安裝lrzsz
 yum install -y lrzsz
# 上傳
 rz
# 安裝vim
 yum install -y vim
yum install -y vsftpd

三、FTP配置和管理

1、安裝vsftpd

(1)查詢vsftpd是否安裝

# 查詢vsftpd是否安裝
rpm -q vsftpd
rpm -q openssh
# 如果提示:package vsftpd is not installed,說明沒有安裝。

#  查詢 Linux 系統中所有已安裝軟件包的命令
rpm -qa

# 管道符查找出需要的內容
rpm -qa| grep httpd
rpm -qa | grep openssh

# 查詢軟件包的詳細信息
rpm -qi httpd
rpm -qi openssh

(2)安裝vsftpd

yum install -y vsftpd
yum install -y ftp
rpm -qa| grep vsftpd

(3)關閉防火牆

setenforce 0;
iptables -F;
iptables -X;
iptables -Z;
systemctl stop firewalld;
systemctl disable firewalld;

2、vsftpd服務控制

(1)控制服務

# 重啟服務
systemctl restart vsftpd
# 關閉服務
systemctl stop vsftpd
# 開啟服務
systemctl start vsftpd
# 查看服務
systemctl status vsftpd

(2)服務開機設置

# 開機啟動vsftpd
systemctl enable vsftpd
# 開機不啟動vsftpd
systemctl disable vsftpd 

四、vim和命令行快捷鍵

1、命令行快捷鍵

ctrl+左右鍵:在單詞之間跳轉
ctrl+a:跳到本行的行首
ctrl+e:跳到頁尾
Ctrl+u:刪除當前光標前面的文字 (還有剪切功能)
ctrl+k:刪除當前光標后面的文字(還有剪切功能)
Ctrl+L:進行清屏操作
Ctrl+y:粘貼Ctrl+u或ctrl+k剪切的內容
Ctrl+w:刪除光標前面的單詞的字符
Alt – d :由光標位置開始,往右刪除單詞。往行尾刪

說明
Ctrl – k: 先按住 Ctrl 鍵,然后再按 k 鍵;
Alt – k: 先按住 Alt 鍵,然后再按 k 鍵;
M – k:先單擊 Esc 鍵,然后再按 k 鍵。

移動光標
Ctrl – a :移到行首
Ctrl – e :移到行尾
Ctrl – b :往回(左)移動一個字符
Ctrl – f :往后(右)移動一個字符
Alt – b :往回(左)移動一個單詞
Alt – f :往后(右)移動一個單詞
Ctrl – xx :在命令行尾和光標之間移動
M-b :往回(左)移動一個單詞
M-f :往后(右)移動一個單詞

編輯命令
Ctrl – h :刪除光標左方位置的字符
Ctrl – d :刪除光標右方位置的字符(注意:當前命令行沒有任何字符時,會注銷系統或結束終端)
Ctrl – w :由光標位置開始,往左刪除單詞。往行首刪
Alt – d :由光標位置開始,往右刪除單詞。往行尾刪
M – d :由光標位置開始,刪除單詞,直到該單詞結束。
Ctrl – k :由光標所在位置開始,刪除右方所有的字符,直到該行結束。
Ctrl – u :由光標所在位置開始,刪除左方所有的字符,直到該行開始。
Ctrl – y :粘貼之前刪除的內容到光標后。
ctrl – t :交換光標處和之前兩個字符的位置。
Alt + . :使用上一條命令的最后一個參數。
Ctrl – _ :回復之前的狀態。撤銷操作。
Ctrl -a + Ctrl -k 或 Ctrl -e + Ctrl -u 或 Ctrl -k + Ctrl -u 組合可刪除整行。

Bang(!)命令
!! :執行上一條命令。
^foo^bar :把上一條命令里的foo替換為bar,並執行。
!wget :執行最近的以wget開頭的命令。
!wget:p :僅打印最近的以wget開頭的命令,不執行。
!$ :上一條命令的最后一個參數, 與 Alt - . 和 $_ 相同。
!* :上一條命令的所有參數
!*:p :打印上一條命令是所有參數,也即 !*的內容。
^abc :刪除上一條命令中的abc。
^foo^bar :將上一條命令中的 foo 替換為 bar
^foo^bar^ :將上一條命令中的 foo 替換為 bar
!-n :執行前n條命令,執行上一條命令: !-1, 執行前5條命令的格式是: !-5

查找歷史命令
Ctrl – p :顯示當前命令的上一條歷史命令
Ctrl – n :顯示當前命令的下一條歷史命令
Ctrl – r :搜索歷史命令,隨着輸入會顯示歷史命令中的一條匹配命令,Enter鍵執行匹配命令;ESC鍵在命令行顯示而不執行匹配命令。
Ctrl – g :從歷史搜索模式(Ctrl – r)退出。

控制命令
Ctrl – l :清除屏幕,然后,在最上面重新顯示目前光標所在的這一行的內容。
Ctrl – o :執行當前命令,並選擇上一條命令。
Ctrl – s :阻止屏幕輸出
Ctrl – q :允許屏幕輸出
Ctrl – c :終止命令
Ctrl – z :掛起命令

重復執行操作動作
M – 操作次數 操作動作 : 指定操作次數,重復執行指定的操作。

五、配置與管理磁盤

1、fdisk:磁盤分區工具

fdisk   /dev/sdb
# 執行如上命令后進入fdisk磁盤分區操作
# a:調整磁盤啟動分區
# d:刪除分區
   Command (m for help): d
   Partition number (1-4, default 4): 4
   Partition 4 is deleted
# l:列出所有支持的分區類型
   Command (m for help): l
   0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
   1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
   2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-  .....

# m:列出所有命令和介紹
   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 system id
      u   change display/entry units
      v   verify the partition table
      w   write table to disk and exit
      x   extra functionality (experts only)

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

# p:列出硬盤分區表
   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: 0x7bded33b

      Device Boot      Start         End      Blocks   Id  System
   /dev/sdb1            2048    10487807     5242880   83  Linux
   /dev/sdb2        10487808    20973567     5242880   83  Linux
# q:不保存更改,退出fdisk命令
   Command (m for help): q
   [root@localhost ~]# 

# t:更改分區類型
   Command (m for help): t
   Partition number (1-3, default 3): 3
   Hex code (type L to list all codes): 82
   Changed type of partition 'Linux' to 'Linux swap / Solaris'

   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: 0x7bded33b

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

# u:切換所顯示的分區大小的單位
   Command (m for help): u
   Changing display/entry units to cylinders (DEPRECATED!).

   Command (m for help): p

   Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
   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 label type: dos
   Disk identifier: 0x7bded33b

      Device Boot      Start         End      Blocks   Id  System
   /dev/sdb1               1         653     5242880   83  Linux
   /dev/sdb2             653        1306     5242880   83  Linux

   Command (m for help): u
   Changing display/entry units to sectors.

   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: 0x7bded33b

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

# w:把修改寫入硬盤分區表,再退出
   Command (m for help): w
   The partition table has been altered!

   Calling ioctl() to re-read partition table.
   Syncing disks.

# x:列出高級選項
   Command (m for help): x
   Expert command (m for help): m
   Command action
      b   move beginning of data in a partition
      c   change number of cylinders
      d   print the raw data in the partition table
      e   list extended partitions
      f   fix partition order
      g   create an IRIX (SGI) partition table
      h   change number of heads
      i   change the disk identifier
      m   print this menu
      p   print the partition table
      q   quit without saving changes
      r   return to main menu
      s   change number of sectors/track
      v   verify the partition table
      w   write table to disk and exit

2、mkfs:建立文件系統

硬盤分區后,下一步的工作就是建立文件系統。建立文件系統的命令是
mkfs [參數] 文件系統

命令常用的參數選項如下:

-t:指定要創建的文件系統類型。比如 ext3(centos5) ;ext4(centos6);xfs(centos7) 。如不指定默認施ext2。
-c:建立文件系統前首先檢查壞塊。
-l file:從文件file中讀磁盤壞塊列表,file文件一般是由磁盤壞塊檢查程序產生的。
-V:輸出建立文件系統詳細信息。

注意:直接格式化擴展分區是不允許的,只能格式化主分區和邏輯分區。如果要格式化一個擴展分區,要在擴展分區上創建邏輯分區.

# 查看分區情況
[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 19.5G  0 part 
  ├─centos-root 253:0    0 17.5G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 
├─sdb1            8:17   0    5G  0 part 
└─sdb2            8:18   0    5G  0 part 
sr0              11:0    1 1024M  0 rom  
# 建立ext4文件系統,建立時檢查磁盤壞塊並顯示詳細信息
[root@localhost ~]# mkfs -t ext4 -V -c /dev/sdb1
mkfs from util-linux 2.23.2
mkfs.ext4 -c /dev/sdb1 
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
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736

Checking for bad blocks (read-only test): done                                                 
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

# 格式化為xfs格式
# 需要加-f原因:提示這個分區已有ext4文件系統;需要使用-f選項強制覆蓋。
[root@localhost ~]# mkfs -t xfs -V -f /dev/sdb1
mkfs from util-linux 2.23.2
mkfs.xfs -f /dev/sdb1 
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
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

# 掛載操作
mkdir /newFS
mount /dev/sdb1 /mnt
mount /dev/sdb2 /newFS
df -H

# 卸載掛載
umount /mnt
umount /newFS
df -H

3、fsck:檢查文件系統的正確性,並對磁盤進行修復

fsck(英文全拼:file system check)命令用於檢查與修復 Linux 檔案系統,可以同時檢查一個或多個 Linux 檔案系統。

# 語法:fsck  [參數選項]  文件系統
-t : 給定檔案系統的型式,若在 /etc/fstab 中已有定義或 kernel 本身已支援的則不需加上此參數
-s : 依序一個一個地執行 fsck 的指令來檢查
-A : 對/etc/fstab 中所有列出來的 partition 做檢查
-C : 顯示完整的檢查進度
-d : 列印 e2fsck 的 debug 結果
-p : 同時有 -A 條件時,同時有多個 fsck 的檢查一起執行
-R : 同時有 -A 條件時,省略 / 不檢查
-V : 詳細顯示模式
-a : 如果檢查有錯則自動修復
-r : 如果檢查有錯則由使用者回答是否修復

# 必須先把磁盤卸載才能檢查分區
[root@localhost /]# fsck -a /dev/sdb1
fsck from util-linux 2.23.2
/sbin/fsck.xfs: XFS file system.
[root@localhost /]# fsck -a /dev/sdb2
fsck from util-linux 2.23.2
/dev/sdb2: clean, 11/327680 files, 58462/1310720 blocks

4、dd:建立和使用交換文件。

dd:用指定大小的塊拷貝一個文件,並在拷貝的同時進行指定的轉換
使用dd命令建立和使用交換文件。當系統的交換分區不能滿足系統的要求而磁盤上又沒有可用空間時,可以使用交換文件提供虛擬內存。

# 查看交換空間方法
# 查看swap交換空間方式一:free -m
[root@localhost dev]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1985         101        1455           8         428        1729
Swap:          2303           0        2303
# 查看swap交換空間方式二:grep SwapTotal /proc/meminfo
[root@localhost dev]# grep SwapTotal /proc/meminfo 
SwapTotal:       2359288 kB
# 查看swap交換空間方式三:cat /proc/swaps
[root@localhost dev]# cat /proc/swaps              
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2097148 0       -1
/swapfile                               file            262140  0       -2

# dd命令創建交換文件
# 在硬盤的根目錄下建立了一個塊大小為1 024字節、塊數為262144的名為swapfile的交換文件。該文件的大小為256MB
[root@localhost dev]# dd  if=/dev/zero  of=/swapfile  bs=1024  count=262144
10240+0 records in
10240+0 records out
10485760 bytes (10 MB) copied, 0.0824385 s, 127 MB/s

# mkswap命令說明文件用於交換空間
[root@localhost dev]# mkswap /swapfile
Setting up swapspace version 1, size = 10236 KiB
no label, UUID=1c15b10a-a536-40f0-9d5e-4ea88cfddc65

# swapon激活交換空間
[root@localhost dev]# swapon /swapfile
swapon: /swap: insecure permissions 0644, 0600 suggested.

# swapoff卸載被激活的交換空間
[root@localhost dev]# swapoff /swapfile

目前紅帽官方推薦交換分區的大小應當與系統物理內存的大小保持線性比例關系,不過在小於2GB物理內存的系統中,交換分區大小應該設置為內存大小的兩倍,如果內存大小多於2GB,交換分區大小應該是物理內存大小加上2GB。其原因在於,系統中的物理內存越大, 對於內存的負荷可能也越大。但是,如果物理內存大小擴展到數百GB,這樣做就沒什么意義了,大家說對吧!

實際上,系統中交換分區的大小並不取決於物理內存的量,而是取決於系統中內存的負荷。Red Hat Enterprise Linux 可以在這樣的情況下工作:完全沒有交換分區,而且系統中匿名內存頁和共享內存頁小於3/4的物理內存量。在這種情況下,系統會將匿名內存頁和共享內存頁鎖定在物理內存中,而使用剩余的物理內存來緩沖文件系統數據(pagecache),當內存耗盡時,系統內核只會回收利用這些pagecache內存。

物理內存 交換分區(SWAP)
<2G 至少2G
2~4G 至少4G
4~16G 至少8G
16G~64G 至少16G
64G~256G 至少32G

5、df:查看文件系統的磁盤空間占用情況。

df命令用來查看文件系統的磁盤空間占用情況。

  • 可以利用該命令來獲取硬盤被占用了多少空間,以及目前還有多少空間等信息。
  • 還可以利用該命令獲得文件系統的掛載位。
# df命令的常見參數選項如下。
-a:顯示所有文件系統磁盤使用情況,包括0塊的文件系統,如/proc文件系統。
-k:以k字節為單位顯示。
-i:顯示i節點信息。
-t:顯示各指定類型的文件系統的磁盤空間使用情況。
-x:列出不是某一指定類型文件系統的磁盤空間使用情況(與t選項相反)。
-T:顯示文件系統類型

[root@localhost ~]# df -i
Filesystem               Inodes IUsed   IFree IUse% Mounted on
/dev/mapper/centos-root 8910848 26331 8884517    1% /
devtmpfs                 121677   384  121293    1% /dev
tmpfs                    124744     1  124743    1% /dev/shm
tmpfs                    124744   720  124024    1% /run
tmpfs                    124744    16  124728    1% /sys/fs/cgroup
/dev/sda1                524288   326  523962    1% /boot
tmpfs                    124744     1  124743    1% /run/user/0
[root@localhost ~]# df -k
Filesystem              1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root  17811456 1032708  16778748   6% /
devtmpfs                   486708       0    486708   0% /dev
tmpfs                      498976       0    498976   0% /dev/shm
tmpfs                      498976    7792    491184   2% /run
tmpfs                      498976       0    498976   0% /sys/fs/cgroup
/dev/sda1                 1038336  132472    905864  13% /boot
tmpfs                       99796       0     99796   0% /run/user/0
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G 1009M   17G   6% /
devtmpfs                 476M     0  476M   0% /dev
tmpfs                    488M     0  488M   0% /dev/shm
tmpfs                    488M  7.7M  480M   2% /run
tmpfs                    488M     0  488M   0% /sys/fs/cgroup
/dev/sda1               1014M  130M  885M  13% /boot
tmpfs                     98M     0   98M   0% /run/user/0
[root@localhost ~]# df -t xfs
Filesystem              1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root  17811456 1032708  16778748   6% /
/dev/sda1                 1038336  132472    905864  13% /boot
[root@localhost ~]# df -t tmpfs
Filesystem     1K-blocks  Used Available Use% Mounted on
tmpfs             498976     0    498976   0% /dev/shm
tmpfs             498976  7792    491184   2% /run
tmpfs             498976     0    498976   0% /sys/fs/cgroup
tmpfs              99796     0     99796   0% /run/user/0

[root@localhost ~]# df -T -h
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        17G 1009M   17G   6% /
devtmpfs                devtmpfs  476M     0  476M   0% /dev
tmpfs                   tmpfs     488M     0  488M   0% /dev/shm
tmpfs                   tmpfs     488M  7.7M  480M   2% /run
tmpfs                   tmpfs     488M     0  488M   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  130M  885M  13% /boot
tmpfs                   tmpfs      98M     0   98M   0% /run/user/0

6、du:查看磁盤空間的使用情況。

du命令用於顯示磁盤空間的使用情況。該命令逐級顯示指定目錄的每一級子目錄占用文件系統數據塊的情況。 du命令的語法如下:
du [參數選項] [文件或目錄名稱]

du命令的參數選項:
-s:對每個name 參數只給出占用的數據塊總數。
-a:遞歸顯示指定目錄中各文件及子目錄中各文件占用的數據塊數。
-b:以字節為單位列出磁盤空間使用情況(AS 4.0中默認以KB為單位)。
-k:以1024字節為單位列出磁盤空間使用情況。-h:按照K\M\G展示
-c:在統計后加上一個總計(系統默認設置total)。
-l:計算所有文件大小,對硬鏈接文件重復計算。
-x:跳過在不同文件系統上的目錄,不予統計。

[root@localhost sysconfig]# du -b
133	./cbq
6	./console
6	./modules
158923	./network-scripts
177031	.
[root@localhost sysconfig]# du -k
8	./cbq
0	./console
0	./modules
232	./network-scripts
324	.
[root@localhost sysconfig]# du -h
8.0K	./cbq
0	./console
0	./modules
232K	./network-scripts
324K	.
[root@localhost sysconfig]# du -c
8	./cbq
0	./console
0	./modules
232	./network-scripts
324	.
324	total

[root@localhost /]# du -xsh
964M	.
[root@localhost /]# du -sh
du: cannot access ‘./proc/1941/task/1941/fd/4’: No such file or directory
du: cannot access ‘./proc/1941/task/1941/fdinfo/4’: No such file or directory
du: cannot access ‘./proc/1941/fd/3’: No such file or directory
du: cannot access ‘./proc/1941/fdinfo/3’: No such file or directory
1.1G	.

7、mount/umount

  1. 在磁盤上建立好文件系統之后,還需要把新建立的文件系統掛載到系統上才能使用,這個過程稱為掛載。
  • 文件系統所掛載到的目錄被稱為掛載點(mount point)。
  • Linux系統中提供了/mnt和/media兩個專門的掛載點。
  • 掛載點應該是一個空目錄,否則目錄中原來的文件將被系統隱藏。
  1. 卸載文件系統的命令是umount。umount 命令的格式為

umount 設備 掛載點

# 掛載磁盤
mount /dev/sdb2 /swift/node

# 掛載本地iso文件到空目錄
mount -o loop CentOS-7-x86_64-DVD-1511.iso /mnt/
umount /mnt
mount -o loop /root/XianDian-IaaS-v2.2.iso /mnt
# 解除掛載
umount /mnt

# 掛載光盤
[root@localhost media]# mount -t iso9660 /dev/cdrom /media/cdrom
mount: /dev/sr0 is write-protected, mounting read-only
[root@localhost media]# du -sh *
4.2G	cdrom
# 解除掛載
[root@localhost media]# umount /media/cdrom
  1. /etc/fstab文件系統的自動掛載
    如果要實現每次開機自動掛載文件系統,可以通過編輯/etc/fstab文件來實現。
    /etc/fstab文件的每一行代表一個文件系統,每一行又包含6列,這6列的內容如下所示。

fs_spec:將要掛載的設備文件。
fs_file:文件系統的掛載點。
fs_vfstype:文件系統類型。
fs_mntops:掛載選項,決定傳遞給mount命令時如何掛載,各選項之間用逗號隔開。
fs_freq:由dump程序決定文件系統是否需要備份,0表示不備份,1表示備份。
fs_passno:由fsck程序決定引導時是否檢查磁盤以及檢查次序,取值可以為0、1、2。

[root@compute bin]# cat /etc/fstab 
# /etc/fstab
# Created by anaconda on Mon Sep 13 16:48:20 2021
#
# 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=f3d08635-77c9-4e68-9fac-a3e23df311c8 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb2 /swift/node xfs loop,noatime,nodiratime,nobarrier,logbufs=8 0 0

六、LVM邏輯卷管理器

LVM(Logical Volume Manager,邏輯卷管理器)可以允許用戶對硬盤資源進行動態調整。

  1. LVM技術是在硬盤分區和文件系統之間添加一個邏輯層,提供了一個抽象的卷組,可以把多塊硬盤進行卷組合並。
  2. 用戶無須關心物理硬盤設備的底層架構和布局,就可以實現對硬盤分區的動態調整。

1、常用命令

功能/命令 物理卷管理 卷組管理 邏輯卷管理
掃描 pvscan vgscan lvscan
建立 pvcreate vgcreate lvcreate
顯示 pvdisplay vgdisplay lvdisplay
刪除 pvremove vgremove lvremove
擴展 -- vgextend lvextend
縮小 -- vgreduce lvreduce

2、部署邏輯卷

# 讓新添加的兩塊硬盤設備支持LVM
[root@localhost ~]# pvcreate /dev/sdb /dev/sdc
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc" successfully created.

# 兩塊硬盤設備加入到storage卷組
[root@localhost ~]# vgcreate storage-hqs /dev/sdb /dev/sdc 
  Volume group "storage-hqs" successfully created

# 查看卷組的狀態
[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               storage-hqs
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               19.99 GiB
  PE Size               4.00 MiB
  Total PE              5118
  Alloc PE / Size       0 / 0   
  Free  PE / Size       5118 / 19.99 GiB
  VG UUID               qVIk5C-AbPD-NH0i-OMHJ-MRMQ-yAmu-QRwYq1
   
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <9.00 GiB
  PE Size               4.00 MiB
  Total PE              2303
  Alloc PE / Size       2303 / <9.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               JqLsRj-HHfi-0n1y-QBua-gIA5-WVo6-l8ayBT

# 創建一個大小為2GB的邏輯卷hqs_test_lvm
[root@localhost ~]# lvcreate -L 2G -n hqs_test_lvm storage-hqs
  Logical volume "hqs_test_lvm" created.

# 查看邏輯卷狀態
[root@localhost ~]# lvdisplay        #  系統會有兩個默認的LV:root、swap
  --- Logical volume ---
  LV Path                /dev/storage-hqs/hqs_test_lvm
  LV Name                hqs_test_lvm
  VG Name                storage-hqs
  LV UUID                npsiLb-bdXO-m0kz-pfLX-UTsf-A9Om-TcnyfT
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2021-11-12 02:27:46 -0500
  LV Status              available
  # open                 0
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

# 查看lv和pv的簡略信息
[root@localhost ~]# pvs
  PV         VG          Fmt  Attr PSize   PFree  
  /dev/sda2  centos      lvm2 a--   <9.00g      0 
  /dev/sdb   storage-hqs lvm2 a--  <10.00g  <8.00g
  /dev/sdc   storage-hqs lvm2 a--  <10.00g <10.00g
[root@localhost ~]# lvs
  LV           VG          Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root         centos      -wi-ao---- <8.00g                                                    
  swap         centos      -wi-ao----  1.00g                                                    
  hqs_test_lvm storage-hqs -wi-a-----  2.00g 

# 把生成好的邏輯卷進行格式化,然后掛載使用
[root@localhost ~]# mkfs.xfs /dev/storage-hqs/hqs_test_lvm 
meta-data=/dev/storage-hqs/hqs_test_lvm isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=524288, 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
[root@localhost ~]# mount /dev/storage-hqs/hqs_test_lvm /mnt/

# 查看掛載狀態
[root@localhost ~]# lsblk
NAME                        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                           8:0    0   10G  0 disk 
├─sda1                        8:1    0    1G  0 part /boot
└─sda2                        8:2    0    9G  0 part 
  ├─centos-root             253:0    0    8G  0 lvm  /
  └─centos-swap             253:1    0    1G  0 lvm  [SWAP]
sdb                           8:16   0   10G  0 disk 
└─storage--hqs-hqs_test_lvm 253:2    0    2G  0 lvm  /mnt
sdc                           8:32   0   10G  0 disk 
sr0                          11:0    1 1024M  0 rom  
[root@localhost ~]# df -H -T
Filesystem                            Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root               xfs       8.6G  1.1G  7.6G  13% /
devtmpfs                              devtmpfs  499M     0  499M   0% /dev
tmpfs                                 tmpfs     511M     0  511M   0% /dev/shm
tmpfs                                 tmpfs     511M  8.0M  503M   2% /run
tmpfs                                 tmpfs     511M     0  511M   0% /sys/fs/cgroup
/dev/sda1                             xfs       1.1G  136M  928M  13% /boot
tmpfs                                 tmpfs     103M     0  103M   0% /run/user/0
/dev/mapper/storage--hqs-hqs_test_lvm xfs       2.2G   34M  2.2G   2% /mnt

3、擴容邏輯卷

# 擴容前一定要卸載設備和掛載點的關聯
umount /mnt/

# 添加新的物理卷到卷組
# 注意添加一個新的磁盤,重啟虛擬機
# 添加/dev/sdd支持LVM
[root@localhost ~]# pvcreate /dev/sdd 
  Physical volume "/dev/sdd" successfully created
# 將sdd添加到storage-hqs卷組
[root@localhost ~]# vgextend storage-hqs /dev/sdd 
  Volume group "storage-hqs" successfully extended
# 查看卷組改變情況
[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               storage-hqs
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               29.99 GiB
  PE Size               4.00 MiB
  Total PE              7677
  Alloc PE / Size       512 / 2.00 GiB
  Free  PE / Size       7165 / 27.99 GiB
  VG UUID               rKNq2A-ABFq-NWZL-OPyC-3Fda-JdC6-beeZ9A

# 將上面的邏輯卷擴展到12G
[root@localhost ~]# lvextend -L 12G /dev/storage-hqs/hqs-test-lvm 
  Size of logical volume storage-hqs/hqs-test-lvm changed from 2.00 GiB (512 extents) to 12.00 GiB (3072 extents).
  Logical volume hqs-test-lvm successfully resized.

# 檢查硬盤完整性 
[root@localhost mnt]# e2fsck -f /dev/storage-hqs/hqs-test-lvm 
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/storage-hqs/hqs-test-lvm: 14/786432 files (0.0% non-contiguous), 93230/3145728 blocks

# 重置硬盤容量
[root@localhost mnt]# resize2fs /dev/storage-hqs/hqs-test-lvm 
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/storage-hqs/hqs-test-lvm to 12582912 (1k) blocks.
The filesystem on /dev/storage-hqs/hqs-test-lvm is now 12582912 blocks long.

# 重新掛載並查看文件系統狀態
[root@localhost mnt]# mount /dev/storage-hqs/hqs-test-lvm /mnt
[root@localhost mnt]# df -H -T
Filesystem                              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root                 xfs        19G  1.5G   18G   8% /
devtmpfs                                devtmpfs  1.1G     0  1.1G   0% /dev
tmpfs                                   tmpfs     1.1G     0  1.1G   0% /dev/shm
tmpfs                                   tmpfs     1.1G  9.0M  1.1G   1% /run
tmpfs                                   tmpfs     1.1G     0  1.1G   0% /sys/fs/cgroup
/dev/sda1                               xfs       521M  113M  409M  22% /boot
tmpfs                                   tmpfs     209M     0  209M   0% /run/user/0
/dev/mapper/storage--hqs-hqs--test--lvm ext4       14G   46M   13G   1% /mnt

七、配置和管理apache服務

HTTP(Hypertext Transfer Protocol,超文本傳輸協議)可以算得上是目前國際互聯網基礎上的一個重要組成部分。而Apache、IIS服務器是HTTP協議的服務器軟件,微軟的Internet Explorer和Mozilla的Firefox則是HTTP協議的客戶端實現。

1、安裝和啟動

# 安裝
yum install -y httpd

# 啟動
systemctl start httpd     # 訪問當前虛擬機地址加端口 : 192.168.37.101:80
# 關閉   
systemctl stop httpd      # 訪問當前虛擬機地址加端口 : 192.168.37.101:80
# 查看
systemctl status httpd
# 重啟
systemctl restart httpd   # 訪問當前虛擬機地址加端口 : 192.168.37.101:80

# 關閉selinux,修改文件為disabled
sed  -i 's/SELINUX=enforcing/SELINUX=disabled/g'  /etc/selinux/config
# selinux修改生效
setenforce 0
# 查看是否生效
getenforce   # 輸出permissive

# 關閉防火牆
systemctl stop firewalld
# 開機不啟動防火牆
systemctl disable  firewalld

2、apache配置文件

Linux系統中的配置文件及存放位置如下表:

配置文件名 存放位置
服務目錄 /etc/httpd
主配置文件 /etc/httpd/conf/httpd.conf
網站數據目錄 /var/www/html
訪問日志 /var/log/httpd/access_log
錯誤日志 /var/log/httpd/error_log

(1)主配置文件httpd.conf

httpd.conf文件不區分大小寫,在該文件中以“#”開始的行為注釋行。除了注釋和空行外,服務器把其他的行認為是完整的或部分的指令。指令又分為類似於shell的命令和偽HTML標記。
指令的語法為“配置參數名稱 參數值”。偽HTML標記的語法格式如下:

<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

在httpd服務程序主配置文件中,存在三種:注釋行信息、全局配置、區域配置。

(2)常用參數及用途

ServerRoot "/etc/httpd"    # 服務目錄

#Listen 12.34.56.78:80
Listen 80       # 監聽的IP地址和端口

User apache     # 運行服務的用戶
Group apache    # 運行服務的用戶組

ServerAdmin root@localhost    # 管理員郵箱

ServerName www.example.com:80 # 網站服務器域名

DocumentRoot "/var/www/html"   # 文檔根目錄(網站)

ErrorLog "logs/error_log"    # 錯誤日志文件
LogLevel warn                # 日志等級

CustomLog "logs/access_log" combined   # 訪問日志文件

AddDefaultCharset UTF-8    # 默認字符集


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM