[root@es-node03 ~]# netstat -lntup Active Internet connections (only servers) 协议 接收 发送 本地IP地址 远程IP地址 状态 PID Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:8070 0.0.0.0:* LISTEN 3790/python3
LISTEN:(Listening for a connection.)侦听来自远方的TCP端口的连接请求 SYN-SENT:(Active; sent SYN. Waiting for a matching connection request after having sent a connection request.)再发送连接请求后等待匹配的连接请求 SYN-RECEIVED:(Sent and received SYN. Waiting for a confirming connection request acknowledgment after having both received and sent connection requests.)再收到和发送一个连接请求后等待对方对连接请求的确认 ESTABLISHED:(Connection established.)代表一个打开的连接 FIN-WAIT-1:(Closed; sent FIN.)等待远程TCP连接中断请求,或先前的连接中断请求的确认 FIN-WAIT-2:(Closed; FIN is acknowledged; awaiting FIN.)从远程TCP等待连接中断请求 CLOSE-WAIT:(Received FIN; waiting to receive CLOSE.)等待从本地用户发来的连接中断请求 CLOSING:(Closed; exchanged FIN; waiting for FIN.)等待远程TCP对连接中断的确认 LAST-ACK:(Received FIN and CLOSE; waiting for FIN ACK.)等待原来的发向远程TCP的连接中断请求的确认 TIME-WAIT:(In 2 MSL (twice the maximum segment length) quiet wait after close. )等待足够的时间以确保远程TCP接收到连接中断请求的确认 CLOSED:(Connection is closed.)没有任何连接状态
1 2 3 4 5 6
服务器中存在大量 [root@es-node03 ~]# cat /proc/sys/net/ipv4/tcp_fin_timeout 2 #通过缩短时间 time walt时间来快速释放链接 vim /etc/sysctl.conf net.ipv4.tcp_fin_timeout = 2
[root@es-node03 ~]# tar -xf awl-0.2.tar.gz [root@es-node03 ~]# cd awl-0.2/ [root@es-node03 ~/awl-0.2]# ./configure && make -j 4 && make install [root@es-node03 ~/awl-0.2]# which awl /usr/local/bin/awl
[root@es-node03 ~/awl-0.2]# ping 192.168.200.153 PING 192.168.200.153 (192.168.200.153) 56(84) bytes of data. 64 bytes from 192.168.200.153: icmp_seq=1 ttl=64 time=0.322 ms [root@es-node03 ~]# arp -n | grep 153 192.168.200.153 ether 00:50:56:9d:0b:0b C ens160 #获取目标主机mac地址
[root@felix ~]# cat /etc/login.defs # # Please note that the parameters in this configuration file control the # behavior of the tools from the shadow-utils component. None of these # tools uses the PAM mechanism, and the utilities that use PAM (such as the # passwd command) should therefore be configured elsewhere. Refer to # /etc/pam.d/system-auth for more information. #
# *REQUIRED* # Directory where mailboxes reside, _or_ name of file, relative to the # home directory. If you _do_ define both, MAIL_DIR takes precedence. # QMAIL_DIR is for Qmail # #QMAIL_DIR Maildir MAIL_DIR /var/spool/mail <==创建用户时,要在目录/var/spool/mail中创建一个用户mail文件 #MAIL_FILE .mail
# Password aging controls: # # PASS_MAX_DAYS Maximum number of days a password may be used. # PASS_MIN_DAYS Minimum number of days allowed between password changes. # PASS_MIN_LEN Minimum acceptable password length. # PASS_WARN_AGE Number of days warning given before a password expires. # PASS_MAX_DAYS 99999 <==密码最大有效期 PASS_MIN_DAYS 0 <==两次修改密码的最小间隔时间 PASS_MIN_LEN 5 <==密码最小长度,对于root无效 PASS_WARN_AGE 7 <==密码过期前多少天开始提示
# # Min/max values for automatic uid selection in useradd #创建用户时不指定UID的话自动UID的范围 UID_MIN 1000 UID_MAX 60000 # System accounts<==虚拟用户UID范围 SYS_UID_MIN 201 SYS_UID_MAX 999
# # Min/max values for automatic gid selection in groupadd #自动组ID的范围 GID_MIN 1000 GID_MAX 60000 # System accounts SYS_GID_MIN 201 SYS_GID_MAX 999
# # If defined, this command is run when removing a user. # It should remove any at/cron/print jobs etc. owned by # the user to be removed (passed as the first argument). # #USERDEL_CMD /usr/sbin/userdel_local <==当删除用户的时候执行的脚本,默认关闭
# # If useradd should create home directories for users by default # On RH systems, we do. This option is overridden with the -m flag on # useradd command line. # CREATE_HOME yes <==是否创建家目录,可用-m控制
# The permission mask is initialized to this value. If not specified, # the permission mask will be initialized to 022. UMASK 077 <==家目录对对应的umask值
# This enables userdel to remove user groups if no members exist. # USERGROUPS_ENAB yes <==删除用户时,同时删除对应组。(如果组内没有其他成员)
# Use SHA512 to encrypt password. ENCRYPT_METHOD SHA512 <==密码的加密算法SHA512 注意: 1.给开发人员等添加用户,尽量给截止时间。
修改用户:
删除用户:
生产环境中一般先注释passwd中该用户的所属行,不立刻进行删除。
设置密码:
–stdin 从标准输入接受密码并设置
不交互设置密码: 方法1:
1 2 3
[root@oldboyedu ~]# echo 123456|passwd --stdin oldgirl Changing password for user oldgirl. passwd: all authentication tokens updated successfully.
方法2:
1 2 3 4 5 6
[root@oldboyedu ~]# echo 123456 >pass [root@oldboyedu ~]# cat pass 123456 [root@oldboyedu ~]# passwd --stdin oldgirl <pass Changing password for user oldgirl. passwd: all authentication tokens updated successfully.
输出: Changing password for user felix01. passwd: all authentication tokens updated successfully. Changing password for user felix02. passwd: all authentication tokens updated successfully. Changing password for user felix03. passwd: all authentication tokens updated successfully. Changing password for user felix04. passwd: all authentication tokens updated successfully. Changing password for user felix05. passwd: all authentication tokens updated successfully. Changing password for user felix06. passwd: all authentication tokens updated successfully. Changing password for user felix07. passwd: all authentication tokens updated successfully. Changing password for user felix08. passwd: all authentication tokens updated successfully. Changing password for user felix09. passwd: all authentication tokens updated successfully. Changing password for user felix10. passwd: all authentication tokens updated successfully.
[root@felix ~]# chage -l felix01 Last password change : Oct 17, 2019 <==密码最近修改时间 2019-10-17 -d参数可控制该行
Password expires : never <==密码过期时间,从来不过期 -M参数控制该行
Password inactive : never <==密码停权时间 -l参数控制该行
Account expires : never <==账户过期时间 -E参数控制该行
Minimum number of days between password change : 0 <==修改密码最小间隔天数 -m参数控制该行
Maximum number of days between password change : 99999 <==最长时间 -M控制该行
Number of days of warning before password expires : 7 <==密码过去提前几天进行警告 -W控制该行
[root@felix ~]# chage -E "2020-10-1||$(date +%F -d'10day')" felix01 <==修改账户过期时间,或十天后停权 [root@felix ~]# chage -l felix01 Last password change : Oct 17, 2019 Password expires : never Password inactive : never Account expires : Oct 01, 2020 * Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7
其他用户相关命令:
1 2 3 4 5
id#查看用户身份 whoami#查看当前用户 w #查看所以登陆用户 last #显示登陆过的用户信息列表 lastlog #汇报最近货指定用户的登录情况
[felix01@felix ~]$ ls /root ls: cannot open directory /root: Permission denied
[root@felix ~]# visudo 97 ## Allow root to run any commands anywhere 98 root ALL=(ALL) ALL 99 felix01 ALL=(ALL) /bin/ls,/bin/cp 用户 主机=(角色) 命令(全路径 which),多个命令用,隔开 99 felix ALL=(ALL) ALL //相当于felix设置成管理员 99 felix ALL=(ALL) NOPASSWD: ALL //sudo去密码 [root@felix ~]# sudo su - Last login: Thu Oct 17 15:30:38 CST 2019 from 10.93.151.253 on pts/0 [root@felix ~]# 禁止root远程连接
[root@felix ~]# ll /var/db/sudo/felix/1 <==密码过期时间戳文件,默认5分钟过期 -rw------- 1 root felix 48 2019-10-17 16:25 /var/db/sudo/felix/1 [felix@felix ~]$ sudo -k <==清除时间戳,使sudo提示输入密码 [felix@felix ~]$ sudo ls /root [sudo] password for felix:
[root@lihuaning felix]# cat test.txt <==环境准别 I am oldboy teacher! i teach linux I like badminton ball billiard ball and chinese chess oursiteishttp://www.oldboyedu.com my qq num is 49000448 not49000e448. my god ,i am not oldbey, but OLDBOY [root@lihuaning felix]# grep "^n" test.txt <==匹配以n开头的行 not49000e448.
[root@lihuaning felix]# grep -n "^$" test.txt <==匹配空行 4: 7: [root@lihuaning felix]# grep -nv "^$" test.txt <==通过-v参数取反输出,则去除了文件的空行 1:I am oldboy teacher! 2:i teach linux 3:I like badminton ball billiard ball and chinese chess 5:oursiteishttp://www.oldboyedu.com 6:my qq num is 49000448 8:not49000e448. 9:my god ,i am not oldbey, but OLDBOY
. 表示匹配任意一个字符,有且只有一个。类似于通配符中的 ?
1 2 3 4 5 6 7 8
[root@lihuaning felix]# grep "." test.txt <==匹配到了任意字符,所有字符都匹配上了 I am oldboy teacher! i teach linux I like badminton ball billiard ball and chinese chess oursiteishttp://www.oldboyedu.com my qq num is 49000448 not49000e448. my god ,i am not oldbey, but OLDBOY
\ 表示转义
1 2 3 4 5 6 7 8 9 10
[root@lihuaning felix]# grep ".$" test.txt <==匹配以任意字符结尾的行 I am oldboy teacher! i teach linux I like badminton ball billiard ball and chinese chess oursiteishttp://www.oldboyedu.com my qq num is 49000448 not49000e448. my god ,i am not oldbey, but OLDBOY [root@lihuaning felix]# grep "\.$" test.txt <==转意后 匹配以点结尾的行 not49000e448.
* 表示重复前面字符 0次或多次
1 2 3 4 5 6 7 8 9 10
[root@lihuaning felix]# grep "0*" test.txt <==由于*表示匹配0次或多次,所以文件未匹配上的(即0次)也一并输出了 I am oldboy teacher! i teach linux I like badminton ball billiard ball and chinese chess
oursiteishttp://www.oldboyedu.com my qq num is 49000448
not49000e448. my god ,i am not oldbey, but OLDBOY
.*包含空行在内的所有行,加上-n参数显示所有行的行号 等价于cat -n
1 2 3 4 5 6 7 8 9 10
[root@lihuaning felix]# grep -n ".*" test.txt 1:I am oldboy teacher! 2:i teach linux 3:I like badminton ball billiard ball and chinese chess 4: 5:oursiteishttp://www.oldboyedu.com 6:my qq num is 49000448 7: 8:not49000e448. 9:my god ,i am not oldbey, but OLDBOY
^.* 表示匹配以任意多个字符开头的行
1 2 3
[root@lihuaning ~]# grep "^.*boy" felix/test.txt <==匹配以任意字符开头 以boy结尾的行。相当于通配符里的 *boy I am oldboy teacher! oursiteishttp://www.oldboyedu.com
.*$ 表示匹配以任意多个字符结尾的行
1 2 3
[root@lihuaning ~]# grep "am.*$" felix/test.txt <==匹配以任意字符结尾但开头是am 的行 I am oldboy teacher! my god ,i am not oldbey, but OLDBOY
[abc]匹配带有 或a或b或c任意一个的行
1 2 3
[root@lihuaning ~]# grep "[qg]" felix/test.txt <==匹配带有q 或者 g的行 ^不能用感叹号替换(会被当做字符) my qq num is 49000448 my god ,i am not oldbey, but OLDBOY
[^abc]取反
1 2 3 4 5 6 7 8
[root@lihuaning ~]# grep "[^qg]" felix/test.txt <==取反 或q或g I am oldboy teacher! i teach linux I like badminton ball billiard ball and chinese chess oursiteishttp://www.oldboyedu.com my qq num is 49000448 not49000e448. my god ,i am not oldbey, but OLDBOY
[root@db02 ~]# set -- "I am" itsix felix [root@db02 ~]# echo $1 I am [root@db02 ~]# echo $2 itsix [root@db02 ~]# echo $3 felix [root@db02 ~]# echo $# 3 [root@db02 ~]# echo $* I am itsix felix [root@db02 ~]# echo $@ I am itsix felix
#类似sed替换 ${parameter/pattern/string} 使用string代替第一个匹配的pattern ${parameter//pattern/string} 使用string代替所有匹配的pattern [root@db02 ~]# OLDBOY="I am Felix. yes. Felix." [root@db02 ~]# echo $OLDBOY I am Felix. yes. Felix. [root@db02 ~]# echo ${OLDBOY/Felix/bangbang} I am bangbang. yes. Felix. [root@db02 ~]# echo ${OLDBOY//Felix/bangbang} I am bangbang. yes. bangbang.
获取变量内容长度的方法:
1 2 3 4 5 6 7 8 9
echo$OLDBOY|wc -L
echo${#OLDBOY}
expr length "$OLDBOY"
echo$OLDBOY|awk '{print length}'
echo$OLDBOY|awk '{print length($0)}'
练习:句子中小于6个字符的单词打印出来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Whatever is worth doing is worth doing well. [root@db02 ~]# sh ww.sh is worth doing is worth doing well. [root@db02 ~]# cat ww.sh judge="Whatever is worth doing is worth doing well." for word in$judge do if [ ${#word} -lt 6 ] then echo"$word" fi done
[root@KFSAMBA ~]# sh function.sh I am felix [root@KFSAMBA ~]# cat function.sh #/bin/bash felix(){ echo"I am felix" } felix
第一步:
1 2 3 4 5 6 7 8
[root@KFSAMBA ~]# sh function.sh I am 第一个参数 [root@KFSAMBA ~]# cat function.sh #/bin/bash felix(){ echo"I am $1" } felix 第一个参数 <==这个参数传递给了函数felix,felix函数中再通过$1进行带入
第二步:
1 2 3 4 5 6 7 8 9 10 11 12 13
[root@KFSAMBA ~]# sh function.sh 第一 I am 第一 [root@KFSAMBA ~]# cat function.sh #/bin/bash felix(){ echo"I am $1" <==2.这里的$1是指函数的第一个参数。此脚本中函数的第一个参数就是脚本的第一个参数 } felix $1 <==1.这里的$1是指脚本的第一个参数,将用户传入脚本的第一个参数传给函数
函数内定义私有变量: 函数(){ local s=1 }
检查url是否正常
检查url是否正常
1 2 3 4 5 6 7 8 9 10 11 12
[root@KFSAMBA ~]# sh check_url.sh baidi.com URL is no [root@KFSAMBA ~]# cat check_url.sh #!/bin/bash wegt -q $1 &>/dev/null <==-q 不显示输出 retval=$? if [ $retval -eq 0 ] <==判断返回值 then echo"URL is ok" else echo"URL is no" fi
start(){ if [ -f $lock_file_path ] then exit 0 fi $sbin -c $conf &> /dev/null gather=$? if [ $gather -eq 0 ];then log_success_msg "Nginx is started." [ -w $lockdir ] && { touch$lock_file_path return$gather }||{ log_failure_msg "Nginx lockfile denied" return 1 } else log_failure_msg "Nginx startd failed" return$gather fi }
stop(){ if [ -s $nginx_pid_file_path ] then nginx_pid=$(cat"$nginx_pid_file_path") if (kill -0 $nginx_pid &>/dev/null) then $sbin -s stop gather=$? [ $gather -eq 0 ]&&{ log_success_msg "Nginx Stop" if [ -f "$lock_file_path" ] then rm"$lock_file_path" fi return$gather }||{ log_failure_msg "Nginx Stop" return$gather } else log_failure_msg "Nginx service has not been started" rm"$nginx_pid_file_path" fi else log_failure_msg "Nginx service PID file is null or not exist!" return 1 fi }
reload(){ $sbin -t &> /dev/null if [ $? -eq 0 ] then kill -HUP $(cat"$nginx_pid_file_path") log_success_msg "Nginx Reload" fi return $? }
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # Check if user is root [ $(id -u) != "0" ] && { echo -e "\033[31mError: You must be root to run this script\033[0m"; exit 1; } clear
[ ! -d felix ] && mkdir -p felix cd felix/ read -p "How many do I need to create: " x c=`seq$x` for n in$c do prefix=`date +%N|md5sum| echo$RANDOM|md5sum|cut -c 1-6|tr'[0-9]''[a-z]'` touch${prefix}_felix.html echo$n done
批量创建随机文件 [ ! -d felix ] && mkdir -p felix cd felix/ read -p "How many do I need to create: " x c=`seq$x` for n in$c do prefix=`date +%N|md5sum|cut -c 1-6|tr'[0-9]''[a-z]'` touch${prefix}_felix.html echo$n done
批量修改文件 方法1:for循环 files=`ls felix/` cd felix for file in$files do mv$file${file//felix.html/feieileks.HTML} done
方法2:rename命令 rename from to towards 例:飞驰.html 改成马飞飞.html rename 飞驰.html 马飞飞.html *飞驰.html 源文件名 目标文件名 针对哪些文件进行修改
#!/bin/bash ################################################### # File Name: ctrl_ddos.sh # Created Time: Tue 14 Jan 2020 02:36:15 PM CST # Version: V1.0 # Author: Felix # Organization: 360JR OPS ################################################### [ -f /etc/init.d/functions ] && . /etc/init.d/functions log=/tmp/ip.log pan(){ if [ $1 -eq 0 ] then action "$2" /bin/true else action "$2" /bin/false fi }
defense(){ netstat -na | awk -F'[ :]+''$NF~/ESTABLISHED/{print $(NF-2)}'|sort|uniq -c|sort -nr|head -10 > $log && echo --operation: $i-- whileread line do ip=`echo$line|awk '{print $(NF)}'` zahl=`echo$line|awk '{print $(NF-1)}'` fw=`iptables -nL|grep -w $ip |wc -l` if [ $zahl -gt 10 ] && [ $fw -eq 0 ] then pan 1 "Source IP: $ip" iptables -I INPUT -s $ip -j DROP else pan 0 "Source IP: $ip" fi done<$log ((i++)) }
main(){ whiletrue do sleep 5 defense done } while :;do read -p"是否启动DDOS主动防御 [Y/n] " n [ -n ${n:=y} ] && break done gather=`echo$n|tr [A-Z] [a-z]` if [ "${gather}" = "y" ] then pan 0 "已开启" main else pan 1 "$gather 未识别" exit 1 fi
单词长度判断
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#判断句子中的单词是否大于三个字母 log="To be or not to be that is a question" echo$log for l in$log do if [ ${#l} -le 3 ] then echo"这是不大于3个字母的单词" echo$l else echo"这是大于3个字母的单词" echo$l fi done
echo"计算中..." for x in `seq 0 32767` do pass=`echo$x|md5sum|cut -c 1-10` <==此处有小问题 if [ "$pass" == "f4cb084dc0" ] then echo"Calculate the number to be $x!" break fi done
正解:
1 2 3 4 5 6 7 8 9 10
echo"计算中..." for x in `seq 0 32767` do pass=`echo$x|md5sum` if [[ $pass =~ ^.*f4cb084dc0.*$ ]] #正则匹配 then echo"Calculate the number to be $x!" break fi done
#数组改编判断单词长度 array=(1 2 3 4 5) log=(To be or not to be that is a questio) i=0 while [ $i -lt ${#log[@]} ] do if [ ${#log[i]} -le 3 ] then echo"此单词 ${log[i]},小于等于3" else echo"此单词 ${log[i]},大于3" fi ((i++)) done
length=(To be or not to be that is a questio) echo"${length[*]}" for((i=0;i<${#length[*]};i++)) do if [ ${#length[i]} -ge 3 ] then echo"${length[i]}" fi done
#检查url #!/bin/bash [ -f /etc/init.d/functions ] && . /etc/init.d/functions url_arr=( https://www.baidu.com/ https://www.qq.com/ https://www.jd.com/ ) pan(){ if [ $1 -eq 0 ] then action "$2" /bin/true else action "$2" /bin/false fi } check_url(){ wget -q -o /dev/null --spider --tries=1 -T 5 $1 retval=$? if [ $retval -eq 0 ] then pan 0 "$1" else pan 1 "$1" fi } loop(){ for url in$@ do check_url $url sleep 2 done } main(){ while :;do loop ${url_arr[*]} done } main
什么是Raid? Raid是廉价冗余磁盘阵列( Redundant Array of Inexpensive Disk)的简称,有时也简称磁盘阵列( Disk Array)Raid是一种把多块独立的物理磁盘按不同的技术方式组合起来形成一个磁盘组,在逻辑上(做完RAID,装系统后)看起来就是一块大的磁盘,可以提供比单个物理磁盘更大的存储容量或更高的存储性能,同时又能提供不同级别数据冗余备份的一种技术。
分区实战:3P+1E(3L) 1 2 3 5 6 7 每个150M [root@S1 ~]# ll /dev/sd* brw-rw----. 1 root disk 8, 0 Oct 20 23:59 /dev/sda brw-rw----. 1 root disk 8, 1 Oct 20 23:59 /dev/sda1 brw-rw----. 1 root disk 8, 2 Oct 20 23:59 /dev/sda2 brw-rw----. 1 root disk 8, 3 Oct 20 23:59 /dev/sda3 brw-rw----. 1 root disk 8, 16 Oct 20 23:59 /dev/sdb [root@S1 ~]# fdisk -l <==查看分区信息
Disk /dev/sda: 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: 0x000ed3d3
Device Boot Start End Blocks Id System /dev/sda1 * 2048 1050623 524288 83 Linux /dev/sda2 1050624 5244927 2097152 82 Linux swap / Solaris /dev/sda3 5244928 41943039 18349056 83 Linux
[root@S1 ~]# fdisk /dev/sdb <==对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 0x8ecef2f8.
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 <==转换硬盘格式为gpt 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'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): 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: 0x5faeac4d
Device Boot Start End Blocks Id System
=======================开始创建主分区========================== Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Using default response p Partition number (1-4, default 1): First sector (2048-2097151, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +150M Partition 1 of type Linux and of size 150 MiB is set
Command (m for help): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p Partition number (2-4, default 2): First sector (309248-2097151, default 309248): Using default value 309248 Last sector, +sectors or +size{K,M,G} (309248-2097151, default 2097151): +150M Partition 2 of type Linux and of size 150 MiB is set
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): First sector (616448-2097151, default 616448): Using default value 616448 Last sector, +sectors or +size{K,M,G} (616448-2097151, default 2097151): +150M Partition 3 of type Linux and of size 150 MiB is set
=======================开始创建扩展分区========================== Command (m for help): n Partition type: p primary (3 primary, 0 extended, 1 free) e extended Select (default e): Using default response e Selected partition 4 First sector (923648-2097151, default 923648): Using default value 923648 Last sector, +sectors or +size{K,M,G} (923648-2097151, default 2097151): <==给剩余全部空间 Using default value 2097151 Partition 4 of type Extended and of size 573 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: 0x5faeac4d
Device Boot Start End Blocks Id System /dev/sdb1 2048 309247 153600 83 Linux /dev/sdb2 309248 616447 153600 83 Linux /dev/sdb3 616448 923647 153600 83 Linux /dev/sdb4 923648 2097151 586752 5 Extended
=======================开始创建逻辑分区========================== Command (m for help): n All primary partitions are in use Adding logical partition 5 First sector (925696-2097151, default 925696): Using default value 925696 Last sector, +sectors or +size{K,M,G} (925696-2097151, default 2097151): +150M Partition 5 of type Linux and of size 150 MiB is set
Command (m for help): n All primary partitions are in use Adding logical partition 6 First sector (1234944-2097151, default 1234944): Using default value 1234944 Last sector, +sectors or +size{K,M,G} (1234944-2097151, default 2097151): +150M Partition 6 of type Linux and of size 150 MiB is set
Command (m for help): n All primary partitions are in use Adding logical partition 7 First sector (1544192-2097151, default 1544192): Using default value 1544192 Last sector, +sectors or +size{K,M,G} (1544192-2097151, default 2097151): Using default value 2097151 Partition 7 of type Linux and of size 270 MiB is set
=======================查看结果3P 1E(3L)========================== 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: 0x5faeac4d
Device Boot Start End Blocks Id System /dev/sdb1 2048 309247 153600 83 Linux /dev/sdb2 309248 616447 153600 83 Linux /dev/sdb3 616448 923647 153600 83 Linux /dev/sdb4 923648 2097151 586752 5 Extended /dev/sdb5 925696 1232895 153600 83 Linux /dev/sdb6 1234944 1542143 153600 83 Linux /dev/sdb7 1544192 2097151 276480 83 Linux
[root@S1 ~]# parted /dev/sdb GNU Parted 3.1 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) p Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 1074MB Sector size (logical/physical): 512B/512B Partition Table: msdos <==此时文件系统为msdos Disk Flags:
Number Start End Size Type File system Flags
(parted) mklabel gpt <==修改磁盘为GPT格式[MBR格式为 msdos ] 等价于:parted -s /dev/sdb mklabel gpt <==非交互式磁盘为GPT格式,-s取消提示 Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No? Y (parted) p Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 1074MB Sector size (logical/physical): 512B/512B Partition Table: gpt <==修改结果 Disk Flags:
Number Start End Size File system Name Flags
(parted) mkpart primary 0 124 <==新建分区定义起始大小(M)为单位 等价于:parted -s /dev/sdb mkpart primary 0 124 Warning: The resulting partition is not properly aligned for best performance. Ignore/Cancel? I (parted) p <==显示分区信息 Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 1074MB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags:
Number Start End Size File system Name Flags 1 17.4kB 124MB 124MB primary
(parted) mkpart primary 581 -1 <==分配所有剩余空间给第2个分区 等价于:parted -s /dev/sdb mkpart primary 581 100% (parted) p Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 1074MB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags:
Number Start End Size File system Name Flags 1 17.4kB 580MB 580MB primary 2 581MB 1073MB 492MB primary
[root@S1 ~]# mkfs -t ext4 /dev/sdb1 <==格式化ext4文件系统,本质就是生成一定数量的Inode和Block。等价于mkfs.exts /dev/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 25688 inodes, 102400 blocks 5120 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=33685504 13 block groups 8192 blocks per group, 8192 fragments per group 1976 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@S1 ~]# mkfs -t ext4 -b 4096 -I 512 /dev/sdb1 <==指定Inode和Block大小 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 25600 inodes, 25600 blocks 1280 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=27262976 1 block group 32768 blocks per group, 32768 fragments per group 25600 inodes per group
Allocating group tables: done Writing inode tables: done Creating journal (1024 blocks): done Writing superblocks and filesystem accounting information: done