标签:ges pie force done creat sub += 指定 grep -v
一、定时任务crontab格式
[root@jiangjunwang ~]# crontab -e
#backup log script
0 0 * /bin/tar -jcPf /backup/beifen.$(date +\%F).tar.bz2 /etc/rc.local /etc/sysconfig/ /var/spool/cron /etc/fstab
稍微验证一下
[root@jiangjunwang ~]# date -s 23:59-----?修改系统时间
Wed Aug 29 23:59:00 CST 2018
[root@jiangjunwang ~]# ll /backup/-----?查看是否备份,看看压缩包创建的时间
total 48
-rw-r--r-- 1 root root 47922 Aug 30 00:00 beifen.2018-08-30.tar.bz2
[root@jiangjunwang ~]# tail -f /var/log/cron-------------?通过日志观察是否执行成功
Aug 30 00:00:01 jiangjunwang CROND[3041]: (root) CMD (/bin/tar -jcPf /backup/beifen.$(date +%F).tar.bz2 /etc/rc.local /etc/sysconfig/ /var/spool/cron /etc/fstab 2>&1 )
chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息。谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接。
[root@jiangjunwang ~]# chkconfig --list iptables
iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@jiangjunwang ~]# ll /etc/rc3.d/|grep "iptab"
lrwxrwxrwx 1 root root 18 Aug 26 19:47 K92iptables -> ../init.d/iptables
[root@jiangjunwang ~]# chkconfig iptables on
[root@jiangjunwang ~]# ll /etc/rc3.d/|grep "iptab"
lrwxrwxrwx 1 root root 18 Sep 6 15:42 S08iptables -> ../init.d/iptables
[root@jiangjunwang ~]# head -10 /etc/init.d/iptables
#!/bin/sh
#iptables Start iptables firewall
#chkconfig: 2345 08 92
#description: Starts, stops and saves iptables firewall
由上述结果中我们不难发现,我们在执行chkconfig on或者off的时候,其实只是改变了/etc/rc3.d里边的一个符号链接的的名字而已,其中k表示关闭s表示开启,后边的数字分别是启动的顺序和关闭的顺序。也就是#chkconfig:2345 08 92 的08和92
[root@jiangjunwang ~]# vim /etc/init.d/test
#chkconfig: 2345 88 88
echo jiangjunwang
~
[root@jiangjunwang init.d]# chmod +x test
[root@jiangjunwang init.d]# chkconfig --add test
[root@jiangjunwang init.d]# chkconfig test on
[root@jiangjunwang init.d]# chkconfig --list test
test 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@jiangjunwang init.d]# ll /etc/rc3.d/|grep "test"
lrwxrwxrwx 1 root root 14 Sep 6 16:11 S88test -> ../init.d/test
[root@jiangjunwang skel]# su - jiangjunwang
-bash-4.1$
-bash-4.1$
-bash-4.1$ \cp -rf /etc/skel/.bash* /home/jiangjunwang/
-bash-4.1$ logout
[root@jiangjunwang ~]# su - jiangjunwang
[jiangjunwang@jiangjunwang ~]$
[root@jiangjunwang ~]# md5sum a.txt
53a8f548d07e3d0efff01d6af6943ab4 a.txt
[root@jiangjunwang ~]# md5sum a.txt >police.log
[root@jiangjunwang ~]# md5sum -c police.log
a.txt: OK
[root@jiangjunwang ~]# echo a>>a.txt
[root@jiangjunwang ~]# md5sum -c police.log
a.txt: FAILED
md5sum: WARNING: 1 of 1 computed checksum did NOT match
sudo的配置文件在/etc/sudoers里边,建议使用visudo命令进行编辑该配置文件,因为此命令会对sudoers文件的语法做自检,方便我们及时发现错误.
用户或者组 | 机器=(授权角色) | 可执行的命令 |
---|---|---|
user | MACHINE= | COMMANDS |
jiangjunwang | ALL=(ALL) | NOPASSWD:/bin/ls,/bin/touch |
给jiangjunwang用户授权以root身份执行ls,touch,passwd命令,但是禁止修改root用户密码
[root@jiangjunwang ~]# visudo
jiangjunwang ALL=(root) NOPASSWD:/bin/ls,/bin/touch /usr/bin/passwd [!-]*,!/usr/bin/passwd root
[jiangjunwang@jiangjunwang ~]$ sudo -l
Matching Defaults entries for jiangjunwang on this host:
!visiblepw, always_set_home, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC
KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE",
env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY
LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS
_XKB_CHARSET XAUTHORITY", secure_path=/sbin\:/bin\:/usr/sbin\:/usr/binUser jiangjunwang may run the following commands on this host:
(root) NOPASSWD: /bin/ls, (root) /bin/touch /usr/bin/passwd [!-]*, (root) !/usr/bin/passwd root
[jiangjunwang@jiangjunwang ~]$ sudo ls /etc/passwd
/etc/passwd
[jiangjunwang@jiangjunwang ~]$ sudo passwd
[sudo] password for jiangjunwang:
Sorry, user jiangjunwang is not allowed to execute ‘/usr/bin/passwd‘ as root on jiangjunwang.
[jiangjunwang@jiangjunwang ~]$
注释:NOPASSWD表示用户在执行命令的时候不需要输入密码,[!-]* 表示在输入命令时候必须带参数,因为在这里如果没有这个符号普通用户可以使用sudo passwd 对root用户的密码进行修改。
现在主流的分区的方式有两种——MBR分区和GPT分区,fdisk命令支持的mbr分区方式 parted分区工具支持GPT和MBR的方式
第一步:把硬盘插到服务器上,然后进行分区格式化
[root@jiangjunwang ~]# fdisk -cu /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x174e0a0c.
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)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):
Using default value 209715199Command (m for help): p
Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 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 identifier: 0x174e0a0cDevice Boot Start End Blocks Id System
/dev/sdb1 2048 209715199 104856576 83 LinuxCommand (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.
[root@jiangjunwang ~]# partprobe /dev/sdb
第三步格式化分区;
[root@jiangjunwang ~]# mkfs.ext4 /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
6553600 inodes, 26214144 blocks
1310707 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
800 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, 7962624, 11239424, 20480000, 23887872Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
第四步关闭磁盘自动检测功能
[root@jiangjunwang ~]# tune2fs -c 0 -i 0 /dev/sdb1
tune2fs 1.41.12 (17-May-2010)
Setting maximal mount count to -1
Setting interval between checks to 0 seconds
[root@jiangjunwang ~]# mount /dev/sdb1 /data01/
[root@jiangjunwang ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 17G 3.6G 13G 23% /
tmpfs 932M 0 932M 0% /dev/shm
/dev/sda1 190M 40M 141M 23% /boot
/dev/sdb1 99G 60M 94G 1% /data01
[root@jiangjunwang ~]# vim /etc/fstab
/dev/sdb1 /data01 ext4 defaults 0 0
[root@jiangjunwang ~]# dd if=/dev/zero of=/tmp/test bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB) copied, 0.446025 s, 1.2 GB/s
[root@jiangjunwang ~]# mkswap /tmp/test
mkswap: /tmp/test: warning: don‘t erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 511996 KiB
no label, UUID=aa2a415e-96e6-46e4-84f9-44acad261a96
[root@jiangjunwang ~]# swapon /tmp/test
[root@jiangjunwang ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 3071996 0 -1
/tmp/test file 511996 0 -2
[root@jiangjunwang ~]# vim /etc/rc.local
swapon /tmp/test
选项 | 含义。 | 指令 | 含义 |
---|---|---|---|
-n | 取消默认输出 | p | 打印匹配内容 |
-r | 支持扩展正则 | a | 在匹配行的下一行添加类容 |
-i | 写入文件支持备份 | i | 在匹配行的上一行添加类容 |
s..g | s表示替换g表示全局 |
首先创建一个测试文件
[root@jiangjunwang test]# cat >bb.txt<<EOF
1 ni hao
2 wo hao
3 ta hao
4 da jia hao
5 wo men dou hao
EOF
[root@jiangjunwang test]# sed -n ‘1p‘ bb.txt
1 ni hao
[root@jiangjunwang test]# sed -n ‘2,4p‘ bb.txt
2 wo hao
3 ta hao
4 da jia hao
[root@jiangjunwang test]# sed -n ‘/wo/p‘ bb.txt
2 wo hao
5 wo men dou hao
[root@jiangjunwang test]# sed -n ‘/ni/,/ta/p‘ bb.txt
1 ni hao
2 wo hao
3 ta hao
[root@jiangjunwang test]# sed -nr ‘/ni|ta/p‘ bb.txt
1 ni hao
3 ta hao
PS:sed里面的正则字符左右必须有“/”
[root@jiangjunwang test]# sed -n ‘2p;4p‘ bb.txt
2 wo hao
4 da jia hao
[root@jiangjunwang test]# sed ‘2a xinlai‘ bb.txt
1 ni hao
2 wo hao
xinlai
3 ta hao
4 da jia hao
5 wo men dou hao
[root@jiangjunwang test]# sed ‘/wo hao/axinlai\naa‘ bb.txt
1 ni hao
2 wo hao
xinlai
aa
3 ta hao
4 da jia hao
5 wo men dou hao
ps:\n表示换行
[root@jiangjunwang test]# sed ‘2,$d‘ bb.txt
1 ni hao
[root@jiangjunwang test]# sed ‘/^$/d‘ bb.txt
1 ni hao
2 wo hao
3 ta hao
4 da jia hao
5 wo men dou hao
[root@jiangjunwang test]# sed -n ‘/^$/!p‘ bb.txt
1 ni hao
2 wo hao
3 ta hao
4 da jia hao
5 wo men dou hao
把文件中wo替换成aa
[root@jiangjunwang test]# sed ‘s/wo/aa/g‘ bb.txt
1 ni hao
2 aa hao
3 ta hao
4 da jia hao
5 aa men dou hao
[root@jiangjunwang test]# sed -i.bak ‘s/wo/aa/g‘ bb.txt
[root@jiangjunwang test]# ls
bb.txt bb.txt.bak
[root@jiangjunwang test]# x=wo
[root@jiangjunwang test]# y=ni
[root@jiangjunwang test]# sed "s/$x/$y/g" bb.txt
1 ni hao
2 ni hao
3 ta hao
4 da jia hao
5 ni men dou hao
除了下面5个服务以外都关闭crond sshd network sysstat rsyslog
[root@jiangjunwang test]# chkconfig --list|sed -r ‘s#(.).0.#\1#‘| egrep -v ‘crond|sshd|network|sysstat|rsyslog‘|sed -r ‘s#(.)#chkconfig \1 off#‘|bash
PS:() 扩展正则表达式 在sed中作用,能够记忆它包含的一段正则表达式,并可以通过\1 \2 \3 ... \9 调取出来 用法:sed -r ‘()‘
awk 参数 ‘模式{动作}‘ 文件
awk 参数 ‘条件(找谁){干啥}‘ 文件
首先创建一个测试文件
[root@jiangjunwang test]# cat >>reg.txt<<EOF
Zhang Dandan 41117397 :250:100:175
Zhang Xiaoyu 390320151 :155:90:201
Meng Feixue 80042789 :250:60:50
Wu Waiwai 70271111 :250:80:75
Liu Bingbing 41117483 :250:100:175
Wang Xiaoai 3515064655 :50:95:135
Zi Gege 1986787350 :250:168:200
Li Youjiu 918391635 :175:75:300
Lao Nanhai 918391635 :250:100:175
EOF
[root@jiangjunwang test]# awk ‘/Xiaoyu/{print $1,$2,$3}‘ reg.txt
Zhang Xiaoyu 390320151
*第2列中包含Xiaoyu的行
[root@jiangjunwang test]# awk ‘$2~/Xiaoyu/‘ reg.txt
Zhang Xiaoyu 390320151 :155:90:201
显示所有以41开头的ID号码的人的全名和ID号码
[root@jiangjunwang test]# awk ‘$3~/^41/{print $1,$2,$3}‘ reg.txt
Zhang Dandan 41117397
Liu Bingbing 41117483
[root@jiangjunwang test]# awk ‘$3~/1$|5$/{print $1,$2,$3}‘ reg.txt
Zhang Xiaoyu 390320151
Wu Waiwai 70271111
Wang Xiaoai 3515064655
Li Youjiu 918391635
Lao Nanhai 918391635
[root@jiangjunwang test]# awk ‘$3~/[15]$/{print $1,$2,$3}‘ reg.txt
Zhang Xiaoyu 390320151
Wu Waiwai 70271111
Wang Xiaoai 3515064655
Li Youjiu 918391635
Lao Nanhai 918391635
显示Xiaoyu的捐款.每个值时都有以$开头.如$520$200$135
[root@jiangjunwang test]# awk ‘/Xiaoyu/{gsub(/:/,"$");print $1,$2,$4}‘ reg.txt
Zhang Xiaoyu $155$90$201
BEGIN{} BEGIN里面的内容,会在awk读取文件内容之前运行。
测试,计算。
i=i+1 == i++ 统计次数 计数
i=i+$n == i+=$n 累计相加 累加
1.9 通过awk同时分析access.log文件每个ip的重复数和每个ip使用的流量
[root@jiangjunwang ~]# awk ‘{count[$1]++;sum[$1]+=$10}END{for(pol in sum)print "ip:"pol,"次数:"count[pol],"流量:"sum[pol]}‘ access.log
ip:101.226.61.184 次数:5 流量:53581
ip:27.154.190.158 次数:2 流量:31602
ip:218.79.64.76 次数:2 流量:36438
ip:114.94.29.165 次数:1 流量:491
root@jiangjunwang ~]# awk ‘$9~/root/{i[$9]++}END{for(a in i )print i[a]}‘ secure-20161219
364611
语法:
单分支 if 条件 1;then;动作 fi
双分支 if 条件 1 ;then;动作1 else 动作2 fi
多分支 if 条件1 ;then动做1 elfi 条件2 then;动作2 else 动作3 fi
一个小栗子之命令行比大小
[root@jiangjunwang ~]# cat tesh.sh
#bin/bash
a=$1
b=$2
if [ $# -ne 2 ];then
echo "Please input correct parameters."
exit
fiif [ $a -eq $b ];then
echo "$a=$b"
elif [ $a -gt $b ];then
echo "$a>$b"
else
echo "$a<$b"
fi
小结:
1.条件表达式
[ -d /oldboy ]
[ -f /oldboy/oldboy.txt ]
[ 10 -gt 9 ] great than >
[ 10 -ge 9 ] great equal >=
[ 10 -eq 10 ] equal ==
[ 10 -ne 9 ] not equal !=
[ 9 -lt 90 ] less than <
[ 9 -le 90 ] less equal <=
man test
[ -d /oldboy ] === test -d /oldboy
小结:特殊变量 位置$1 $2 $0 状态$? 参数个数$#
创建十个用户并生成随机密码并把信息追加到一个文件中(一个栗子)
#bin/bash
pass=$(date +%N)
for user in bb{01..10}
do
useradd $user
echo $pass|passwd --stdin $user
echo $user:$pass>>/pass.txt
done
~
[root@jiangjunwang ~]# for i in $(chkconfig --list |egrep -v "crond|sshd|rsyslog|sysstat|network"|awk ‘{print $1}‘); do chkconfig $i off; done
标签:ges pie force done creat sub += 指定 grep -v
原文地址:http://blog.51cto.com/13268236/2172885