Ubuntu LTS 18.04 配置路由转发

以下命令都是root用户执行

1.开启ip转发

Linux系统缺省并没有打开IP转发功能
执行 cat /proc/sys/net/ipv4/ip_forward
可以看到内容为 0

此时,执行下面的命令

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

再次查看【ip_forward】的内容
cat /proc/sys/net/ipv4/ip_forward

内容为

1

改为1,表示开启IP转发,但是重启后会失效,故需要修改 【/etc/sysctl.conf】

将 【net.ipv4.ip_forward=1】 的注释打开

  • 【/etc/sysctl.conf】
#
# /etc/sysctl.conf - Configuration file for setting system variables
# See /etc/sysctl.d/ for additional system variables.
# See sysctl.conf (5) for information.
#

#kernel.domainname = example.com

# Uncomment the following to stop low-level messages on console
#kernel.printk = 3 4 1 3

##############################################################3
# Functions previously found in netbase
#

# Uncomment the next two lines to enable Spoof protection (reverse-path filter)
# Turn on Source Address Verification in all interfaces to
# prevent some spoofing attacks
#net.ipv4.conf.default.rp_filter=1
#net.ipv4.conf.all.rp_filter=1

# Uncomment the next line to enable TCP/IP SYN cookies
# See http://lwn.net/Articles/277146/
# Note: This may impact IPv6 TCP sessions too
#net.ipv4.tcp_syncookies=1

# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1

# Uncomment the next line to enable packet forwarding for IPv6
#  Enabling this option disables Stateless Address Autoconfiguration
#  based on Router Advertisements for this host
#net.ipv6.conf.all.forwarding=1


###################################################################
# Additional settings - these settings can improve the network
# security of the host and prevent against some network attacks
# including spoofing attacks and man in the middle attacks through
# redirection. Some network environments, however, require that these
# settings are disabled so review and enable them as needed.
#
# Do not accept ICMP redirects (prevent MITM attacks)
#net.ipv4.conf.all.accept_redirects = 0
#net.ipv6.conf.all.accept_redirects = 0
# _or_
# Accept ICMP redirects only for gateways listed in our default
# gateway list (enabled by default)
# net.ipv4.conf.all.secure_redirects = 1
#
# Do not send ICMP redirects (we are not a router)
#net.ipv4.conf.all.send_redirects = 0
#
# Do not accept IP source route packets (we are not a router)
#net.ipv4.conf.all.accept_source_route = 0
#net.ipv6.conf.all.accept_source_route = 0
#
# Log Martian Packets
#net.ipv4.conf.all.log_martians = 1
#

###################################################################
# Magic system request Key
# 0=disable, 1=enable all
# Debian kernels have this set to 0 (disable the key)
# See https://www.kernel.org/doc/Documentation/sysrq.txt
# for what other values do
#kernel.sysrq=1

###################################################################
# Protected links
#
# Protects against creating or following links under certain conditions
# Debian kernels have both set to 1 (restricted) 
# See https://www.kernel.org/doc/Documentation/sysctl/fs.txt
#fs.protected_hardlinks=0
#fs.protected_symlinks=0

2. 配置NAT

设置ip伪装

iptables -t nat -A POSTROUTING -o enp1s0 -j MASQUERADE

enp1s0 为网卡名称,请根据自己的网卡名称设置

安装 iptables-persistent

apt install iptables-persistent

将ip规则追加到rules.v4中

iptables-save > /etc/iptables/rules.v4

还原iptables配置。

iptables-restore < /etc/iptables/rules.v4

此时,rules.v4的内容

  • [/etc/iptables/rules.v4]
# Generated by iptables-save v1.6.1 on Fri Apr 12 06:40:39 2019
*filter
:INPUT ACCEPT [25:1976]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [8:1104]
COMMIT
# Completed on Fri Apr 12 06:40:39 2019
# Generated by iptables-save v1.6.1 on Fri Apr 12 06:40:39 2019
*nat
:PREROUTING ACCEPT [50:3595]
:INPUT ACCEPT [9:1603]
:OUTPUT ACCEPT [11:853]
:POSTROUTING ACCEPT [2:161]
-A POSTROUTING -o enp1s0 -j MASQUERADE
COMMIT
# Completed on Fri Apr 12 06:40:39 2019
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值