站长梦想,靠谱的在线交易网站 帮助 每日签到

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

  • 时间:2020-09-08 20:14 编辑:未知 来源:网上收集 阅读:3256
  • 扫一扫,手机访问
摘要:使用Frp外网访问内网FTP服务器(以访问内网samba服务为例)FTP是基于TCP的服务,使用两个端口,命令端口和数据端口,命令端口是21,数据端口在主动模式下为20,在被动模式随机分配。FTP协议有两种工作方式:PORT方式和PASV方式,中文意思为主动式和被动式。主动(PORT):PORT(主动)方式的连接过程是:客户端向服务器的FTP端口(默认是21)发送连接请求,服务器接受连接,建立一条命令链路。当需要传送数据时,客户端在命令

使用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 -version1
sudo apt-get install vsftpd               yum -y install vsftpd
sudo /etc/init.d/vsftpd restart           sudo service vsftpd restart12
2. 修改FTP配置文件:
vi /etc/vsftpd/vsftpd.conf     也许在 /etc/vsftpd.conf1

覆盖成以下内容:

# 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/123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
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/12

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

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

#查看放行端口
iptables -nL

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

在这里插入图片描述

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

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

#确保端口被监听
netstat -natp12345678

在这里插入图片描述

如果在检查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前加#注释root1234567

二、配置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

添加以下内容:123456789101112
[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。1234
#后台保持启动 需要在frp_0.17.0_linux_amd64目录下操作
nohup ./frps -c ./frps.ini &12

返回: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添加以下内容(需要删除注释):123456789101112
[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 = 10003123456789101112131415161718192021222324252627282930313233
#强烈建议先运行该命令,只要运行过程中有错误,都会回显./frpc -c ./frpc.ini12

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

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

设置为开机自动启动

sudo vim /etc/systemd/system/frpc.service1
[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.target12345678910111213

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

#启动frpc
systemctl daemon-reload
systemctl start frpc

#设置为开机启动
systemctl enable frpc123456

在这里插入图片描述

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

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

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

在这里插入图片描述

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

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

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

方法3:本地FTP映射:

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


  • 全部评论(0)
资讯详情页最新发布上方横幅
最新发布的资讯信息
【金融/投资|股票】如何研究基本面的?(2020-12-29 01:02)
【站长经验|网站运维】一款很好用的内网穿透工具--FRP(2020-09-08 20:21)
【站长经验|网站运维】使用Frp外网访问内网FTP服务器(以访问内网samba服务为例)(2020-09-08 20:14)
【站长经验|网站运维】使用FRP实现内网穿透,远程访问内网服务器(2020-09-08 20:06)
【站长经验|网站运维】安装 MySQL 后,需要调整的 10 个性能配置项(2020-08-31 23:02)
【站长经验|网站运维】Mysql如何优化MyISAM存储引擎(2020-08-31 20:53)
【站长经验|网站运维】Mysql5.7 innodb innodb_* 参数详解(2020-08-31 17:44)
【站长经验|网站运维】Linux下sysstat安装使用图文详解(2020-08-31 13:20)
【计算机/互联网|】centos安装iostat命令(2020-08-31 13:15)
【站长经验|网站运维】mysql占用磁盘IO过高的解决办法(2020-08-31 13:12)
联系我们
Q Q:258266
电话:18132120255
邮箱:pr888_admin@163.com
时间:09:00 - 19:00
底部广告
联系客服
购买源码 源码咨询 订制开发 联系客服
0577-67068160
手机版

扫一扫进手机版
返回顶部