mov link 应用 ifup 只需要 图形 print scrip 动作
10.11 linux 网络相关
ifconfig 如果没有 ifconfig,需要安装包
[root@centos-01 ~]# yum install net-tools
如果需要显示所有的网卡信息,包括 down 掉的或者没有 IP 地址的网络,使用 - a 命令
[root@centos-01 ~]# ifconfig -a
有时候会单独针对一个网卡做一些更改(如改网关,或者增加 DNS),但是不想把所有的网卡都重启,只需要重启指定的网卡,为了避免 down 掉后无法启动,我们需要 2 个命令一起执行
[root@centos-01 ~]# ifdown ens33 && ifup ens33
成功断开设备 'ens33'.
成功激活的连接(D-Bus 激活路 径:/org/freedesktop/NetworkManager/ActiveConnection/1)
设定虚拟网卡
1,切换至网卡配置文件
[root@centos-01 ~]# cd /etc/sysconfig/network-scripts
2,复制网卡
[root@centos-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:0
3,编辑配置文件
[root@centos-01 network-scripts]# vi !$
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33:0
UUID=584a55df-e887-4d4e-a11a-2ea0b5f6f5ed
DEVICE=ens33:0
ONBOOT=YES
IPADDR=192.168.187.150
NETMASK=255.255.255.0
说明:更改了 NAME,DEVICE,IPADDR, 删除了 DNS 和网关,之前已经设定过.
4,重启系统网卡
[root@centos-01 network-scripts]# ifdown ens33 && ifup ens33
成功断开设备 'ens33'.
成功激活的连接(D-Bus 激活路径:/org/freedesktop/NetworkManager/ActiveConnection/2)
5,查看网卡信息
[root@centos-01 network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.187.130 netmask 255.255.255.0 broadcast 192.168.187.255
inet6 fe80::9b87:5d89:4bd9:8e53 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:02:6a:c4 txqueuelen 1000 (Ethernet)
RX packets 3124 bytes 244651 (238.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 689 bytes 85729 (83.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.187.150 netmask 255.255.255.0 broadcast 192.168.187.255
ether 00:0c:29:02:6a:c4 txqueuelen 1000 (Ethernet)
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 20 bytes 1700 (1.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 20 bytes 1700 (1.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
6,检测虚拟网卡 IP 是否能 ping 通 在 cmd 中 ping 一下就可以
查看网卡是否连接
方法 1:mii-tool [网卡名]
[root@centos-01 ~]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok
查看:link ok说明网卡连接OK!
方法 2:ethtool [网卡名]
[root@centos-01 ~]# ethtool ens33
Settings for ens33:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
......
Wake-on: d
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes
查看:Link detected: yes说明网卡连接OK!
更改主机名
更改:
[root@centos-01 ~]# hostnamectl set-hostname aminglinux-001
查看主机名:
[root@centos-01 ~]# hostname
aminglinux-001
不重启的时候更改主机名
[root@centos-01 ~]# bash
[root@aminglinux-001 ~]# exit
exit
DNS 配置
DNS 配置文件:/etc/resolv.conf
[root@centos-01 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.187.2
注: 更改网卡配置即可更改 DNS 配置文件,更改后需要重启网卡(ifdown/ifup)后生效,也可编辑'/etc/resolv.conf'临时更改 DNS 配置,该办法在重启网卡后会被网卡配置文件中的 DNS 覆盖!
本地域名配置文件文件:/etc/hosts
[root@centos-01 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
说明: 使用 vi 命令可在该配置文件下可以自定义 IP 所对应的域名(一个 IP 对应多个域名或一个域名对应多个 IP,用空格隔开,当一个域名对应多个 IP 时,以配置文件中靠后面的配置为准),但是该域名配置只在本机生效!
10.12 firewalld 和 netfilter
永久关闭防火墙
[root@aminglinux-001 ~]# vi etc/setlinux/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=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
将 SELINUX=enforcing 改为 disabled 保存,重启系统即可!
查看防火墙状态
[root@aminglinux-001 ~]# getenforce
Enforcing
临时关闭防火墙
[root@aminglinux-001 ~]# setenforce 0
[root@aminglinux-001 ~]# getenforce
Permissive
Centos7 中的防火墙叫 firewalld,而 7 之前的防火墙叫 netfilter. 内部的工具 iptables 是一样的(可以打开或者关闭端口)
停用 firewalld
关闭防火墙
[root@aminglinux-001 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus- org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
停止服务
[root@aminglinux-001 ~]# systemctl stop firewalld
开启 netfilter
开启前先安装iptables工具包:
[root@aminglinux-001 ~]# yum install -y iptables-services
开启iptables服务:
[root@aminglinux-001 ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@aminglinux-001 ~]# systemctl start iptables
查看 iptables 规则: iptables -nvL
[root@aminglinux-001 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
37 2432 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
25 1450 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 21 packets, 2204 bytes)
pkts bytes target prot opt in out source destination
10.13 netfilter5 表 5 链介绍
表名
filter:包过滤,用于防火墙规则. nat:地址转换,用于网关路由器. mangle:用于给数据包打标记,然后根据标记去操作那些表.(不常用) 还有两个不常用的表:raw 和 security,在此不多讲述. 规则链名
filter 的三种链:
INPUT 链:作用于输入本机的数据包. OUTPUT 链:作用于本机输出的数据包. FORWARD 链:作用于与本机无关的包. nat 的三种链:
PREROUTING 链:作用是在包刚刚到达防火墙时改变它的目的地址,如果需要的话. OUTPUT 链:改变本地产生的包的目的地址. POSTROUTING 链:在包离开防火墙之前改变其源地址. nat 表应用(了解内容)
环境: 假设有三台机器 (A,B,C),A 机器上有一块网卡连接公网 IP1;B 机器上有两块网卡 b1,b2,b1,b2 分别连接公网和私网 IP2,,IP3;C 机器上有一块网卡连接私网 IP4.很明显此时 A&B,B&C 之间都分别能互通,但是 A 和 C 之间是不能直接连通的,只能借助 B 做媒介才能连通.那么如何设置才能使得 A 和 C 之间互通呢?
方法:
先打开路由转发功能:
[root@aminglinux-001 ~]# echo "1" > /proc/sys/net/ipv4_forward
然后对nat表做IP转发配置:
[root@aminglinux-001 ~]# iptables -t nat -A POSTROUTING -s IP2(此处IP在同一个网段即可) -o ensA(A机器的网卡名称) -j MASQUERADE
说明: -o 选项后面跟设备名称,表示出口网卡,MASQUERADE 是伪装,冒充的意思.
10.14 iptables 语法
iptables 命令是 Linux 上常用的防火墙软件,是 netfilter 项目的一部分.可以直接配置,也可以通过许多前端和图形界面配置.
语法: iptables [options] [参数]
Options:
-n:不针对 IP 反解析主机名
-v:显示更详细的信息
-t:指定表(iptables 命令默认作用于 filter 表)
-L:显示信息
-F:清空所有规则
-A/D:=add/delete,添加 / 删除一条规则
-I:插入一条规则
-p:指定协议,可以是 tcp,udp 或 icmp --sport:跟 - p 一起使用,指定源端口
--dport:跟 - p 一起使用,指定目标端口
-s:指定源 IP(可以是一个 IP 段)
-d:指定目的 IP(可以是一个 IP 段)
-j:后面跟动作(ACCEPT 表示允许包;DROP 表示丢掉包;REJECT 表示拒绝包)
-i:指定网卡
-Z:把包以及流量计数器清零
-P:=pre,预设策略
iptables 命令选项输入顺序: iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源 IP / 源子网> --sport 源端口 <-d 目标 IP / 目标子网> --dport 目标端口 -j 动作
查看规则 [root@aminglinux-001 ~]# iptables -nvL
iptables 规则配置文件:/etc/sysconfig/iptables
清空规则 [root@aminglinux-001 ~]# iptables -F
注: 该命令不会清除配置文件内的规则!当更改规则后需要执行命令'service iptables save'将其保存到配置文件.
增加一条规则(-A/I) [root@aminglinux-001 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
说明: 增加一条规则,当 IP192.168.188.1,协议为 tcp,端口为'1234'的向 IP192.168.188.128,端口为 80 的机器发送包时执行操作:drop(丢掉包). 注: 该命令也可以把 - A 换成 - I,两者的区别类似于排队和插队,两种方法插入的规则优先级不同.
删除一条规则(-D)
方法 1: 知道规则的内容 [root@aminglinux-001 ~]# iptables -D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
注: 要删除一条规则时,必须和插入的规则一致,也就是说,两条 iptables 命令除了 - A/I 和 - D 不一样外,其他地方都一样.
方法 2: 忘记规则内容
首先所以用以下命令查看规则序号: [root@aminglinux-001 ~]# iptables -nvL --line-number
然后再执行删除命令: [root@aminglinux-001 ~]# iptables -D INPUT [序号]
更改预设策略(-P)
执行命令:
#iptables -P OUTPUT DROP
注意: 尽量不要随意更改该配置,尤其是在进行远程登录时,一旦执行该命令后将会断开连接.这个策略设定后只能用命令:'iptables -P OUTPUT ACCEPT'来恢复成原始状态,不能使用 - F 参数.
linux 网络相关 | 防火墙 |netfilter5 表 5 链
来源: http://www.bubuko.com/infodetail-2472758.html