iptables
表示filter 表,表下面有链,链下面有规则,查看规则用-nvL #iptables -t filter -nvL
查看filter下面的规则
netfilter --> iptables
[root@abinlinux ~]# iptables -t filter -nvL filter 的表
Chain INPUT (policy ACCEPT 640 packets, 50115 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
Chain OUTPUT (policy ACCEPT 518 packets, 119K bytes)
pkts bytes target prot opt in out source destination
[root@abinlinux ~]# iptables -t nat -nvL nat的表
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
[root@abinlinux ~]# iptables -t mangle -nvL mangle 的表
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
最主要还是filter 表 ,主要是过滤包的,网卡无非就是进包和出包,针对进包出包做个规则。 INPUT OUTPUT 这两个用的是最多的
[root@binbinlinux ~]# iptables -t filter -I INPUT -p tcp --dport 80 -s 12.12.12.12 -j REJECT
添加规则 iptanles -t filter 指定这个表 -I INPUT 对input表进行操作 I 是插入,-p tcp 指定tcp的包 --dport 到达的端口 是80 -s 12.12.12.12限定来源ip -j对这个包进行什么样的行为和操作 -j DROP 把这个包扔掉 -j REJECT 也可以拒绝
查看添加的规则 iptables -t filter -nvL iptables -nvL 默认就是filter的表
[root@binbinlinux ~]# iptables -t filter -D INPUT -p tcp --dport 80 -s 12.12.12.12 -j REJECT
-D 就是把规则删除掉
[root@binbinlinux ~]# iptables -t filter -A INPUT -p tcp --dport 80 -s 12.12.12.12 -j REJECT
-A是增加一条规则 (-I 会增加到上面 ,-A 会增加到下面 。比如下面是10 上面是20发生了改变上面先生效)-I越往后添加的越先生效 -A 越往后添加的规则越后生效
OUTPUT -d 192.168.1.108 --sport 80
[root@binbinlinux ~]# vim /etc/sysconfig/iptables 在这个配置文件添加规则生效
(-s是来源ip -d是目标ip,INPUT是和s有关的,output是和d有关的 ) iptables -F 是清空所有的规则
iptables 详解2
[root@binbinlinux ~]# iptables -t filter -I INPUT -p tcp --dport 80 -s 12.12.12.12 -j DROP扔掉
[root@binbinlinux ~]# iptables -t filter -I INPUT -p tcp --dport 80 -s 12.12.12.12 -j ACCEPT
[root@binbinlinux ~]# iptables -t filter -I INPUT -p tcp --dport 80 -s 12.12.12.12 -j REJECT 拒绝
[root@binbinlinux ~]# iptables -Z 把数据重置成零
pkta就是包的数量 bytes 包的字节大小 ,(source source ip ) (destination 原ip) [root@binbinlinux ~]# iptables -F 清空规则
不管是清空还是 -nvL都是filter 表
[root@binbinlinux ~]# service iptables restart 重启防火墙规则
iptables:将链设置为政策 ACCEPT:mangle nat filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]
iptables:应用防火墙规则: [确定]
[root@binbinlinux ~]# service iptables save 保存防火墙 添加规则不保存下次开机会丢失
iptables:将防火墙规则保存到 /etc/sysconfig/iptables: [确定]
/etc/sysconfig/iptables 用vim打开 这个配置文件进行添加规则
可以cat /etc/sysconfig/iptables 查看配置规则
[root@binbinlinux ~]# iptables -t filter -I INPUT -p tcp --dport 80 -s 12.12.12.12 -j ACCEPT
[root@binbinlinux ~]# iptables-save > 1.ipt 重定向备份下
[root@binbinlinux ~]# cat 1.ipt 可以查看到规则
*filter
:INPUT ACCEPT [71:5367]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [33:3640]
-A INPUT -s 12.12.12.12/32 -p tcp -m tcp --dport 80 -j ACCEPT
COMMIT
iptables -F 然后反向重定向
[root@binbinlinux ~]# iptables-restore < 1.ipt
[root@binbinlinux ~]# iptables -nvL
Chain INPUT (policy ACCEPT 27 packets, 2090 bytes)
0 0 ACCEPT tcp -- 12.12.12.12 0.0.0.0/0 tcp dpt:80
Chain OUTPUT (policy ACCEPT 15 packets, 1592 bytes)
net 公私ip转换
mangle 给包打标记
ask.apelearn.com/question/7255 参考数据
iptables -t nat -A PREROUTING -d dest IP -j DNAT --to-destination new dest IP
iptables -t nat -A POSTROUTING -s source IP -j SNAT --to-source new source new source IP
[root@binbinlinux ~]# iptables -P INPUT ACCEPT
Chain INPUT (policy ACCEPT 26 packets, 2039 bytes)
cron 计划任务
[root@binbinlinux ~]# crontab -l 可以查看计划任务
[root@binbinlinux ~]# crontab -u root -l 查看指定用户的计划任务
no crontab for root
[root@binbinlinux ~]# crontab -e 更改计划任务
min hou day mon week
分 时 日 月 周
cat /var/spool/cron/root 这是root的crontab
系统服务 ntsysv yum install -y ntsysv
[root@binbinlinux ~]# ntsysv 开更改服务 按空格清除
[root@binbinlinux ~]# chkconfig --list 列出来都有哪些服务
auditd 0:关闭 1:关闭 2:启用 3:关闭 4:启用 5:启用 6:关闭
crond 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
htcacheclean 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
httpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
ip6tables 0:关闭 1:关闭 2:启用 3:关闭 4:启用 5:启用 6:关闭
iptables 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
kdump 0:关闭 1:关闭 2:关闭 3:关闭 4:启用 5:启用 6:关闭
mdmonitor 0:关闭 1:关闭 2:启用 3:关闭 4:启用 5:启用 6:关闭
messagebus 0:关闭 1:关闭 2:启用 3:关闭 4:启用 5:启用 6:关闭
netconsole 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
netfs 0:关闭 1:关闭 2:关闭 3:关闭 4:启用 5:启用 6:关闭
network 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
ntpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
ntpdate 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
postfix 0:关闭 1:关闭 2:启用 3:关闭 4:启用 5:启用 6:关闭
rdisc 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
restorecond 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
rpcbind 0:关闭 1:关闭 2:启用 3:关闭 4:启用 5:启用 6:关闭
rsyslog 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
saslauthd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
sshd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
svnserve 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
sysstat 0:关闭 1:启用 2:启用 3:启用 4:启用 5:启用 6:关闭
udev-post 0:关闭 1:启用 2:启用 3:关闭 4:启用 5:启用 6:关闭
ypbind 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
[root@binbinlinux ~]# chkconfig auditd off 关闭一个 auditd 的命令
[root@binbinlinux ~]# chkconfig --list auditd 查看已经关闭的 命令
auditd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
[root@binbinlinux ~]# chkconfig auditd on 开启命令
[root@binbinlinux ~]# chkconfig --list auditd
auditd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
chkconfig 可以手动的开关某个服务 也可以增加删除某些服务
[root@binbinlinux ~]# chkconfig --level 345 atd on 打开345 端口的用法 也可以的单个打开
默认在2345 下面可以关闭开启
[root@binbinlinux init.d]# cp auditd 123 创建一个
[root@binbinlinux init.d]# ls -l 123 查看
-rwxr-xr-x 1 root root 3580 11月 8 01:09 123
[root@binbinlinux init.d]# chkconfig --list |grep 123 过滤一下
[root@binbinlinux init.d]# chkconfig --add 123 添加进去
[root@binbinlinux init.d]# chkconfig --list |grep 123 过滤出添加的任务
123 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@binbinlinux init.d]# chkconfig --del 123 删除命令
[root@binbinlinux init.d]# chkconfig --list |grep 123 没有了
linux日志
[root@binbinlinux ~]# ls /var/log/messages 系统里最核心的日志
/var/log/messages
[root@binbinlinux ~]# cat /etc/logrotate.conf 日志切割的配置文件
weekly 表示日志每周切割一次
rotate 4 切割之后只保留四个 create 生成一个新的 dateext 以日期命名
#compress 是没有打开的 打开的话会把日志压缩一下
include /etc/logrotate.d 在这个文件下还有很多相关的配置文件
/var/log/wtmp {
monthly 按月进行归档
create 0664 root utmp 权限664 属主root 属组是utmp
minsize 1M 最小的大小是1M 小于1M 就不进行切割了
rotate 1 只保留一个备份
[root@binbinlinux ~]# kill pid 可以把 一个进程杀死
kill -9 pid
kill -hup 从新加载下文件
killall 跟进程的名字直接杀死
[root@binbinlinux ~]# tail /var/log/message 获得系统内核相关的日志
[root@binbinlinux ~]# ls /var/log/wtmp 查看登录信息 不可以cat 查看
/var/log/wtmp
[root@binbinlinux ~]# last 用last查看
root pts/0 192.168.1.102 Mon Nov 7 18:24 still logged in
root tty1 Mon Nov 7 18:23 still logged in
reboot system boot 2.6.32-573.el6.x Mon Nov 7 18:23 - 01:55 (07:32)
root pts/1 192.168.1.105 Sun Nov 6 21:17 - down (07:32)
[root@binbinlinux ~]# ls /var/log/btmp 无效登录的
/var/log/btmp
[root@binbinlinux ~]# lastb 查看无效登录的 历史
btmp begins Tue Nov 1 19:29:01 2016
[root@binbinlinux ~]# ls /var/log/maillog 跟邮件相关的 日志
/var/log/maillog
[root@binbinlinux ~]# ls /var/log/secure 关于验证相关的 正常或失败的登录都会记录在这个日志
/var/log/secure
[root@binbinlinux ~]# ls /var/log/dmesg 系统启动过程中硬件相关的日志
/var/log/dmesg 可以看到网卡 usb 等等一些
dmesg命令 查看的是时时更新的 log文件 是在系统启动时记录到日志
xargs 和exec 详解
[root@binbinlinux ~]# find /var/log/ -type f -mtime +10 -exec cp {} {}.bak \;
find /var/log/ -type f -mtime +10 这个目录下的文件 超过十天的搜索出来
在拷贝下从命名 -exec cp {} {}.bak \; {}表示前面找到的文件 不加\会认为是一条命令
find 选项 -exce find |xargs 前面的命令丢给后面的命令 去执行操作
[root@binbinlinux ~]# find /var/log/ -type f -mtime +10 |xargs -i cp {} {}.1
[root@binbinlinux ~]# ls p
p.txt p.txxt
[root@binbinlinux ~]# ls p |xargs rm 删除文件命令
[root@binbinlinux ~]# ls p
ls: 无法访问p: 没有那个文件或目录
[root@binbinlinux ~]# ls .txt
0.txt 123.txt 12.txt 1.txt 2.txt 6.txt test.txt
[root@binbinlinux ~]# ls .txt |xargs -i cp {} {}.bak 后面加.bak
screen 命令
[root@binbinlinux ~]# nohup 表示这个命令不会出现中断 的情况 nohup sleep 100 &
[root@binbinlinux ~]# screen
-bash: screen: command not found
[root@binbinlinux ~]# yum install -y screen 安装这个命令
运行screen 会进入虚拟的终端 可以运行top 之类的命令 ctrl+a+d 可以退出来 exit
[root@binbinlinux ~]# screen -ls 可以查看已经在后台跑的screen
There are screens on:
2596.pts-0.binbinlinux (Detached)
2620.pts-0.binbinlinux (Detached)
2 Sockets in /var/run/screen/S-root.
[root@binbinlinux ~]# ps aux |grep -E 'top|vmst' 查看下进程 top 和vmstat
root 5 0.0 0.0 0 0 ? S Nov07 0:00 [stopper/0]
root 8 0.0 0.0 0 0 ? S Nov07 0:00 [stopper/1]
root 2637 0.0 0.0 103320 916 pts/0 S+ 05:09 0:00 grep -E top|vmst
[root@binbinlinux ~]# screen -ls
[root@binbinlinux ~]# screen -r 2596 进入虚拟终端 ctrl+a+d 可以退出来 exit也可以从退出
[root@binbinlinux ~]# screen -S pts-0.binbinlinux -S的用法 后面跟文件名
curl 工具
[root@binbinlinux ~]# curl linux当中访问一个web或者一个网站
[root@binbinlinux ~]# curl -I www.binbinlinux.com 不想看到文本文档 -I
HTTP/1.1 200 OK 只需要查看他的状态码 200 常用的 200,301,302,404,403,502,503
Server: nginx
Date: Tue, 08 Nov 2016 10:53:30 GMT
Content-Type: text/html
Content-Length: 686
Last-Modified: Fri, 20 Mar 2015 05:09:51 GMT
Connection: keep-alive
ETag: "550bab9f-2ae"
Accept-Ranges: bytes
301 是跳转 访问域名时会跳转到另外一个域名
[root@binbinlinux ~]# ping www.qq.com 拼qq的看到ip
PING www.qq.com (180.96.86.192) 56(84) bytes of data.
64 bytes from 180.96.86.192: icmp_seq=1 ttl=56 time=2.41 ms
64 bytes from 180.96.86.192: icmp_seq=2 ttl=56 time=4.15 ms
^C
--- www.qq.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1675ms
rtt min/avg/max/mdev = 2.419/3.288/4.158/0.871 ms
[root@binbinlinux ~]# curl -x180.96.86.192:80 www.qq.com -I -x是代理的意思
HTTP/1.1 200 OK
Server: squid/3.5.20
Date: Tue, 08 Nov 2016 11:39:33 GMT
Content-Type: text/html; charset=GB2312
Vary: Accept-Encoding
Expires: Tue, 08 Nov 2016 11:40:33 GMT
Cache-Control: max-age=60
X-Cache: HIT from nanjing.qq.com
vim /etc/hosts 更改linux的hosts
[root@binbinlinux ~]# curl -Iv www.baidu.com 访问过程命令
访问一个用户名和密码的站点去访问
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 6958 100 6958 0 0 1344 0 0:00:05 0:00:05 --:--:-- 522k
[root@binbinlinux ~]# ls logo_white_fe6da1ec.png
logo_white_fe6da1ec.png
100 6958 100 6958 0 0 43396 0 --:--:-- --:--:-- --:--:-- 566k
[root@binbinlinux ~]# ls baidu.png png是图片的意思
baidu.png
正在解析主机 ss0.bdstatic.com... 58.215.118.32
正在连接 ss0.bdstatic.com|58.215.118.32|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:6958 (6.8K) [image/png]
正在保存至: “logo_white_fe6da1ec.png.1” 变成.1
100%[==========================================================================>] 6,958 --.-K/s in 0s
2016-11-08 05:57:16 (112 MB/s) - 已保存 “logo_white_fe6da1ec.png.1” [6958/6958])
rsync 格式
是用来同步数据的 拷贝数据的 本机的数据拷贝到远程 ,远程的数据拷贝到本机 支持网络通信
[root@binbinlinux ~]# rsync -av 192.168.11.190:/tmp/1.txt 远程的文件送到本地 拉
[root@binbinlinux ~]# rsync -av /tmp/1.txt 192.168.11.190:/tmp/ 本地文件送到远程 推
[root@binbinlinux ~]# rsync -av 192.168.11.190::binbinlinux/123/1.txt /tmp/
[root@binbinlinux ~]# rsync -av /tmp/1.txt 192.168.11.190::binbinlinux/123/
av r=归档 ,目录 l =软连接 ,文件 L=不需要软连接 p=它的权限 t=时间
--delete(a,b两台机器a考到b数据,a数据删除 加上delete 就会同步删除 不加则b设备备份不会删除)
--exclude 同步的时候,有log日志缓存文件,可以排除某些文件或者目录不同步
-P --progress 同步过程中 可以看到他的进度
-u --update 同步过程中删除或者更改的文件 会覆盖-u 不覆盖(a修改同步到b会覆盖原文件,避免目标用户的新数据覆盖掉)
[root@binbinlinux ~]# rsync -av 234/ /tmp/29/ /=就是把目录下的文件同步到tmp下的29下面
sending incremental file list
created directory /tmp/29
./
123.txt
222/
sent 16840 bytes received 38 bytes 33756.00 bytes/sec
total size is 16729 speedup is 0.99
[root@binbinlinux ~]# ln -s /etc/inittab 234/10.txt 软链接 方法
[root@binbinlinux ~]# ls -l 234
总用量 24
lrwxrwxrwx 1 root root 12 11月 8 08:50 10.txt -> /etc/inittab
-rw-r--r-- 1 root root 16729 8月 25 18:50 123.txt
drwxr-xr-x 2 root root 4096 9月 5 04:53 222
[root@binbinlinux ~]# rsync -av 234/ /tmp/29 同步过去
10.txt -> /etc/inittab
sent 104 bytes received 19 bytes 246.00 bytes/sec
total size is 16741 speedup is 136.11
[root@binbinlinux ~]# ls -l /tmp/29
drwxr-xr-x 2 root root 4096 9月 5 04:53 222
[root@binbinlinux ~]# rsync -avL 234/ /tmp/29 -L 同步原文件
10.txt
sent 17783 bytes received 54 bytes 35674.00 bytes/sec
total size is 17613 speedup is 0.99
[root@binbinlinux ~]# diff /etc/inittab /tmp/29/10.txt diff 对比差异的 , 一模一样
--delete 的用法
[root@binbinlinux ~]# rm -f 234/123.txt 删除一个文件
[root@binbinlinux ~]# rsync -avL 234/ /tmp/29 同步 不删除不覆盖文件
sent 68 bytes received 16 bytes 168.00 bytes/sec
total size is 884 speedup is 10.52
总用量 4
[root@binbinlinux ~]# rsync -avL --delete 234/ /tmp/29 同步文件 并删除保留一样
deleting 123.txt
-u的用法
[root@binbinlinux ~]# rsync -avLu 234/ /tmp/29 -u的用法
--exclude 的用法 过滤
[root@binbinlinux ~]# touch 234/23
[root@binbinlinux ~]# rsync -avLu 234/ --exclude="23" /tmp/29
sent 998 bytes received 38 bytes 2072.00 bytes/sec
total size is 884 speedup is 0.85
[root@binbinlinux ~]# tree 234
234
├── 10.txt -> /etc/inittab
├── 222
└── 23
1 directory, 2 files
-p的 用法 同步的时候显示的更多一些
[root@binbinlinux ~]# rsync -avLuP 234/ /tmp/29
884 100% 0.00kB/s 0:00:00 (xfer#1, to-check=2/4)
23
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=1/4)
sent 1048 bytes received 57 bytes 2210.00 bytes/sec
total size is 884 speedup is 0.80
-z压缩
[root@binbinlinux ~]# rsync -avLuPz34/ /tmp/29
rsync 同步之ssh隧道方式
[root@binbinlinux ~]# rsync avPz 111/ 192.168.1.109:/tmp/111 第一次需要输入密码
yes 然后输入密码 可以看到同步过程 也可以tree 一下看看
[root@binbinlinux ~]# rm -rf 111 把本地的删除了
[root@binbinlinux ~]# rsync -avpz 192.168.1.109:/tmp/111/ ./111/ 把远程的拷贝过来
[root@binbinlinux ~]# rsync -avpz [email protected]:/tmp/111/ ./111/ 也可以这样写
有时候遇到对方prot 不是22端口
[root@binbinlinux ~]# rsync -avpz -e "ssh-p 10022"192.168.1.109:/tmp/111/ ./111/
不是22号端口的写法 并且制定端口的写法
iptables -I INPUT -p --dport 22 -s 192.168.1.109 -j DROP 封掉22端口命令
[root@binbinlinux ~]# telnet 192.168.1.109 22
没有telnet 安装一下 是用的来探测远程机器某一个端口是否开启
[root@binbinlinux ~]# telnet 192.168.1.109 1022 1022这个端口是通的 通的就会出一些信息出来
按ctrl +中括号右半部分 退出来 quit
rsynv 后台服务的方式
[root@binbinlinux ~]# vim /etc/rsyncd.comf 配置文件 的名字可以自定义 放在/etc/下启动的时候自动去加载这个配置文件
port=8730 首先制定它的port 默认就是8730
log file=/var/log/rsync.log 日志文件
pid file=/var/run/rsync.pid pid的file
address= 也可以写下监听的ip
[abin] 关于具体的模块名字的配置
path=/tmp/rsync 模块目录就写在/tmp/rsync 写在这个目录 没有创建下
use chroot =yes no use限定家目录的
max connection=4 最大的连接数
read only=yes
list=yes
uid=root
gid=root
auth users=abin
secrets file=/etc/rs.passwd
hosts allow=192.168.11.190
rsync --daemon 启动一个守护进程
本文转自 amenging 51CTO博客,原文链接:http://blog.51cto.com/11335852/2045883