centos7基本命令,参考视频:视频,此文章为此视频笔记稍作修改,需要window服务器和vm的使用基础!!!

ls(列出当前目录,蓝色目录,白色普通文件,绿色可执行文件,红色压缩包文件),
ls-l(以长格式显示文件信息)卡头是d是文件,开头是-表示目录,开头c是字符串设备,d是块设备。
ls-la(列出系统目录和文件夹)开头.是系统文件
ls-lh(内存单位换算)方便阅读
ls-lh --sort=size(从文件大到小查看)
ls-lh --sort=time(按时间顺序查看)

mkdir(创建目录)
cd(进入目录)
cd.(进入当前目录)
cd..(进入上一级目录)
cd /(进入根目录)
cd ~(进入家目录)
pwd(查看当前工作目录)
rmdir(删除空文件夹)

cat(显示文本类型的内容)(/etc/passwd可以查看本地用户)
more(分屏查看,适合查看开头部分,按q退出)
less(分屏查看,但左下角不会显示占文件的百分之多少)
tail(查看最尾部十条内容,可以-数字指定显示几条)
watch -n 2(每隔两秒(可替换)执行一次命令)        //监视

touch(创建文件)
cp ps ps1(拷贝文件并改名)
mv(移动,也可改名)
cp -r(拷贝文件夹到目录下(如果没有,就新建目录))
rm(删除文件)
rm -r(删除目录)
rm -rf(无提示删除)

vi编辑器三种模式:

a                                                  :
                  ------>                                      --------->
 输入模式                  命令模式(默认模式)                 末行模式(wq保存并退出;q!强制退出)
                 <------                                       <---------
                     ESC                                            ESC

-------------------------------------------------------------------------------------------------
vi编辑器命令模式下的相关命令:
yy:复制当前行                     nyy:复制n行
dd:删除当前行                    ndd:删除n行
p:粘贴                                np:粘贴n行

------------------------------------------------------------------------------------------------------
centos的存储器管理

外存:
1,移动存储设备:光盘,U盘
2,固定存储设备:硬盘

光盘的使用:安装光盘(准备centos的ios)
实验步骤(核心思想:挂载):
     1.将iso镜像放入虚拟机的光驱中,右键设置光驱。
     2.创建挂载目录(建议挂载目录建立在media目录下或mnt目录下)
     3.挂载光盘到指定的挂载目录下:mount   设备名  挂载目录
     4.通过访问挂载目录实现对光盘的访问
     5.使用完毕,可以卸载光盘:umount 挂载目录(需要去掉其他目录)
      命令:将/下的dev的cdrom挂载到/下的media的cdrom中,打开cdrom即可使用,使用完毕,退出
      此目录,卸载光盘。

[root@localhost ~]# cd /media
[root@localhost media]# ls
[root@localhost media]# mkdir cdrom
[root@localhost media]# ls
cdrom
[root@localhost media]# mount /dev/cdrom cdrom
mount: /dev/sr0 is write-protected, mounting read-only
[root@localhost media]# ls
cdrom
[root@localhost media]# cd cdrom\
> 
[root@localhost cdrom]# ls
CentOS_BuildTag  GPL       LiveOS    RPM-GPG-KEY-CentOS-7
EFI              images    Packages  RPM-GPG-KEY-CentOS-Testing-7
EULA             isolinux  repodata  TRANS.TBL
[root@localhost cdrom]# cd ..
[root@localhost media]# umount cdrom
[root@localhost media]# ls
cdrom
[root@localhost media]# cd cdrom/
[root@localhost cdrom]# ls
[root@localhost cdrom]#

 
 -----------------------------------------------------------------------------------------------------
硬盘的使用:静态磁盘(基本磁盘)
知识点:理解分区的类型
       1.主分区:编号1~4
       2.扩展分区:编号1~4
       3.逻辑分区: 编号大于5
   特点:
             1.主分区+扩展分区<=4
             2.扩展分区0或1
             3.扩展分区的空间不能直接使用,如果要使用,需要在它上面建立逻辑分区
             4.逻辑分区的数量不受限制
       实验:
                 1.添加一块1GB的硬盘(虚拟机(关机)添加),分区(主分区+扩展分区(两个逻辑分区))500M+300M+200M
                 2.分区(分区文件:/dev/sdb(b是第二块,可累加))
                 3.fdisk /dev/sdb对/dev/sdb进行磁盘分区等操作,输入m可以有提示
                    子命令:n(创建分区) ; p(查看分区) ; w(保存分区) ; q(退出分区) ;d(删除分区)
                 4.创建文件系统(格式化):ext4:mkfs.ext4(格式化某分区)
                 5.创建挂载目录(/mnt/disk;/mnt/disk2;/mnt/disk3)
                 6.使用umount挂载
            步骤:输入fdisk /dev/sdb,在输入p查看分区,输入n创建分区(p是主分区,e是扩展分区)(因为没有逻辑分区,所以没有扩展分区)选择p,输入分区号(1-4),回车(从默认分区中的块开始),输入
+500M(添加500M的分区)。可以使用p查看有无分区,第二步:输入n创建分区,输入e扩展分区,输入分区号(2-4),回车开始,+500M(添加500M的扩展分区)。第三步:n(在扩展分区中创建分区),l(创建逻辑分区,回车开始,+300M分区)。第四步,输入n,l逻辑分区,回车开始,+200M(或者之间回车取全部)分区。p查看分区,w保存并退出。

[root@localhost ~]# cd /
[root@localhost /]# cd dev
[root@localhost dev]# ls sd*
sda  sda1  sda2  sdb
[root@localhost dev]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

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

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

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

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0xbdd32c67

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1026047      512000   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): 2
First sector (1026048-2097151, default 1026048): 
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-2097151, default 2097151): +500M
Partition 2 of type Extended and of size 500 MiB is set

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0xbdd32c67

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1026047      512000   83  Linux
/dev/sdb2         1026048     2050047      512000    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): l
Adding logical partition 5
First sector (1028096-2050047, default 1028096): 
Using default value 1028096
Last sector, +sectors or +size{K,M,G} (1028096-2050047, default 2050047): +300M
Partition 5 of type Linux and of size 300 MiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 6
First sector (1644544-2050047, default 1644544): 
Using default value 1644544
Last sector, +sectors or +size{K,M,G} (1644544-2050047, default 2050047): +200M
Value out of range.
Last sector, +sectors or +size{K,M,G} (1644544-2050047, default 2050047): 
Using default value 2050047
Partition 6 of type Linux and of size 198 MiB is set

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0xbdd32c67

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1026047      512000   83  Linux
/dev/sdb2         1026048     2050047      512000    5  Extended
/dev/sdb5         1028096     1642495      307200   83  Linux
/dev/sdb6         1644544     2050047      202752   83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost dev]#

     格式化步骤:打开/dev/用is查看,mkfs.ext4 /dev/sdb1(用ext4格式化sdb1),其他分区都格式化。第二步:在/mnt下创建三个目录disk1/2/3,使用mount挂载分区例如:mount /dev/sdb1 /mnt/disk1   不用时再用umount卸载,实验结束。

[root@localhost dev]# cd /dev
[root@localhost dev]# ls sd*
sda  sda1  sda2  sdb  sdb1  sdb2  sdb5  sdb6
[root@localhost dev]# mkfs.ext4 sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
128016 inodes, 512000 blocks
25600 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=34078720
63 block groups
8192 blocks per group, 8192 fragments per group
2032 inodes per group
Superblock backups stored on blocks: 
        8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@localhost dev]# mkfs.ext4 sdb2
mke2fs 1.42.9 (28-Dec-2013)
mkfs.ext4: inode_size (128) * inodes_count (0) too big for a
        filesystem with 0 blocks, specify higher inode_ratio (-i)
        or lower inode count (-N).

[root@localhost dev]# mkfs.ext4 sdb5
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
76912 inodes, 307200 blocks
15360 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33947648
38 block groups
8192 blocks per group, 8192 fragments per group
2024 inodes per group
Superblock backups stored on blocks: 
        8193, 24577, 40961, 57345, 73729, 204801, 221185

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@localhost dev]# mkfs.ext4 sdb6
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
50800 inodes, 202752 blocks
10137 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
25 block groups
8192 blocks per group, 8192 fragments per group
2032 inodes per group
Superblock backups stored on blocks: 
        8193, 24577, 40961, 57345, 73729

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@localhost dev]# ^C
[root@localhost dev]# cd /mnt
[root@localhost mnt]# ls
[root@localhost mnt]# mkdir disk1 disk2 disk3
[root@localhost mnt]# ls
disk1  disk2  disk3
[root@localhost mnt]# mount /dev/sdb1 disk1
[root@localhost mnt]# mount /dev/sdb5 disk2
[root@localhost mnt]# mount /dev/sdb6 disk3
[root@localhost mnt]# ls
disk1  disk2  disk3
[root@localhost mnt]# cd disk1
[root@localhost disk1]# lw
-bash: lw: command not found
[root@localhost disk1]# ls
lost+found
[root@localhost disk1]# umount disk1
umount: disk1: mountpoint not found
[root@localhost disk1]# cd ..
[root@localhost mnt]# umount disk1
[root@localhost mnt]# umount dis2
umount: dis2: mountpoint not found
[root@localhost mnt]# umount disk2
[root@localhost mnt]# umount disk3
[root@localhost mnt]# ls
disk1  disk2  disk3

备注:EXT4是第四代扩展文件系统(英语:Fourth extended filesystem,缩写为 ext4)是Linux系统下的日志文件系统,是ext3文件系统的后继版本。
------------------------------------------------------------------------------------------------------
自动挂载存储设备
查询已挂载的设备:mount

mount具有“临时性”:当主机重启后,系统将自动卸载设备

如何使设备永久挂载?解决方法:修改配置文件 /etc/fstab(自启动),用vi编辑,#的行是注释

写入六列,挂载文件     ,挂载位置,     文件类型,    权限            是否,       是否
                /dev/sdb1    /mnt/disk1       ext4              rw(读写)  0(否)  0(否)

如何使/etc/fstab文件生效
方法一,重启系统(如果文件输入错误,可能开不了机)
方法二,mount -a:重新加载测试

光驱也可以永久挂载

写入六列:/dev/cdrom            /media/cdrom        iso9660(文件类型)       ro(只读)    0   0

[root@localhost mnt]# cd /etc
[root@localhost etc]# vi fstab 
# /etc/fstab
# Created by anaconda on Fri Dec 18 10:09:40 2020
#
# 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=8922fb97-276c-47d1-be38-924b92e53bd5 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/cdrom      /media/cdrom    iso9660 ro      0       0
[root@localhost etc]# mount -a