Linux常用基础

MBR磁盘分区–fdisk

]# fdisk /dev/sdb 最大支持容量为 2.2TB 的磁盘.4个主分区
n 创建新的分区----->分区类型 回车----->分区编号 回车---->起始扇区 回车----->在last结束时 +5G
p 查看分区表,分错Ctrl+c,终止

[root@test ~]# fdisk /dev/sd
/dev/sda  /dev/sdb
[root@test ~]# 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 0xe1049276.

Command (m for help): n		#创建新的分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p	#分区类型 回车
Partition number (1-4, default 1):		#回车
First sector (2048-41943039, default 2048):		#回车
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G 	#5G
Partition 1 of type Linux and of size 5 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@test ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part
  ├─centos-root 253:0    0   17G  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

格式化mkfs.(tab)(tab)

ext4 适合文件量多而小 xfs 适合文件量少而大

[root@test ~]# mkfs.
mkfs.btrfs   mkfs.cramfs  mkfs.ext2    mkfs.ext3    mkfs.ext4    mkfs.minix   mkfs.xfs
[root@test ~]# mkfs.ext4 /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

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@test ~]# blkid /dev/sdb1
/dev/sdb1: UUID="8f1fb8b7-83d3-45b5-9425-689a09bbc16c" TYPE="ext4"

未分区的情况下你也可以对整块磁盘格式化

[root@test ~]# mkfs.ext4 /dev/sdb
[root@test ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk

GPT磁盘分区–part

最大支持128个主分区,支持18EB容量

[root@test ~]# parted /dev/sdc
GNU Parted 3.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mktable gpt	#指定分区模式
(parted) mkpart		 #划分新的分区
Partition name?  []? test	#命名
File system type?  [ext2]? ext4		 #随意写,不会进行格式化
Start? 0	#起始点
End? 10G	#结束点
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? ignore		#选择忽略
(parted) print		#查看分区表信息
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size     File system  Name  Flags
 1      17.4kB  10.0GB  10000MB               test

(parted) unit   GB	#使用GB作为单位
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name  Flags
 1      0.00GB  10.0GB  10.0GB               test
[root@test ~]# lsblk
sdc               8:32   0   20G  0 disk
└─sdc1            8:33   0  9.3G  0 part

交换空间

[root@test ~]# swapon
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition   2G   0B   -2
[root@test ~]# mkswap   /dev/sdc1   #格式化交换文件系统 
[root@test ~]# swapon /dev/sdc1  #启用交换分区
[root@test ~[root@test ~]# swapon
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition   2G   0B   -2
/dev/sdc1 partition 9.3G   0B   -3
[root@test ~]# free -h             #查看交换空间的大小
              total        used        free      shared  buff/cache   available
Mem:           972M        127M        756M        7.6M         88M        727M
Swap:           11G          0B         11G
]# swapoff  -a        #停用所有交换分区
]# swapon -a             #打开所有交换分区
]# free   -m               #查看交换空间的大小 
]# vim    /etc/fstab 
/dev/sdc1   swap   swap    defaults  0   0		# 

使用dd制作交换空间

–dd if=源设备 of=目标设备 bs=块大小 count=块数
/dev/zero:可以产生无数的数据 /dev/null 黑洞

]# dd  if=/dev/zero   of=/opt/swap.txt  bs=1M  count=2048   	#产生2G文件
[root@test ~]# dd  if=/dev/zero   of=/opt/swap.txt  bs=1M  count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 15.0055 s, 143 MB/s
]# du  -sh   /opt/swap.txt 

使用dd测试linux磁盘读、写速度、备份磁盘

[root@test ~]# dd  if=/dev/zero   of=/opt/write.txt  bs=1M  count=2048  
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 31.5853 s, 68.0 MB/s  #写入速度
[root@test ~]# dd if=/opt/write.txt  of=/dev/null
4194304+0 records in
4194304+0 records out
2147483648 bytes (2.1 GB) copied, 8.09183 s, 265 MB/s  #读取速度
[root@test ~]# dd  if=/dev/sdc1   of=/opt/sdc1.bak  #备份分区
19531217+0 records in
19531217+0 records out
9999983104 bytes (10 GB) copied, 117.454 s, 85.1 MB/s

LVM管理工具集

功能物理卷管理卷组管理逻辑卷管理
Scan 扫描pvscanvgscanlvscan
Create 创建pvcreatevgcreatelvcreate
Display 显示pvdisplayvgdisplaylvdisplay
Remove 删除pvremovevgremovelvremove
Extend 扩展/vgextendlvextend

制作逻辑卷

1.建立卷组(VG) vgcreate
格式:vgcreate 卷组名 设备路径…….

[root@test ~]# lsblk
sdb               8:16   0   20G  0 disk
├─sdb1            8:17   0    5G  0 part
└─sdb2            8:18   0    5G  0 part
[root@test ~]# vgcreate   systemvg   /dev/sdb[1-2]	# 扩展分区的逻辑分区要partprobe刷新一下
[root@test ~]# pvs    #查看系统所有物理卷信息
  PV         VG       Fmt  Attr PSize   PFree
  /dev/sdb1  systemvg lvm2 a--   <5.00g <5.00g
  /dev/sdb2  systemvg lvm2 a--   <5.00g <5.00g
[root@test ~]# vgs    #查看系统卷组信息
  VG       #PV #LV #SN Attr   VSize   VFree
  systemvg   2   0   0 wz--n-   9.99g 9.99g

2.建立逻辑卷(LV) lvcreate
格式: lvcreate -L 大小G -n 逻辑卷名字 卷组名

[root@test ~]# lvcreate   -L  8G   -n    vo    systemvg
  Logical volume "vo" created.
[root@test ~]# lvs
  LV   VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  vo   systemvg -wi-a-----   8.00g

3.逻辑卷挂载mkfs.格式化

[root@test ~]#  ls   /dev/systemvg/vo
/dev/systemvg/vo
[root@test ~]# mkfs.xfs    /dev/systemvg/vo
[root@test ~]# blkid /dev/systemvg/vo
/dev/systemvg/vo: UUID="2b322a5f-77fb-4083-9160-7951193ef21c" TYPE="xfs"

逻辑卷的扩展

扩展卷组的空间 vgextend systemvg /dev/sdc1

[root@test ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdc               8:32   0   20G  0 disk
└─sdc1            8:33   0    5G  0 part
[root@test ~]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree
  centos     1   2   0 wz--n- <19.00g    0
  systemvg   2   1   0 wz--n-   9.99g 1.99g
[root@test ~]# vgextend   systemvg    /dev/sdc1
  Physical volume "/dev/sdc1" successfully created.
  Volume group "systemvg" successfully extended
[root@test ~]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree
  centos     1   2   0 wz--n- <19.00g     0
  systemvg   3   1   0 wz--n- <14.99g <6.99g

扩展逻辑卷的空间 lvextend -L 12G /dev/systemvg/vo

[root@test ~]# lvs
  LV   VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  vo   systemvg -wi-a-----   8.00g
[root@test ~]#  lvextend   -L  12G   /dev/systemvg/vo
  Size of logical volume systemvg/vo changed from 8.00 GiB (2048 extents) to 12.00 GiB (3072 extents).
  Logical volume systemvg/vo successfully resized.
[root@test ~]# lvs
  LV   VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  vo   systemvg -wi-a-----  12.00g
 

刷新逻辑卷的文件系统(刷新文件系统)

xfs_growfs:刷新xfs文件系统 resize2fs:刷新ext4文件系统

xfs_growfs  /dev/systemvg/vo

删除逻辑卷lvremove vgremove

删除卷组的前提:基于此卷组创建的所有逻辑卷,要全部删除

删除逻辑卷的前提:不能删除正在挂载使用的逻辑卷

磁盘阵列

用法: mdadm -C /dev/md0 -a yes -n 2 -l 0 /dev/sdb /dev/sdc

常用选项
-C 创建raid
-a yes 自动创建raid设备/dev/mdX
-n num 阵列中磁盘的数量
-l num 阵列的等级0 1 5 10
-x num 热备盘的数量
mdadm -D /dev/mdx 查看raid信息
cat /proc/mdstat 查看全部的raid信息
mdadm -S /dev/md0 停止raid

配置网络参数/etc/default/grub

]# ifconfig   |    head    -2
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 00:0c:29:8a:72:4f  txqueuelen 1000  (Ethernet)
]# vim   /etc/default/grub      #修改grub内核引导程序
……..此处省略一万字
GRUB_CMDLINE_LINUX="…….. quiet   net.ifnames=0  biosdevname=0"
……..此处省略一万字              #  =0 固定格式
]# grub2-mkconfig  -o  /boot/grub2/grub.cfg  #命名生效输出到grub.cfg
]# reboot		#重启
]# ifconfig   |   head   -2
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.81.132  netmask 255.255.255.0  broadcast 192.168.81.255
2.nmcli删除错误网卡命名
]# nmcli   connection   show             #查看
]# nmcli   connection   delete   有线连接\ 1 
3.添加网卡命名
]# nmcli connection  add  type  ethernet      ifname eth0   con-name  eth0			# nmcli connection 添加   类型   以太网设备   	网卡设备名为eth0    nmcli命令的命名为eth0			网卡命名使用较多
 ]# nmcli  connection show
4.修改IP地址、子网掩码、网关地址     
]# nmcli connection   modify  网卡名    ipv4.方法    手动   ipv4.地址   192.168.4.1/24     ipv4.网关   192.168.4.254  connection.autoconnect    yes

]# nmcli connection modify  eth0    # eth0     con-name
ipv4.method    manual                          # 手动配置
ipv4.addresses   192.168.4.1/24             #   IP地址 掩码
ipv4.gateway   192.168.4.254                #网关
connection.autoconnect    yes
]# nmcli connection up eth0     #激活,配IP,使用较多
]# ifconfig   |   head   -2
网卡配置文件:/etc/sysconfig/network-scripts/

YUM

本地的yum

]# vim    /etc/yum.repos.d/mydvd.repo
[centos]
name=2010
baseurl=file:///mydvd
enabled=1
gpgcheck=0

基于ftp的yum

搭建

[root@web ~]# yum -y install createrepo  vsftpd
[root@web ~]# mkdir /var/ftp/localrepo
[root@web ~]# cd /var/ftp/localrepo
[root@web localrepo]# createrepo  .
[root@web localrepo]# ls
repodata		docker-ce.rpm#测试	
[root@web localrepo]#  createrepo --update .
Spawning worker 0 with 1 pkgs
[root@web localrepo]# systemctl enable --now vsftpd

验证

[root@test ~]# vim /etc/yum.repos.d/local.repo 
[local_repo]
name=CentOS-$releasever – Localrepo
baseurl=ftp://192.168.1.17/localrepo			
enabled=1			
gpgcheck=0
[root@test ~]# yum repolist
local_repo     		 CentOS-7 – Localrepo  			1

基于HTTP

[root@web ~]# yum -y install httpd
[root@web ~]# mkdir /var/www/html/localrepo
[root@web ~]# cd /var/www/html/localrepo
[root@web localrepo]# createrepo  .
[root@web localrepo]# ls
repodata		docker-ce.rpm#测试	
[root@web localrepo]#  createrepo --update .
Spawning worker 0 with 1 pkgs
[root@web localrepo]# systemctl enable --now  httpd

验证

[root@test ~]# vim /etc/yum.repos.d/http.repo 
[http_repo]
name=CentOS-Localrepo
baseurl=http://192.168.1.17/localrepo			
enabled=1			
gpgcheck=0
[root@test ~]# yum repolist
local_repo     		 CentOS-7 – Localrepo  			1

下载包

[root@web ~]# yumd-y install  yum-utils.noarch
[root@web ~]# yumdownloader yum-utils.noarch
[root@web ~]# ls
yum-utils-1.1.31-54.el7_8.noarch.rpm

DNS

时间服务器

chrony时间服务器搭建

]# yum  -y  install   chrony
]# vim  /etc/chrony.conf      
#注释掉0.centos.pool.ntp.org机器在国外
#server   0.centos.pool.ntp.org   iburst    #与谁同步时间
#server   1.centos.pool.ntp.org   iburst    #iburst表示快速同步
#server   2.centos.pool.ntp.org   iburst
#server   3.centos.pool.ntp.org   iburst

26行  allow   all           #开头的#去掉,修改为允许所有客户端
29行  local  stratum  10     #本机通常设置为第10层的时间服务器,
]# systemctl    restart  chronyd

chrony客户端时间同步

]# yum  -y  install   chrony
]# vim   /etc/chrony.conf      
server  192.168.4.1   iburst      #向192.168.4.1同步时间
#server  1.centos.pool.ntp.org  iburst    #iburst表示快速同步
#server  2.centos.pool.ntp.org  iburst
#server  3.centos.pool.ntp.org  iburst
]# systemctl  restart  chronyd
]# date   -s   "1998-1-1"
]#  chronyc  sources  -v    #列出时间服务器信息,带*同步ok
* 192.168.4.1              2   6     1     2  +0s[+4s] +/-   26ms
]# vim  /etc/ntp.conf
restrict 192.168.1.1 nomodify notrap nopeer noquery          //当前节点IP地址
restrict 127.0.0.1
restrict ::1
restrict 192.168.1.254 mask 255.255.255.0 nomodify notrap  //集群所在网段的网关、掩码
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 127.127.1.0
Fudge 127.127.1.0 stratum 10
[root@test html]# systemctl restart ntpd
[root@test html]# ntpstat
synchronised to local net (127.127.1.0) at stratum 6
   time correct to within 7948 ms
   polling server every 64 s
[root@test html]# ntpq -p			#查看ntp服务器与上层ntp的状态
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.           5 l   40   64    1    0.000    0.000   0.000

]# vim  /etc/ntp.conf
restrict 192.168.1.5 nomodify notrap nopeer noquery          //当前节点IP地址
restrict 127.0.0.1
restrict ::1
restrict 192.168.1.254 mask 255.255.255.0 nomodify notrap  //集群所在网段的网关、掩码
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 192.168.1.1			#服务器节点
Fudge 192.168.1.1 stratum 10	#服务器节点
[root@web ~]# systemctl start ntpd
[root@web ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.1.18    LOCAL(0)         6 u   38   64    1    0.529  -468005   0.000

rsync与inotify数据同步

[root@svr7 ~]# ssh-keygen         #一路回车
]# ssh-copy-id     root@192.168.4.207
]# rsync  -av  --delete   /mydir/    root@192.168.4.207:/opt
源码编译安装步骤:
]# yum   -y   install   make  gcc
]# tar  -xf  /root/tools.tar.gz  -C   /usr/local/
]# tar -xf   /usr/local/tools/inotify-tools-3.13.tar.gz   -C /usr/local/
]# cd    /usr/local/inotify-tools-3.13/
]# ./configure     --prefix=/opt/myrpm       #指定安装位置
]# make
]# make    install
]# ls  /opt/myrpm/bin/  
]#  /opt/myrpm/bin/ inotifywait    -rq   /mnt   #监控/mnt目录 
•基本用法
inotifywait  [选项]  目标文件夹
•常用命令选项
-m,持续监控(捕获一个事件后不退出)
-r,递归监控、包括子目录及文件
-q,减少屏幕输出信息
-e,指定监视的 modify、move、create、delete、attrib 等事件类别     
]# vim   /etc/rsync.sh
while   /opt/myrpm/bin/inotifywait    -rqq    /mydir/
do
rsync -a  --delete   /mydir/    root@192.168.4.2:/opt
done
[root@svr7 /]# chmod   a+x    /etc/rsync.sh    
[root@svr7 /]# /etc/rsync.sh   &     

ntpdate同步时间

1、查看当前服务器时间
# date -R
Mon, 27 Apr 2021 10:42:51 +0800
2、安装ntpdate服务
# yum install ntpdate
3、同步中国上海时间
#timedatectl set-timezone Asia/Shanghai 
# ntpdate ntp1.aliyun.com
19 Feb 11:36:56 ntpdate[2310]: adjust time server 120.25.115.20 offset 0.001033 sec
4、查看新时间是否已同步
# date -R
Sat, 19 Feb 2022 11:38:26 +0800
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值