一、网络相关的基础:linux
ifconfig:查看IP地址信息:vim
也能够用ip addr查看,不过这个显示不直观:安全
若是这个包没有安装:yum install -y net-toolsbash
[root@localhost ~]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.149.129 netmask 255.255.255.0 broadcast 192.168.149.255 ether 00:0c:29:37:3b:d9 txqueuelen 1000 (Ethernet) RX packets 109556 bytes 80107827 (76.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 43183 bytes 16149071 (15.4 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 2 bytes 224 (224.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2 bytes 224 (224.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifconfig -a:ifconfig有一个-a选项(能显示没有IP的网卡和down掉的网卡):服务器
启动或关闭网卡:有时候咱们须要针对单个网卡进行操做,可使用以下命令:网络
启动网卡:ifup eth0 ==== ifconfig eth0 upapp
关闭网卡:ifdown eth0 ==== ifconfig eth0 downtcp
同时启动和关闭:ifdown eth0;ifup eth0工具
设置网卡别名:有时须要给网卡设置多个IP地址用于通讯:以下:oop
一、复制网卡脚本到新的网卡eth0:0里,后面的冒号须要脱义: [root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth0\:0 #复制网卡脚本到新的文本内容: 二、并修改网卡eth0:0的内容:IP地址 DEVICE NAME等: [root@localhost network-scripts]# cat ifcfg-eth0:0 #查看网卡内容: TYPE=Ethernet BOOTPROTO=static DEFROUTE=yes NAME=eth0:0 DEVICE=eth0:0 ONBOOT=yes IPADDR=192.168.149.130 NETMASK=255.255.255.0 三、而后ping也能够通: [root@localhost ~]# ping 192.168.149.130 PING 192.168.149.130 (192.168.149.130) 56(84) bytes of data. 64 bytes from 192.168.149.130: icmp_seq=1 ttl=64 time=0.068 ms 64 bytes from 192.168.149.130: icmp_seq=2 ttl=64 time=0.046 ms 64 bytes from 192.168.149.130: icmp_seq=3 ttl=64 time=0.046 ms
查看网卡的链路是否连通性有两种方式:
ethtool eth0 #查看网卡连通性:
mii-tool eth0 #查看网卡连通性:
[root@localhost ~]# mii-tool eth0 eth0: negotiated 1000baseT-FD flow-control, link ok [root@localhost ~]# ethool eth0 Link detected: yes
二、修改DNS地址:
配置文件: /etc/resolv.conf
DNS是用来作IP和域名之间的解析,而在linux下设置DNS有两种方式:
一、编辑并临时修改/etc/resolv.conf文件里的DNS配置,重启网卡后会被网卡配置文件的里的覆盖:(格式:nameserver 114.114.114.114)
二、编辑网卡主配置文件(ifcfg-eth0),更改后重启网卡生效:(格式:DNS1=114.114.114 \n DNS2=8.8.8.8)
注意:若是是临时修改DNS,则建议修改/etc/resolv.conf,若是是永久更改,则修改网卡主配置文件:
[root@localhost ~]# tail -n2 /etc/sysconfig/network-scripts/ifcfg-eth0 DNS1=114.114.114.114 DNS2=8.8.8.8 [root@localhost ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 114.114.114.114 nameserver 8.8.8.8
此外还有一个能够解析的文件:只适合用本机: /etc/hosts #在此不作过多解释:
三、linux下的防火墙(netfilter):
SELinux:是linux系统里特有的安全机制,几乎不多使用,安装完系统后通常SELinux是关闭的(秘钥验证是会受此影响):
netfilter:是linux下的防火墙,它是经过两个管理工具"iptables" "firewalld"来实现的,在Centos 6包括以前都是用的iptables,从Centos 7开始使用的firewalld:
注意:要区分开:iptables和firewalld只是netfillter防火墙实现的两种手段方式而已:
2.1:SELinux基本操做:
临时关闭SELinux:setenforce 0
[root@localhost ~]# getenforce #查看selinux的状态 Disabled [root@localhost ~]# setenforce 0 #临时关闭selinux: setenforce: SELinux is disabled
永久关闭selinux:编辑/etc/selinux/config文件:
[root@localhost ~]# vim /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=enforcing SELINUXTYPE=targeted
把selinux=enforcing修改成disabled就能够了,从新启动系统便可:
三、netfilter(iptables):因为如今好多公司仍是以前的Centos 6/5系统,咱们先简单介绍iptables的用法:
首先作一个准备工做,关闭firewalld, 开启iptables(须要安装iptables-services这个包):
[root@localhost ~]# systemctl disable firewalld #禁止firewalld服务开机启动: [root@localhost ~]# systemctl stop firewalld #关闭firewalld服务: [root@localhost ~]# yum install -y iptables-services #安装iptables,这样可使用之前版本: 总下载量:483 k Downloading packages: Delta RPMs disabled because /usr/bin/applydeltarpm not installed. (1/2): iptables-services-1.4.21-24.1.el7_5.x86_64.rpm | 51 kB 00:00:00 (2/2): iptables-1.4.21-24.1.el7_5.x86_64.rpm | 432 kB 00:00:00 完毕! [root@localhost ~]# systemctl enable iptables #容许iptables服务开机启动: Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service. [root@localhost ~]# systemctl restart iptables #启动iptables服务: 注意:iptables安装完成后,默认此服务是开启的:
3.1:查看iptables的默认规则:iptables -nvL == iptables -t filter -nvL(默认是显示filter表)
默认规则保存在/etc/sysconfig/iptables
[root@localhost ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 53 4728 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 41 packets, 4264 bytes) pkts bytes target prot opt in out source destination
3.2:netfilter(iptables):内置有五个表及五个链:
一、flter表:是系统默认的表,主要用于过滤包,该表内置有三个链:
INPUT:数据包进入本机时通过的链,做用于进入本机的包:
OUTPUT:本机自身产生的包发送出去的,做用于本机送出的包:
FORWARD:数据包转发(只是通过本机的包),做用于那些跟本机无关的包:
二、nat表:用于网络地址转换:内置有三个链:
PERROUTING:数据包刚刚进来到达防火墙时改变其目的地址(若是须要的话):
OUTPUT:改变本地产生的包的目的地址:
POSTROUTE:数据包离开防火墙时改变其源地址:
三、mangle表:用于数据包作标记,而后根据标记去操做相应的包:用的较少,无需太关注:内置五个链以下:
INPUT OUTPUT FORWARD PERROUTING POSTROUTE
四、raw表:能够实现不追踪某些数据包,用来节省资源:用的较少:内置以下两个链: PERROUTING OUTPUT
5、security:它是在Centos 7里面才有的,用于强制访问控制MAC的网络规则:没人用过:
3.3:iptables options 参数:
options:
-n:表示不针对IP反解析主机名:
-v:表示列出的信息更详细:
-L:表示列出:
-t:后面跟表名(默认打印filter表的相关信息):
-A:add:表示增长一条规则,增长后规则是在最下面排列:
-I:insert:插入一条规则,插入后规则在最上面显示:
-s:后面跟源IP地址: -s 192.168.188.1
-d:后面跟目的地址: -d 192.168.1.1
-sport:表示指定源端口,需结合-p使用:
-dport:表示指定目标端口,需结合-p使用:
-p:指定协议:tcp udp icmp:
-j:后跟动做: ACCEPT(容许包)、REGECT(拒绝包)、 DROP(丢弃)
注意:REGECT和DROP都是丢掉包,区别在于:drop是直接丢弃,不会看数据包:而regect会查看数据包,才会说你不能够进来:并拒绝:
-i:表示指定网卡,不经常使用:
-P:预设策略,通常用于写脚本,体如今iptables -nvL的第一行policy ACCEPT:
-F:清空规则,不会清空配置文件的,但再次重启iptables后还会加载出来:
-Z:计数器清零,用于写脚本,能够用来封IP,以半小时为准,经过判断计数器的数量封IP,达到目的:
注意:在iptables里匹配时会采起顺序匹配,也就是说一旦匹配到了就会执行,不往下执行了:
用法1:增长一条规则,当源IP为192.168.188.1且端口123向IP为192.168.1.1端口为80的数据包,执行丢弃的操做:最下面:
iptables -A INPUT -s 192.168.188.1 -p tcp --sport 123 -d 192.168.1.1 --dport 80 -j DROP
[root@localhost ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 123 -d 192.168.1.1 --dport 80 -j DROP [root@localhost ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 1037 96780 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 108 8424 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited 0 0 DROP tcp -- * * 192.168.188.1 192.168.1.1 tcp spt:123 dpt:80 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with Chain OUTPUT (policy ACCEPT 23 packets, 2504 bytes) pkts bytes target prot opt in out source destination D
用法2:插入一条规则,源IP是192.168.149.0/24网段到本机的56888的端口数据包,所有放行:
[root@localhost ~]# iptables -I INPUT -s 192.168.149.0/24 -p tcp --dport 56888 -j ACCEPT
[root@localhost ~]# iptables -I INPUT -s 192.168.149.0/24 -p tcp --dport 56888 -j ACCEPT [root@localhost ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 35 3120 ACCEPT tcp -- * * 192.168.149.0/24 0.0.0.0/0 tcp dpt:56888 108 8424 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with 0 0 DROP tcp -- * * 192.168.188.1 192.168.1.1 tcp spt:123dpt:80 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 icmp-host-prohibited Chain OUTPUT (policy ACCEPT 27 packets, 2832 bytes) pkts bytes target prot opt in out source destination
如上图所示:-A(增长)和-I(插入)还有有区别:-A增长后是会在最下面依次排序,-I插入后是在最上面,也就是说匹配的时候会优先匹配:而基于iptables的匹配规则,会是从上往下匹配依次,一旦匹配到就执行相关操做,在往下面的则不匹配:
用法3:删除一条规则:iptable -D INPUT .........
1:若是要是知道规则的内容:删除一条规则必须和添加时候的规则一致,也就是说出了-A/-D不同,其余地方要相同:
[root@localhost ~]# iptables -D INPUT -s 192.168.149.0/24 -p tcp --dport 56888 -j ACCEPT [root@localhost ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 1328 122K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 138 10764 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited 0 0 DROP tcp -- * * 192.168.188.1 192.168.1.1 tcp spt:123 dpt:80 Chain OUTPUT (policy ACCEPT 28 packets, 2936 bytes) pkts bytes target prot opt in out source destination
2:若是要是忘记规则内容:则须要先查看当前序列号对应的规则,而后根据序列号操做:第一列num表示序列号:
[root@localhost ~]# iptables -nvL --line-number #列出每条规则所对应的序列号: Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 2 193 17212 ACCEPT tcp -- * * 192.168.149.0/24 0.0.0.0/0 tcp dpt:56888 3 1341 123K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 4 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 5 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 6 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 7 138 10764 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 8 0 0 DROP tcp -- * * 192.168.188.1 192.168.1.1 tcpspt:123 dpt:80 [root@localhost ~]# iptables -D INPUT 8 #删除第一列的第8条规则:
三、清除规则:iptables -F #不会更改配置文件的内容:
[root@localhost ~]# iptables -F #清除规则: [root@localhost ~]# iptables -nvL #再次查看规则: Chain INPUT (policy ACCEPT 25 packets, 2236 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 19 packets, 2056 bytes) pkts bytes target prot opt in out source destination [root@localhost ~]# service iptables restart #从新启动规则: 注释:重启以后即会恢复以前的规则,由于它原来的规则的配置都保存在/etc/sysconfig/iptables文件里,因此从新启动会再次加载这个配置文件,恢复原有配置: 若是想保存永久保存当前配置,则可使用service iptables save 保存当前配置到配置文件里便可:
永久保存当前配置到配置:service iptables save
四、iptables还能够针对某个网卡进行操做: -i #把INPUT链里源IP为192.168.149.0/24网段到eth0的数据包都丢弃:
[root@localhost ~]# iptables -A INPUT -s 192.168.149.0/24 -i eth0 -j DROP [root@localhost ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 235 20380 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 DROP all -- eth0 * 192.168.149.0/24 0.0.0.0/0
五、预设策略:-P #表示iptables规则链里第一行(policy ACCEPT)这个,
[root@localhost ~]# iptables -P OUTPUT DROP #执行OUTPUT的预设策略为DROP,远程会断掉:
此时则须要再去机房登陆你的主机,执行以下命令:把预设策略修改成ACCEPT: #那若是主机在美国了,你也去么???
[root@localhost ~]# iptables -P OUTPUT ACCEPT #修改output的预设策略为ACCEPT;
如上命令切勿操做,由于一旦执行,不只会端口你的远程服务器,有可能还会影响到业务,由于修改了它的默认策略为DROP:
附记:
iptables的使用格式:
iptables -t [ filter|nat ] -A|-I [ INPUT|OUTPUT|FORWARD ] -s [ 源ip|源子网 ] -p [ tcp|udp|icmp ] --sport 源端口 -d [ 目标ip|目标子网 ] --dport 目标端口 -j [ ACCEPT|REGECT|DROP ]