linux操作
来源:互联网 发布:延安文艺座谈会 知乎 编辑:程序博客网 时间:2024/06/17 14:56
说明:
大家需要能完成三个实验:
1.添加网卡,并配置IP,网关和DNS
2.添加磁盘,创建文件系统并挂载
3.基本的LVM实验
文件管理
文件操作
目录操作
创建目录 mkdir mydir
删除目录 rm -rf mydir
修改属组 chown -R user1:group1 /mydir
文件操作
创建文件 touch myfile
删除文件 rm myfile
查看文件 cat myfile
less myfile
修改属组 chown -R user1:group1 myfile
权限设置
查看权限 ls -lrt myfile
修改权限 chmod 754 myfile
文件权限说明
-rwx-rwx-rwx
拥有者 组 其他
r读权限
w写权限
x执行权限
举例:
chmod 754 myfile
4代表读权限,2代表写权限,1代表执行权限,-:对应数值0
7=4 + 2 + 1,表示拥有可读可写可执行权限
5=4 + 1,表示拥有可读可执行权限,但是没有写权限
0 代表没有任何权限
以此类推
用户操作
创建用户
useradd user1 (id,属组,家目录默认)
passwd user1 (设置密码)
useradd -u 2001 -g 2000 user2 (添加用户时同时指定id和组id)
删除用户
userdel -r user1 (-r 同时删除家目录)
查看用户
id user1
cat /etc/passwd
组操作
创建组 gourpadd -g 1000 group1
删除组 gourpdel group1
网络配置
开启ssh
1.查看是否安装ssh包
#rpm -qa | grep ssh
(若没有安装yum install openssh-server安装)
2.开启ssh服务
service sshd restart
3.查看服务端口是否起来
#netstat -antp | grep sshd
4.设置开机启动
#chkconfig sshd on
网络接口配置
添加网卡后查看系统是否识别网卡
ip link show
1,配置DNS
vi /etc/resolv.conf
加入:
nameserver 192.168.1.1
nameserver 8.8.8.8
2,配置网关:
vi /etc/sysconfig/network
加入:
GATEWAY=192.168.0.1
3,配置ip地址:
vi /etc/sysconfig/network-scripts/ifcfg-eth0
其中ifcfg-eth0表时对eth0网卡修改,如果新加网卡要修改eth1的配置文件ifcfg-eth1
内容如下:
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE="eth0"
BOOTPROTO="static" dhcp #修改为收到配置
HWADDR="00:0C:29:4A:FB:7A"
IPV6INIT="no"
NM_CONTROLLED="yes"
ONBOOT="yes" #确认为yes
TYPE="Ethernet"
UUID="3a314364-99f4-48cd-bf84-0ae75d9e4c42"
IPADDR=192.168.142.136 #设置为你需要的IP地址
NETMASK=255.255.255.0 #掩码
GATEWAY=192.168.142.2 #指定网管
DNS1=8.8.8.8 #配置dns
4,重新启动服务:
#service network restart
5,确认网卡的配置信息:
#ifconfig
配置完成后,ping一下网关,检查是否能ping通或在用ifconfig eth0,检查下实际配置的ip地址。
1、使用route命令添加的路由,机器重启或者网卡重启后路由就失效了,方法:
//添加到主机的路由
# route add –host 192.168.1.11 dev eth0
# route add –host 192.168.1.12 gw 192.168.1.1
//添加到网络的路由
# route add –net 192.168.1.11 netmask 255.255.255.0 dev eth0
# route add –net 192.168.1.11 netmask 255.255.255.0 gw 192.168.1.1
# route add –net 192.168.1.0/24 dev eth1
//添加默认网关
# route add default gw 192.168.2.1
//删除路由
# route del –host 192.168.1.11 dev eth0
添加磁盘
用一个实验说明
添加磁盘前
[root@localhost ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
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 identifier: 0x000a419f
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 6275 50088960 83 Linux
/dev/sda3 6275 6528 2031616 82 Linux swap / Solaris
[root@localhost ~]#
添加磁盘
使用vmwareWorkstation + centos6.5 测试
重启主机
#reboot
[root@lb ~]#
[root@lb ~]# fdisk -l #查看是否系统识别了添加的磁盘
Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
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 identifier: 0x000a419f
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 6275 50088960 83 Linux
/dev/sda3 6275 6528 2031616 82 Linux swap / Solaris
Disk /dev/sdb: 21.5 GB, 21474836480 bytes #系统已经识别添加的磁盘
255 heads, 63 sectors/track, 2610 cylinders
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 identifier: 0x00000000
[root@lb ~]#
在新添加的磁盘创建分区
[root@lb ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x946f4430.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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): n 添加分区
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1 指定分区的开头
First cylinder (1-2610, default 1): 回车,使用默认
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): 回车,使用默认
Using default value 2610
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
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 identifier: 0x946f4430
Device Boot Start End Blocks Id System
/dev/sdb1 1 2610 20964793+ 83 Linux 已经创建好的分区
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): w 保存分区表并退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@lb ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
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 identifier: 0x000a419f
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 6275 50088960 83 Linux
/dev/sda3 6275 6528 2031616 82 Linux swap / Solaris
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
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 identifier: 0x946f4430
Device Boot Start End Blocks Id System
/dev/sdb1 1 2610 20964793+ 83 Linux系统已经识别到新分区
[root@lb ~]#
挂载分区
创建文件系统
[root@lb /]# mkfs -t ext3 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5241198 blocks
262059 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
160 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, 1605632, 2654208,
4096000
Writing inode tables: done
Creating journal (32768 blocks):
done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@lb /]#
创建文件夹并挂载
[root@lb ~]# cd /
[root@lb /]# mkdir /data
[root@lb /]# mount /dev/sdb1 /data
[root@lb /]# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 49302836 2951612 43846776 7% /
tmpfs 502304 72 502232 1% /dev/shm
/dev/sda1 297485 33544 248581 12% /boot
/dev/sdb1 20635700 176196 19411268 1% /data已经挂载上
[root@lb /]#
创建文件夹和文件并测试读写文件
[root@lb /]# cd /data
[root@lb data]# mkdir mydir
[root@lb data]# cd mydir/
[root@lb mydir]# touch myfile
[root@lb mydir]# echo helloworld >myfile
[root@lb mydir]# cat myfile
helloworld
[root@lb mydir]# ls /data/mydir/
myfile
[root@lb mydir]# ls -l /data/mydir/
total 4
-rw-r--r--. 1 root root 11 May 15 08:42 myfile
[root@lb mydir]#
LVM
如下图所示PV、VG、LV三者关系:
物理卷的管理
先创建0x8e分区
fdiks
partx
pvcreate
pvscand
pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3
pvdisplay
pvs
pvremove
卷组管理
vgcreate
vgcreate vgs /dev/sdb1 /dev/sdb2 /dev/sdb3
vgscand
vgdisplay
vgs
vgreduce
vgextend
vgextend vgname /dev/sdaN
vgremove
逻辑卷管理
lvcreate
lvcreate -n lv1 -L 1G vg1
mkfs -t ext4 /dev/vg1/lv1
mkdir /data
mount /dev/vg1/lv1 /data
lvscan
lvdisplay
lvextend
lvextend -l +128 /dev/vgname/lvname
resie2fs -p /dev/vgname/lvname
lvreduce
umount /data
fsck -f /dev/vgname/lvname
resize2fs -p /dev/vgname/lvname 512M
lvreduce -L 512M /dev/vgname/lvname 减小到512M
mount /data
lvremove
文件系统空间管理
增加文件空间
1.umount
2.vgextend,lvextend
3.resize2fs
4.mount
缩小文件空间
1.umount
2.resize2fs
3.lvreduce
4.mount
LVM基本实验
实验准备:创建三个分区
[root@lb ~]# fdisk /dev/sdb
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): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4):
Value out of range.
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): 200
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (201-2610, default 201):
Using default value 201
Last cylinder, +cylinders or +size{K,M,G} (201-2610, default 2610): 400
Command (m for help): n
Partition number (1-4): 3
First cylinder (401-2610, default 401):
Using default value 401
Last cylinder, +cylinders or +size{K,M,G} (401-2610, default 2610): 600
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
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 identifier: 0x946f4430
Device Boot Start End Blocks Id System
/dev/sdb1 1 200 1606468+ 83 Linux
/dev/sdb2 201 400 1606500 83 Linux
/dev/sdb3 401 600 1606500 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@lb ~]# partprobe
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy). As a result, it may not reflect all of your changes until after reboot.
[root@lb ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
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 identifier: 0x000a419f
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 6275 50088960 83 Linux
/dev/sda3 6275 6528 2031616 82 Linux swap / Solaris
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
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 identifier: 0x946f4430
Device Boot Start End Blocks Id System
/dev/sdb1 1 200 1606468+ 83 Linux
/dev/sdb2 201 400 1606500 83 Linux
/dev/sdb3 401 600 1606500 83 Linux
[root@lb ~]#
第一步:创建pv
[root@lb ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
[root@lb ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created
[root@lb ~]# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created
[root@lb ~]#
第二步:创建vg
[root@lb ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2 /dev/sdb3
Volume group "vg1" successfully created
[root@lb ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg1 3 0 0 wz--n- 4.58g 4.58g
第三步:创建lv
[root@lb ~]# lvcreate -n lv1 -L 1G vg1
Logical volume "lv1" created
[root@lb ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv1 vg1 -wi-a---- 1.00g
第四步:创建文件系统并挂载
[root@lb ~]# mkfs -t ext4 /dev/vg1/lv1
[root@lb ~]# mount /dev/vg1/lv1 /data
[root@lb ~]#
[root@lb ~]# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 49302836 2951788 43846600 7% /
tmpfs 502304 72 502232 1% /dev/shm
/dev/sda1 297485 33544 248581 12% /boot
/dev/mapper/vg1-lv1 1032088 34052 945608 4% /data
[root@lb ~]#
- linux 操作
- linux 操作
- linux操作
- linux操作
- Linux 操作
- linux 操作
- Linux 操作
- linux操作
- linux操作
- linux操作
- Linux 操作
- linux操作
- 操作linux
- linux 操作
- linux 操作
- Linux 操作
- linux操作
- Linux 操作
- hdu1166+线段树经典题
- activiti 查询流程定义
- drbd是一个用软件实现的,无共享,服务器之间镜像块设备内容的储存复制
- git/github学习笔记-阅读笔记二
- shiro缓存
- linux操作
- (六)API文档介绍
- java synchronized对象锁与类锁的区别、同步代码块与同步方法的区别
- TOMCAT8009端口与AJP13协议
- Hive学习指南(一)
- typeof与instanceof的区别
- iscsi磁盘/储存分享
- c++第六次作业
- eclipse 安装tomcat插件