天天看點

firewalld和netfilter, iptablesGenerated by iptables-save v1.4.7 on Mon Nov 7 00:21:20 2016Completed on Mon Nov 7 00:21:20 2016

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