linux nat,linux nat

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

设置 IPv4 转发1

2

3

4

5

6

7vim /etc/sysctl.conf

设置

net.ipv4.ip_forward = 1

生效

sysctl -p

显示所有系统参数

sysctl -a

sysctl

sysctl 配置与显示在/proc/sys 目录中的内核参数.可以用 sysctl 来设置或重新设置联网功能,如 IP 转发、IP 碎片去除以及源路由检查等。用户只需要编辑/etc/sysctl.conf 文档,即可手工或自动执行由 sysctl 控制的功能。

命令格式:1

2

3

4

5

6

7

8

9

10

11

12

13

14

15sysctl [-n] [-e] -w variable=value

sysctl [-n] [-e] -p (default /etc/sysctl.conf)

sysctl [-n] [-e] -a

常用参数的意义:

-w 临时改变某个指定参数的值,如

sysctl -w net.ipv4.ip_forward=1

-a 显示所有的系统参数

-p 从指定的文档加载系统参数,如不指定即从/etc/sysctl.conf中加载

如果仅仅是想临时改变某个系统参数的值,可以用两种方法来实现,例如想启用 IP 路由转发功能:1

2

3echo 1 > /proc/sys/net/ipv4/ip_forward

sysctl -w net.ipv4.ip_forward=1

以上两种方法都可能立即开启路由功能,但如果系统重启,或执行了service network restart命令,所设置的值即会丢失,如果想永久保留配置,可以修改/etc/sysctl.conf 文档

将 net.ipv4.ip_forward=0 改为 net.ipv4.ip_forward=1

iptables

查看 iptables 规则内容1

2

3

4

5service iptables status

或者

iptables -L

更详细的信息

iptables -L -v

清空 iptables 规则1iptables -F

允许 192.168.10.0.0/16 连接 ssh1iptables -t filter -A INPUT -s 192.168.0.0/16 -p tcp --dport 22 -j ACCEPT

INPUT 默认策略改为 DROP1iptables -P INPUT DROP

OUTPUT 默认策略改为 ACCEPT1iptables -P OUTPUT ACCEPT

添加 SNAT1iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -o em1 -j MASQUERADE

不需要下面这条1iptables -t nat -R POSTROUTING 2 -d 192.168.10.2 -p tcp --dport 8081 -j SNAT --to 172.10.2.201:80

添加 DNAT1iptables -t nat -A PREROUTING -d 172.10.2.201 -p tcp --dport 80 -j DNAT --to-destination 192.168.10.10:80

添加 INPUT 允许1iptables -t filter -A INPUT -s 172.10.2.201/32 -p tcp --dport 80 -j ACCEPT

允许 PING1iptables -A INPUT -p icmp -j ACCEPT

临时添加多个公网 IP1ip address add 172.10.2.203/24 dev em1

永久添加多个公网 IP

仿照/etc/sysconfig/network-scripts/ifcfg-em1 增加一文档根据网络虚拟接口的名字进行命名

/etc/sysconfig/network-scripts 目录下复制 ifcfg-em1 到 ifcfg-em1:01cp ifcfg-em1 ifcfg-em1:0

修改 ifcfg-em1:0 文档1

2

3

4

5

6

7

8DEVICE=em1:0

TYPE=Ethernet

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=172.10.2.201

NETMASK=255.255.255.0

GATEWAY=172.10.2.1

ifconfig em1:0 down

关闭虚拟接口1ifconfig eth*[:x] down(*代表的是网卡编号,x代表虚拟接口号0-255)

ifconfig em1:0 up

启动虚拟网口出现错误

SIOCSIFFLAGS: Cannot assign requested address

出现以上错误的原因是把 em1:1 的信息写在了 ifcfg-em1:1 的配置文档里,本意是想,开机启动的时候自动加载,实现一块网卡双 IP。

但当手动把 em1:1 设备 down(执行了:“ifconfig em1:1 down”),然后再启用的时候会报以上错误,主要是 mac 地址重复了

解决办法:

1、手动分配一个 mac 地址(不建议)

2、手动执行添加 IP 的命令,把写在 ifcfg-eth0:1 里的信息配置上,如

ifconfig eth0:1 192.168.1.2/24

route add default gw 192.168.1.1 dev eth0:1

查看 IP1

2ip addr

ifconfig

添加默认网关1route add default gw 172.10.2.1

问题

iptables -P INPUT DROP 后无法 ping 通 www.baidu.com(无法进行域名解析)

使用 iptables -P INPUT ACCEPT 允许所有通过1

2iptables -A INPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT

iptables -A INPUT -m state --state INVALID -j DROP

demo

配置后内容1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22[[email protected] ~]# vim /etc/sysconfig/iptables

# Generated by iptables-save v1.4.7 on Mon Aug 15 17:30:49 2016

*nat

:PREROUTING ACCEPT [20:3272]

:POSTROUTING ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A PREROUTING -d 172.10.2.201/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination 192.168.10.10:80

-A POSTROUTING -s 192.168.0.0/16 -o em1 -j MASQUERADE

COMMIT

# Completed on Mon Aug 15 17:30:49 2016

# Generated by iptables-save v1.4.7 on Mon Aug 15 17:30:49 2016

*filter

:INPUT ACCEPT [1368:118485]

:FORWARD ACCEPT [21251:9829802]

:OUTPUT ACCEPT [425:52772]

-A INPUT -s 192.168.0.0/16 -p tcp -m tcp --dport 22 -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

-A INPUT -m state --state INVALID -j DROP

COMMIT

# Completed on Mon Aug 15 17:30:49 2016

IP 命令

ip link set–改变设备的属性. 缩写:set、s

示例 1:up/down 起动/关闭设备。

ip link set dev eth0 up

这个等于传统的 ifconfig eth0 up(down)

ip link show–显示设备属性. 缩写:show、list、lst、sh、ls、l

-s 选项出现两次或者更多次,ip 会输出更为详细的错误信息统计。

示例:

ip -s -s link ls eth0

eth0: mtu 1500 qdisc cbq qlen 100

link/ether 00:a0:cc:66:18:78 brd ff:ff:ff:ff:ff:ff

RX: bytes packets errors dropped overrun mcast

2449949362 2786187 0 0 0 0

RX errors: length crc frame fifo missed

0 0 0 0 0

TX: bytes packets errors dropped carrier collsns

178558497 1783946 332 0 332 35172

TX errors: aborted fifo window heartbeat

0 0 0 332

这个命令等于传统的 ifconfig eth0

ip address add–添加一个新的协议地址. 缩写:add、a

示例 1:为每个地址设置一个字符串作为标签。为了和 Linux-2.0 的网络别名兼容,这个字符串必须以设备名开头,接着一个冒号,

ip addr add local 192.168.4.1/28 brd + label eth0:1 dev eth0

示例 2: 在以太网接口 eth0 上增加一个地址 192.168.20.0,掩码长度为 24 位(155.155.155.0),标准广播地址,标签为 eth0:Alias:

ip addr add 192.168.4.2/24 brd + dev eth1 label eth1:1

这个命令等于传统的: ifconfig eth1:1 192.168.4.2

ip address delete–删除一个协议地址. 缩写:delete、del、d

ip addr del 192.168.4.1/24 brd + dev eth0 label eth0:Alias1

ip address show–显示协议地址. 缩写:show、list、lst、sh、ls、l

ip addr ls eth0

ip address flush–清除协议地址. 缩写:flush、f

示例 1 : 删除属于私网 10.0.0.0/8 的所有地址:

ip -s -s a f to 10/8

示例 2 : 取消所有以太网卡的 IP 地址

ip -4 addr flush label “eth0”

ip neighbour–neighbour/arp 表管理命令

缩写 neighbour、neighbor、neigh、n

命令 add、change、replace、delete、fulsh、show(或者 list)

ip neighbour add – 添加一个新的邻接条目

ip neighbour change–修改一个现有的条目

ip neighbour replace–替换一个已有的条目

缩写:add、a;change、chg;replace、repl

示例 1: 在设备 eth0 上,为地址 10.0.0.3 添加一个 permanent ARP 条目:

ip neigh add 10.0.0.3 lladdr 0:0:0:0:0:1 dev eth0 nud perm

示例 2:把状态改为 reachable

ip neigh chg 10.0.0.3 dev eth0 nud reachable

ip neighbour delete–删除一个邻接条目

示例 1:删除设备 eth0 上的一个 ARP 条目 10.0.0.3

ip neigh del 10.0.0.3 dev eth0

ip neighbour show–显示网络邻居的信息. 缩写:show、list、sh、ls

示例 1: ip -s n ls 193.233.7.254

193.233.7.254. dev eth0 lladdr 00:00:0c:76:3f:85 ref 5 used 12/13/20 nud reachable

ip neighbour flush–清除邻接条目. 缩写:flush、f

示例 1: (-s 可以显示详细信息)

ip -s -s n f 193.233.7.254

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值