(一)编辑器的使用
1. 进入编辑器界面
[root@VM_141_177_centos ~]# vi myfile
若已有文件myfile,则打开该文件;若没有,则创建。
2. 编辑界面
首先进入命令模式:
i----进入编辑模式 :q----退出 :w----保存 o----进入编辑模式,并换行
:wq----保存后退出 :q!----强制退出(不保存)
常用操作步骤:
(1)按键i进入编辑模式
(2)输入编辑内容:例如My own file.
(3)按ESC返回命令模式
(4):wq保存并退出
(5)查看文件内容:
[root@VM_141_177_centos ~]# cat myfile
(二)目录操作
1. 进入绝对目录/etc/sysconfig/
[root@VM_141_177_centos ~]# cd /etc/sysconfig
2. 查看当前所在目录
[root@VM_141_177_centos sysconfig]# pwd
/etc/sysconfig
3. 切换到上一级目录
[root@VM_141_177_centos sysconfig]# cd ..
4. 切换到子目录sysconfig
[root@VM_141_177_centos etc]# cd sysconfig/
5. 查看目录下的文件和子目录
[root@VM_141_177_centos sysconfig]# ls
[root@VM_141_177_centos sysconfig]# ls –l
6. 进入用户的home目录
[root@VM_141_177_centos sysconfig]# cd
7. 创建目录myDirectory
[root@VM_141_177_centos ~]# ls
anaconda-ks.cfg myfile
[root@VM_141_177_centos ~]# mkdir myDirectory
[root@VM_141_177_centos ~]# ls
anaconda-ks.cfg myDirectory myfile
8. 删除目录
[root@VM_141_177_centos ~]# rmdir myDir/ (只能删除空目录)
[root@VM_141_177_centos ~]# rm -rf myDirectory/
-f----强制删除 -r----删除该目录及其子目录和文件
(三)用户组与用户名
1. 创建用户组
[root@VM_141_177_centos ~]# groupadd group1
[root@VM_141_177_centos ~]# groupadd group2
2.查看用户组
[root @VM_141_177_centos /]$ cat /etc/group
group1:x:1000:
group2:x:1001:
3. 创建用户
[root@VM_141_177_centos ~]# useradd -g group1 -m user1 -p user1111
[root@VM_141_177_centos ~]# useradd -g group2 -m user2 -p user2222
g----指定用户组 m----用户名及home目录 p----用户密码
[root@VM_141_177_centos ~]# cd /home
[root@VM_141_177_centos home]# ls
user1 user2
4. 查看用户
[root @VM_141_177_centos /]$ cat etc/passwd
user1:x:1000:1000::/home/user1:/bin/bash
user2:x:1001:1001::/home/user2:/bin/bash
5. 修改用户组
[root@VM_141_177_centos /]# usermod user2 -g group1
[root @VM_141_177_centos /]$ cat etc/passwd
user1:x:1000:1000::/home/user1:/bin/bash
user2:x:1001:1000::/home/user2:/bin/bash
6. 删除用户
[root@VM_141_177_centos home]# userdel user2
7. 登录用户
[root@VM_141_177_centos /]# su user1
8. 切换到root用户
[user1@VM_141_177_centos /]$ su
输入密码:
9. 修改用户密码
[root@VM_141_177_centos /]# passwd user1
10. 用户退出登录
[root@VM_141_177_centos /]# su user1
[user1@VM_141_177_centos /]$ exit
exit
11. 分配用户root权限(使用sudo)
[root@JXLY maliang]# vi /etc/sudoers
找到root ALL=(ALL) ALL一行,下面插入
maliang ALL=(ALL) ALL
liyang ALL=(ALL) ALL
wangjiuniu ALL=(ALL) ALL
jiangsitan ALL=(ALL) ALL
pengfeng ALL=(ALL) ALL
(四)文件的操作
1. 创建文件和编辑文件见(一)
2. 文件拷贝
[root@VM_141_177_centos ~]# ls
anaconda-ks.cfg myDir myfile
案例:myfile拷贝到myDir中
[root@VM_141_177_centos ~]# cp myfile myDir/
[root@VM_141_177_centos ~]# cd myDir/
[root@VM_141_177_centos myDir]# ls
myfile
3. 文件删除
[root@VM_141_177_centos myDir]# rm -f myfile (r----强制删除)
或者[root@VM_141_177_centos myDir]# rm myfile
[root@VM_141_177_centos myDir]# ls
4. 文件重命名
[root@VM_141_177_centos ~]# ls
anaconda-ks.cfg myDir myfile
[root@VM_141_177_centos ~]# mv myfile myNewFile
[root@VM_141_177_centos ~]# ls
anaconda-ks.cfg myDir myNewFile
5. 文件移动
[root@VM_141_177_centos ~]# ls
anaconda-ks.cfg myDir myNewFile
[root@VM_141_177_centos ~]# mv myNewFile myDir/
[root@VM_141_177_centos ~]# ls
anaconda-ks.cfg myDir
[root@VM_141_177_centos ~]# cd myDir/
[root@VM_141_177_centos myDir]# ls
myNewFile
6. 文件权限修改
[root@VM_141_177_centos myDir]# ls -l
-rw-r--r-- 1 root root 13 May 16 10:19 myNewFile
(权限说明:rw-r--r-- 110 100 100 == 644)
[root@VM_141_177_centos myDir]# chmod 777 myNewFile
[root@VM_141_177_centos myDir]# ls -l
-rwxrwxrwx 1 root root 13 May 16 10:19 myNewFile
(权限说明:rwxrwxrwx 111 111 111 == 777)
[root@VM_141_177_centos myDir]# chmod 644 myNewFile
[root@VM_141_177_centos myDir]# ls -l
-rw-r--r-- 1 root root 13 May 16 10:19 myNewFile
(五)网络配置
1. 查看IP地址和网关
[root@VM_141_177_centos ~]# ifconfig
inet 10.141.141.177 netmask 255.255.192.0
broadcast 10.141.191.255 ether 52:54:00:ee:63:73
inet----IPV4地址 netmask----掩码 broadcast----广播地址 ether----MAC地址
[root@VM_141_177_centos ~]$ netstat –rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 172.17.223.253 0.0.0.0 UG 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
172.17.208.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0
[root@VM_141_177_centos ~]$ cat /etc/resolv.conf
[root@VM_141_177_centos ~]# ip addr
link/ether 52:54:00:ee:63:73 brd ff:ff:ff:ff:ff:ff
inet 10.141.141.177/18 brd 10.141.191.255
2. 配置IP地址
(1)命令行配置(任选一种方式)
a)
[root@VM_141_177_centos ~]# ip addr add 10.141.141.178 dev eth0
add----配置IPV4地址 dev----指定网卡
b)
[root@VM_141_177_centos ~]#ifconfig eth0 192.168.1.123 netmask 255.255.255.0
(2)手动配置(永久有效)
[root@VM_141_177_centos ~]# cd /etc/sysconfig/network-scripts/
[root@VM_141_177_centos network-scripts]# vi ifcfg-eth0
输入以下内容:
DEVICE='eth0' 设备名称
NM_CONTROLLED='no'
ONBOOT='yes' 开机启动
IPADDR='10.141.141.177' IP地址
NETMASK='255.255.192.0' 掩码
GATEWAY='10.141.128.1' 网关
3. 配置网关
(1)手动配置(永久有效)
[root@VM_141_177_centos /]# cd /etc/sysconfig/
[root@VM_141_177_centos sysconfig]# vi network
输入以下内容:
NETWORKING=yes
HOSTNAME=localhost.localdomain
GATEWAY=10.141.128.1
(2)命令行配置(选择一种)
route add default gw 192.168.10.1
route add -net 192.168.10.0 netmask 255.255.255.0 gw 192.168.10.1
4. 重启网络服务
[root@VM_141_177_centos ~]# service network restart
上述手动配置完毕,必须执行该命令才能生效
5. 网络配置文件
/etc/sysconfig/network设置默认的网关,主机名称,是否启动网络3项; /etc/sysconfig/network-script/ifcfg-ethN ,N表示网卡编号,这个地方可以设置该网卡的参数, 如ip,netmask,network,broadcast,gateway,开机取得ip的协议方式(dhcp,static)static, 是否在开机时启动等, /etc/resolv.conf配置dns服务器 /etc/hosts将局域网内机器名与ip地址对应起来,达到通过机器名访问其他机器的目的(仅在通过 机器名无法反查ip情况下)
(六)新增网卡
1. 查看物理网卡
[root@VM_141_177_centos ~]# ifconfig -a
2. 添加网卡配置信息(eth1)
[root@VM_141_177_centos ~]# cd /etc/sysconfig/network-scripts
[root@VM_141_177_centos network-scripts]# vi ifcfg-eth1
编辑内容:
DEVICE='eth1'
NM_CONTROLLED='no'
ONBOOT='yes'
IPADDR='10.141.141.167'
NETMASK='255.255.192.0'
GATEWAY='10.141.128.1'
3. 启用新网卡eth1
[root@VM_141_177_centos ~]# ifup eth1
4. 重启网络服务(见网络配置)
5. ifconfig查看配置结果
(七)磁盘操作
1. 查看磁盘
[root@VM_141_177_centos ~]# fdisk –l
Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 sectors
2. 查看新硬盘
[root@VM_141_177_centos ~]# fdisk –l
Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
3. 创建分区
[root@VM_141_177_centos ~]# fdisk /dev/vdb
此时进入fdisk命令界面(退出时要使用 w:保存并退出)
Command (m for help):
(1)创建主分区
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-20971519, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): 3147776
Partition 1 of type Linux and of size 1.5 GiB is set
(2)创建扩展分区
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 (3147777-20971519, default 3149824): 3149824
Last sector, +sectors or +size{K,M,G} (3149824-20971519, default 20971519): 6299648
Partition 2 of type Extended and of size 1.5 GiB is set
(3)创建逻辑分区
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 (3151872-6299648, default 3151872):
Using default value 3151872
Last sector, +sectors or +size{K,M,G} (3151872-6299648, default 6299648): 6299648
Partition 5 of type Linux and of size 1.5 GiB is set
(4)查看分区
Command (m for help): p
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xa4de193b
Device Boot Start End Blocks Id System
/dev/vdb1 2048 3147776 1572864+ 83 Linux
/dev/vdb2 3149824 6299648 1574912+ 5 Extended
/dev/vdb5 3151872 6299648 1573888+ 83 Linux
(5)删除分区
Command (m for help): d
Partition number (1,2,5, default 5): 5
Partition 5 is deleted
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted
(6)重建扩展分区和逻辑分区
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 (3147777-20971519, default 3149824):
Using default value 3149824
Last sector, +sectors or +size{K,M,G} (3149824-20971519, default 20971519):
Using default value 20971519
Partition 2 of type Extended and of size 8.5 GiB 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 5
First sector (3151872-20971519, default 3151872):
Using default value 3151872
Last sector, +sectors or +size{K,M,G} (3151872-20971519, default 20971519): 12670487
Partition 5 of type Linux and of size 4.6 GiB 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 (12672536-20971519, default 12673024):
Using default value 12673024
Last sector, +sectors or +size{K,M,G} (12673024-20971519, default 20971519):
Using default value 20971519
Partition 6 of type Linux and of size 4 GiB is set
(7)查看分区
Command (m for help): p
Device Boot Start End Blocks Id System
/dev/vdb1 2048 3147776 1572864+ 83 Linux
/dev/vdb2 3149824 20971519 8910848 5 Extended
/dev/vdb5 3151872 12670487 4759308 83 Linux
/dev/vdb6 12673024 20971519 4149248 83 Linux
(8)保存分区并退出
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
4. 格式化分区
[root@VM_141_177_centos ~]# fdisk –l
Device Boot Start End Blocks Id System
/dev/vdb1 2048 3147776 1572864+ 83 Linux
/dev/vdb2 3149824 20971519 8910848 5 Extended
/dev/vdb5 3151872 12670487 4759308 83 Linux
/dev/vdb6 12673024 20971519 4149248 83 Linux
[root@VM_141_177_centos ~]# mkfs -t ext3 /dev/vdb1
[root@VM_141_177_centos ~]# mkfs -t ext3 /dev/vdb5
[root@VM_141_177_centos ~]# mkfs -t ext3 /dev/vdb6
5. 挂载磁盘分区
(1)创建挂载点
[root@VM_141_177_centos ~]# cd /mnt
[root@VM_141_177_centos mnt]# mkdir vdb1
[root@VM_141_177_centos mnt]# mkdir vdb5
[root@VM_141_177_centos mnt]# mkdir vdb6
(2)挂载分区
[root@VM_141_177_centos mnt]# mount /dev/vdb1 /mnt/vdb1
(3)查看挂载结果
[root@VM_141_177_centos mnt]# df –h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.2G 18G 7% /
devtmpfs 492M 0 492M 0% /dev
tmpfs 498M 0 498M 0% /dev/shm
tmpfs 498M 6.6M 491M 2% /run
tmpfs 498M 0 498M 0% /sys/fs/cgroup
/dev/vdb1 1.5G 2.3M 1.4G 1% /mnt/vdb1
(4)继续挂载
[root@VM_141_177_centos mnt]# mount /dev/vdb5 /mnt/vdb5
[root@VM_141_177_centos mnt]# mount /dev/vdb6 /mnt/vdb6
[root@VM_141_177_centos mnt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.2G 18G 7% /
devtmpfs 492M 0 492M 0% /dev
tmpfs 498M 0 498M 0% /dev/shm
tmpfs 498M 6.6M 491M 2% /run
tmpfs 498M 0 498M 0% /sys/fs/cgroup
/dev/vdb1 1.5G 2.3M 1.4G 1% /mnt/vdb1
/dev/vdb5 4.4G 9.3M 4.1G 1% /mnt/vdb5
/dev/vdb6 3.9G 8.0M 3.7G 1% /mnt/vdb6
6. 卸载磁盘分区
[root@VM_141_177_centos /]# umount /mnt/vdb6
[root@VM_141_177_centos /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 1.2G 18G 7% /
devtmpfs 492M 0 492M 0% /dev
tmpfs 498M 0 498M 0% /dev/shm
tmpfs 498M 6.6M 491M 2% /run
tmpfs 498M 0 498M 0% /sys/fs/cgroup
/dev/vdb1 1.5G 2.3M 1.4G 1% /mnt/vdb1
/dev/vdb5 4.4G 9.3M 4.1G 1% /mnt/vdb5
(八)LVM配置
1. 进入fdisk命令,配置LVM分区格式
[root@VM_141_177_centos /]# fdisk /dev/vdb
设置指定分区格式为8e(LVM),默认为83(Linux)
Command (m for help): t
Partition number (1,2,5,6, default 6): 6
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Device Boot Start End Blocks Id System
/dev/vdb1 2048 3149823 1573888 83 Linux
/dev/vdb2 3149824 20971519 8910848 5 Extended
/dev/vdb5 3151872 12670487 4759308 83 Linux
/dev/vdb6 12673024 20971519 4149248 8e Linux LVM
Command (m for help): w
2. 配置分区表生效
[root@VM_141_177_centos /]# partprobe
3. 创建PV
[root@VM_141_177_centos /]# pvcreate /dev/vdb6
[root@VM_141_177_centos /]# pvdisplay
或者[root@VM_141_177_centos /]# pvs
4. 创建VG
[root@VM_141_177_centos /]# vgcreate myvg11 /dev/vdb6
[root@VM_141_177_centos /]# vgs
删除VG
[root@VM_141_177_centos /]# vgremove myvg11
[root@VM_141_177_centos /]# vgcreate myvg1 /dev/vdb6
5. 创建LV
[root@VM_141_177_centos /]# lvcreate -L 1024M -n mylv1 myvg1
容量为1024M,命名为mylv1。
[root@VM_141_177_centos /]# lvcreate -L 1024M -n mylv2 myvg1
[root@VM_141_177_centos /]# lvs
删除LV
[root@VM_141_177_centos /]# lvremove /dev/myvg1/mylv2
6. LV格式化
[root@VM_141_177_centos /]# mkfs -t ext3 /dev/myvg1/mylv1
7. 挂载格式化的LV分区
[root@VM_141_177_centos /]# cd /mnt
[root@VM_141_177_centos mnt]# mkdir lvpoint
[root@VM_141_177_centos mnt]# mount /dev/myvg1/mylv1 lvpoint
[root@VM_141_177_centos mnt]# df -h
8. 设置系统启动时启动LVM
[root@VM_141_177_centos etc]# vi /etc/fstab
最后一行添加:
/dev/myvg1/mylv1 /mnt/lvpoint etx3 defaults 1 2
9. 修改LV分区大小
(1)卸载分区
[root@VM_141_177_centos ~]# umount /mnt/lvpoint
[root@VM_141_177_centos ~]# df -h
(2)查看VG和LV大小
[root@VM_141_177_centos ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg1 1 1 0 wz--n- 3.95g 2.95g
[root@VM_141_177_centos ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
mylv1 myvg1 -wi-a----- 1.00g
(3)无需扩展VG,直接扩展LV
[root@VM_141_177_centos ~]# lvextend -L 2G /dev/myvg1/mylv1
Extending logical volume mylv1 to 2.00 GiB
Logical volume mylv1 successfully resized
[root@VM_141_177_centos ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
mylv1 myvg1 -wi-a----- 2.00g
(4)扩展文件系统,并重新挂载
[root@VM_141_177_centos ~]# e2fsck -f /dev/myvg1/mylv1
[root@VM_141_177_centos ~]# resize2fs /dev/myvg1/mylv1
[root@VM_141_177_centos ~]# mount /dev/myvg1/mylv1 /mnt/lvpoint
[root@VM_141_177_centos ~]# df -h
10. 压缩LV分区大小
(1)卸载LV分区
[root@VM_141_177_centos ~]umount /dev/myvg1/mylv1
(2)缩小文件系统
[root@VM_141_177_centos ~]# e2fsck -f /dev/myvg1/mylv1
[root@VM_141_177_centos ~]# resize2fs /dev/myvg1/mylv1 1G
(3)缩小LV容量
[root@VM_141_177_centos ~]# lvreduce -L 1G /dev/myvg1/mylv1
[root@VM_141_177_centos ~]# lvs
(4)重新挂载文件系统
[root@VM_141_177_centos ~]# mount /dev/myvg1/mylv1 /mnt/lvpoint
[root@VM_141_177_centos ~]# df -h
11. 修改VG
(1)fdisk增加一块LVM(8e)格式分区
[root@VM_141_177_centos ~]# fdisk /dev/vdb
Command (m for help): t
Partition number (1,2,5,6, default 6): 5
Hex code (type L to list all codes): 8e
(2)创建新的pv
[root@VM_141_177_centos ~]# pvcreate /dev/vdb5
[root@VM_141_177_centos ~]# pvs
(3)扩展VG
[root@VM_141_177_centos ~]# vgextend myvg1 /dev/vdb5
[root@VM_141_177_centos ~]# vgs
(4)缩小VG
[root@VM_141_177_centos ~]# vgreduce myvg1 /dev/vdb5
[root@VM_141_177_centos ~]# vgs
12. 删除PV
[root@VM_141_177_centos ~]# pvremove /dev/vdb5
[root@VM_141_177_centos ~]# pvs
[root@VM_141_177_centos ~]# fdisk /dev/vdb
恢复linux格式(83)分区
Command (m for help): t
Partition number (1,2,5,6, default 6): 5
Hex code (type L to list all codes): 83
Command (m for help): w
(十)配置路由
1. 配置静态路由
vi /etc/sysconfig/network-scripts/route-eth*
添加内容:
192.168.201.0/24 via 192.168.100.2
重启网络服务:
service network restart