【内网穿透服务器】使用Frp外网访问内网FTP服务器(以访问内网samba服务为例)

使用Frp外网访问内网FTP服务器(以访问内网samba服务为例)

FTP是基于TCP的服务,使用两个端口,命令端口和数据端口,命令端口是21,数据端口在主动模式下为20,在被动模式随机分配。

FTP协议有两种工作方式:PORT方式和PASV方式,中文意思为主动式和被动式。

主动(PORT):

在这里插入图片描述
在这里插入图片描述
PORT(主动)方式的连接过程是:客户端向服务器的FTP端口(默认是21)发送连接请求,服务器接受连接,建立一条命令链路。当需要传送数据时,客户端在命令链路上用PORT命令告诉服务器:“我打开了XXXX端口,你过来连接我”。于是服务器从20端口向客户端的XXXX端口发送连接请求,建立一条数据链路来传送数据。

主动模式存在的问题是,在客户端一般都会有防火墙的设置,当服务端与客户端数据进行数据通信时,客户端的防火墙会将服务端的端口挡在外面。此时,通信就会受阻。因此,被动模式就产生了。

被动(PASV):

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
PASV(被动)方式的连接过程是:客户端向服务器的FTP端口(默认是21)发送连接请求,服务器接受连接,建立一条命令链路。当需要传送数据时,服务器在命令链路上用PASV命令告诉客户端:“我打开了XXXX端口,你过来连接我”。于是客户端向服务器的XXXX端口发送连接请求,建立一条数据链路来传送数据。

被动模式也会存在防火墙的问题,客户端与服务端传输数据时,在服务端也会有防火墙,但在服务端的防火墙有连接追踪的功能,解决了防火墙的问题。因此,一般使用被动模式比较多。

一、服务端和客户端开启FTP服务(以vsftpd为例)

1. 两台服务器操作相同,安装vsftpd来完成FTP的设置(进入root权限操作:su root)
rpm -qa|grep vsftpd   #确定是否存在         vsftpd -version
sudo apt-get install vsftpd               yum -y install vsftpd
sudo /etc/init.d/vsftpd restart           sudo service vsftpd restart
2. 修改FTP配置文件:
vi /etc/vsftpd/vsftpd.conf     也许在 /etc/vsftpd.conf

覆盖成以下内容:

# Example config file /etc/vsftpd/vsftpd.conf 
# 
# The default compiled in settings are fairly paranoid. This sample file 
# loosens things up a bit, to make the ftp daemon more usable. 
# Please see vsftpd.conf.5 for all compiled in defaults. 
# 
# READ THIS: This example file is NOT an exhaustive list of vsftpd options. 
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's 
# capabilities. 
# 
# Allow anonymous FTP? (Beware - allowed by default if you comment this out). 
anonymous_enable=YES 
# 
# Uncomment this to allow local users to log in. 
# When SELinux is enforcing check for SE bool ftp_home_dir 
local_enable=YES 
# 
# Uncomment this to enable any form of FTP write command. 
write_enable=YES 
# 
# Default umask for local users is 077. You may wish to change this to 022, 
# if your users expect that (022 is used by most other ftpd's) 
local_umask=022 
# 
# Uncomment this to allow the anonymous FTP user to upload files. This only 
# has an effect if the above global write enable is activated. Also, you will 
# obviously need to create a directory writable by the FTP user. 
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access 
#anon_upload_enable=YES 
# 
# Uncomment this if you want the anonymous FTP user to be able to create 
# new directories. 
#anon_mkdir_write_enable=YES 
# 
# Activate directory messages - messages given to remote users when they 
# go into a certain directory. 
dirmessage_enable=YES 
# 
# Activate logging of uploads/downloads. 
xferlog_enable=YES 
# 
# Make sure PORT transfer connections originate from port 20 (ftp-data). 
connect_from_port_20=YES 
# 
# If you want, you can arrange for uploaded anonymous files to be owned by 
# a different user. Note! Using "root" for uploaded files is not 
# recommended! 
#chown_uploads=YES 
#chown_username=whoever 
# 
# You may override where the log file goes if you like. The default is shown 
# below. 
#xferlog_file=/var/log/xferlog 
# 
# If you want, you can have your log file in standard ftpd xferlog format. 
# Note that the default log file location is /var/log/xferlog in this case. 
xferlog_std_format=YES 
# 
# You may change the default value for timing out an idle session. 
#idle_session_timeout=600 
# 
# You may change the default value for timing out a data connection. 
#data_connection_timeout=120 
# 
# It is recommended that you define on your system a unique user which the 
# ftp server can use as a totally isolated and unprivileged user. 
#nopriv_user=ftpsecure 
# 
# Enable this and the server will recognise asynchronous ABOR requests. Not 
# recommended for security (the code is non-trivial). Not enabling it, 
# however, may confuse older FTP clients. 
#async_abor_enable=YES 
# 
# By default the server will pretend to allow ASCII mode but in fact ignore 
# the request. Turn on the below options to have the server actually do ASCII 
# mangling on files when in ASCII mode. The vsftpd.conf(5) man page explains 
# the behaviour when these options are disabled. 
# Beware that on some FTP servers, ASCII support allows a denial of service 
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd 
# predicted this attack and has always been safe, reporting the size of the 
# raw file. 
# ASCII mangling is a horrible feature of the protocol. 
#ascii_upload_enable=YES 
#ascii_download_enable=YES 
# 
# You may fully customise the login banner string: 
#ftpd_banner=Welcome to blah FTP service. 
# 
# You may specify a file of disallowed anonymous e-mail addresses. Apparently 
# useful for combatting certain DoS attacks. 
#deny_email_enable=YES 
# (default follows) 
#banned_email_file=/etc/vsftpd/banned_emails 
# 
# You may specify an explicit list of local users to chroot() to their home 
# directory. If chroot_local_user is YES, then this list becomes a list of 
# users to NOT chroot(). 
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that 
# the user does not have write access to the top level directory within the 
# chroot) 
#chroot_local_user=YES 
#chroot_list_enable=YES 
# (default follows) 
#chroot_list_file=/etc/vsftpd/chroot_list 
# 
# You may activate the "-R" option to the builtin ls. This is disabled by 
# default to avoid remote users being able to cause excessive I/O on large 
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume 
# the presence of the "-R" option, so there is a strong case for enabling it. 
#ls_recurse_enable=YES 
# 
# When "listen" directive is enabled, vsftpd runs in standalone mode and 
# listens on IPv4 sockets. This directive cannot be used in conjunction 
# with the listen_ipv6 directive. 
listen=YES 
# 
# This directive enables listening on IPv6 sockets. By default, listening 
# on the IPv6 "any" address (::) will accept connections from both IPv6 
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6 
# sockets. If you want that (perhaps because you want to listen on specific 
# addresses) then you must run two copies of vsftpd with two configuration 
# files. 
# Make sure, that one of the listen options is commented !! 
 
pasv_enable=YES 
pasv_min_port=10000 
pasv_max_port=10003 
pasv_promiscuous=YES
chroot_list_enable=NO
chroot_local_user=NO
utf8_filesystem=YES
allow_writeable_chroot=YES
local_root=/samba/
3. 关于上述配置,做以下解释:
(1) pasv_enable=YES

FTP内网穿透需要采用被动模式。

(2) pasv_port

服务器用于数据传输的随机端口需要用“pasv_max_port” 和 “pasv_min_port’”设置范围
这里意思是只允许使用10000,10001,10002,10003这四个端口连接,间接的意思是只能同时有四个用户连接。

(3) chroot

在这里插入图片描述

(4) 默认访问目录与权限
allow_writeable_chroot=YES
local_root=/samba/

/samba/目录是你要访问的目录 ,并开启权限。这里由于是为了远程外网访问内网服务器samba服务,所以直接设置默认访问目录为samba目录,该目录挂载了移动硬盘,具体操作方法可见我的博文

4. 端口放行
#查看iptables是否已安装
iptables --version

#查看放行端口
iptables -nL

#开启指定端口 (开:ACCEPT   关:DROP)
iptables -I INPUT -i eth0 -p tcp --dport 20 -j ACCEPT
iptables  -I OUTPUT -o eth0 -p tcp --sport 20 -j ACCEPT
iptables -I INPUT -i eth0 -p tcp --dport 21 -j ACCEPT
iptables  -I OUTPUT -o eth0 -p tcp --sport 21 -j ACCEPT
iptables -I INPUT -i eth0 -p tcp --dport 10000:10003 -j ACCEPT
iptables  -I OUTPUT -o eth0 -p tcp --sport 10000:10003 -j ACCEPT

#验证
iptables -nL

在这里插入图片描述

5. 启动ftp服务
#启动
sudo /etc/init.d/vsftpd restart

#查看状态(务必查看)
service vsftpd status

#确保端口被监听
netstat -natp

在这里插入图片描述

如果在检查vsftpd status显示下图,请完全复制上面的配置文件。
在这里插入图片描述
下图表示vsftpd status正常,vsftpd已开始正常运行。

在这里插入图片描述

此时,可以通过FileZilla、WinSCP等,通过FTP协议访问服务器。

6. 放行root用户(可选)

ftp默认是不允许root用户操作的,如果我们想直接进入根目录进行操作,可以选择放行root用户:

cd /etc/vsftpd        # 进入vsftpd目录

ls                    # 查看该目录包含的文件

vi ftpusers           # 在root前加#注释root

vi user_list          # 在root前加#注释root

二、配置FRP

1. frps配置:
sudo apt-get update

wget https://github.com/fatedier/frp/releases/download/v0.17.0/frp_0.17.0_linux_amd64.tar.gz

tar -zxvf frp_0.17.0_linux_amd64.tar.gz  #解压缩:tar xvf 文件名

cd frp_0.17.0_linux_amd64                #进入解压目录

#修改frps.ini文件
sudo vim ./frps.ini

添加以下内容:
[common]
bind_addr = 0.0.0.0         # 0.0.0.0为服务器全局所有IP可用,假如你的服务器有多个IP则可以这样做,或者填写为指定其中的一个服务器IP,支持IPV6.
bind_port = 7000            # 客户端与服务端进行通信的端口,即frp服务端口,需与客户端server_port一致
dashboard_port = 7500       # 控制台端口 通过 Dashboard 可以方便的查看 FRP 的状态以及代理统计信息展示 通过 http://[server_addr]:7500 访问 Dashboard 界面,用户名密码默认都为 admin。
#后台保持启动 需要在frp_0.17.0_linux_amd64目录下操作
nohup ./frps -c ./frps.ini &

返回:nohup: ignoring input and appending output to ‘nohup.out’ 代表执行成功,ctrl+c关闭即可;用ps -ef 会在进程中看见frp工作进程。

2. frpc配置:
sudo apt-get update

wget https://github.com/fatedier/frp/releases/download/v0.17.0/frp_0.17.0_linux_arm.tar.gz

tar -zxvf frp_0.17.0_linux_arm.tar.gz  #解压缩:tar xvf 文件名

cd frp_0.17.0_linux_arm                #进入解压目录

#修改frps.ini文件
sudo vim ./frpc.ini

添加以下内容(需要删除注释)
[common]
server_addr = your_server_ip               #VPS服务器IP
server_port = 7000                         #端口,与服务端bind_port一致  

[ftp]
type = tcp
local_ip = 127.0.0.1
local_port = 21
remote_port = 2121

[ftp1]
type = tcp
local_ip = 127.0.0.1
local_port = 10000
remote_port = 10000

[ftp2]
type = tcp
local_ip = 127.0.0.1
local_port = 10001
remote_port = 10001

[ftp3]
type = tcp
local_ip = 127.0.0.1
local_port = 10002
remote_port = 10002

[ftp4]
type = tcp
local_ip = 127.0.0.1
local_port = 10003
remote_port = 10003
#强烈建议先运行该命令,只要运行过程中有错误,都会回显
./frpc -c ./frpc.ini

如果运行报错,需要重新修改配置文件,需要先停止FRP的运行:

#查询进程与结束进程命令:
ps -ef |grep frpc
kill -9 [进程号]
# 在frp_0.17.0_linux_arm目录下,启动命令
./frpc -c ./frpc.ini
# 后台启动
nohup ./frpc -c ./frpc.ini &

设置为开机自动启动

sudo vim /etc/systemd/system/frpc.service
[Unit]
Description=frpc daemon
After=syslog.target  network.target
Wants=network.target

[Service]
Type=simple
ExecStart=/home/pi/frp_0.17.0_linux_arm/frpc -c /home/pi/frp_0.17.0_linux_arm/frpc.ini
Restart= always
RestartSec=1min

[Install]
WantedBy=multi-user.target

注意ExecStart中要配置成自己的绝对路径,绝对路径可以通过pwd命令查询。

#启动frpc
systemctl daemon-reload
systemctl start frpc

#设置为开机启动
systemctl enable frpc

在这里插入图片描述

三、实现内外穿透,外网实现访问

方法1:ftp链接浏览器直接访问

通过 ftp://用户名:密码@IP:端口 的形式访问samba

在这里插入图片描述

方法2:使用FileZilla登陆FTP访问:

链接:https://pan.baidu.com/s/11hd0Kqw5pGxRcer6aBh__Q
提取码:jtn2

在这里插入图片描述
在这里插入图片描述

方法3:本地FTP映射:

在这里插入图片描述
在这里插入图片描述
最后友情提示一点,你是不是在用公网的服务器地址尝试连接本地内网的FTP文件夹,舍不得开热点,恐怕要经历失败(手动狗头)。点此了解出现 227 Entering Passive Mode错误 原因及解决方法

©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页