Saturday 31 January 2015

windows 与 linux 之间的 ftp



本文分为两个 part, 分别是 centOS 6 连接去 win8.1(ip 10.8.7.158) 的 ftp server, 以及 win8.1 连接去 centOS 6(ip 10.8.7.144)  的 ftp server。例子中, centOS 直接用 root #。


Part A: centOS 6 连接去 win8.1 的 ftp server


首先, 目标是建立 win8.1 ftp server。win+s 搜索打开 Programs and Features


按左下角的 Turn Windows features on or off, 确保 Internet Information Services, FTP Server, Web Management Tools   3 个的空格都有点或勾。 然后 OK。



然后 win+s 搜索打开 Administrative Tools


双击打开  Internet Information Services (IIS) Manager



选 No 不鸟它。


expand 然后右键点击 Sites,选 Add FTP Site...


帮 FTP site 取个名字, 然后 browse 要分享的 folder path。 Next。


拉下选你的 lan IP 地址。 圈上 No SSL 。Next。
I





打勾 Anonymous。 Allow access to: 选 All users。打勾 Read 权限就够了。


有时可能会 connect 不到, 可以尝试选 site 然后点击图中右下角的 Restart


直接用firefox 浏览成功了(用 File explorer 也能浏览)。 看得见我分享的 C:\holeftp 的 lala folder。


 cmd 也成功了。 ftp IP 地址。 输入 anonymous 用户名, 因为没放密码所以 password 只按 [Enter] 就行。 dir 看得见 C:\holeftp 的 lala folder。



可是其它电脑, centOS 仍然 connect 不到 windows ftp server。 是为什么呢 ? 那就要 check firewall 了。



win+s 搜索打开 Windows Firewall


选左下角的 Advanced settings


右键 Inbound RulesNew Rule...

PortNext

输入  20, 21Next

Allow the connectionNext

打勾 Domain 和 Private。Next


随便帮这个 rule 命名, 小白 ftp ruleFinished

CentOS 成功 connect 了。 但是又有另一个 error。 输入指令 dir, 它会讲 "Entering Passive Mode", hang 在那里一直到 timed out。


So, 在刚刚新建的 小白 ftp rule, 右键选 Properties


进去 Protocols and ports tab, 把 20, 21 改成 All ports。 Apply


 然后去 Scope tab。Local IP address 点选 These IP addresses:, 按 Add...
This IP address range。 输入愿意分享的 lan IP 范围。

然后 Remote IP address 也一样步骤。


按 Apply。 OK。CentOS 那边也立马带来捷报 :)


Part B: win8.1 连接去 centOS 6 的 ftp server

如果还没有安装的话就 yum install vsftpd。


service vsftpd start 启动 server。vsftpd 对小白来说这个字有点难记, 但是只需要铭记在心 Very Secure  + ftp + Daemon 就不会难了。


centOS 自己 connect 自己, 搞掂。可是默认的 pub directory 是在哪里呢?

哦, pub 是 under /var/ftp/。ok, 想法子改成别的 path。


vi /etc/vsftpd/vsftpd.conf 然后加上这条 line (如果还没有 anon_root 的话)

anon_root=/root/Videos/

anon_root 故名思义就是给 anonymous 的 root directory。 /root/Videos/ 就是我要分享的 directory path。我之前已经加过 Downloads 了,所以图中用 # comment out 旧的。


改了 config file 就要 restart。service vsftpd restart


"500 OOPS: cannot change directory:/root/Videos/ #Downloads/" error 。



setsebool -P ftp_home_dir on (别忘了 restart vsftpd)可以解决这个问题。哎, 我之前也是用这招解决的, 做么现在不能 liao neh?


转念一想, error 信息把 comment 都  print 出来, 可能 trailing comment 不 support。就把 comment #/Downloads/ 丢了


终于可以了 :)


centOS 已经可以 connect 回来自己。但是 windows 8.1 那边 connect 不到 centOS ftp server。


如果我用 iptables -F flush 掉全部 chains/rules


windows 8.1 那里马上传来捷报。


不过要深入探索原因。总不能丢完全部 rule 吧, 看完下面你会发现其实跟之前的 windows 一样道理。
首先,  service iptables restart 弄回开机时的 /etc/sysconfig/iptables。

如果这时候丢掉可疑的 line 9 reject-with icmp-host-prohibited (可疑个屁,其实小白我是每一条 line 一个个丢才找到。)


iptables -D INPUT 9


windows 8.1 那边也一样 works 了。

service iptables restart, 试验

 iptables -I INPUT 9 -m iprange --src-range 10.8.7.0-10.8.7.255 -p tcp  -j ACCEPT




一样成功。其实跟 windows 一样允许 lan IP range。

9 就是飞去第九条 line(要配合 -I INPUT, 不是 -A INPUT ) , 原本第九条 line 的 reject-with icmp-host-prohibited 就往后移。上面的 priority 较高, 所以才放在 reject-with icmp-host-prohibited 前面。当然, 放 INPUT 1 也行。


把现在的状态 save 进去, iptables 重启就会使用这个更改了。


chkconfig vsftpd on 可以让它在 boot runlevels 5 时自动启动。


bonus note:

1. 如何下载名字太长的文件~


2. 要重新输入 IP 通常都是想不起来,必须特意去目标电脑输入 ifconfig/ipconfig 找。 所以我们就来制作一个方便输入 shortcut。
(1.) win+x+a 打开 admin 权限的 cmd。
(2.) C:\Windows\System32> mkdir c:\windows\hole
(3)  C:\Windows\System32> notepad
(4) 打开notepad 后, 输入 @doskey centftp=ftp -A 10.8.7.144
(5) 然后菜单 File -> Save As... -> 去 C:\Windows\hole 那里,输入 holedoskey.bat 文件名, Save ad type:  则选 All Files。Save。
(6) win+r 输入打开 regedit, 一路狂奔 HKEY_CURRENT_USER ->Software ->Micorsoft -> Command Processor。
(7) 对着 Command Processor 右键选 New -> String Value。改名为 Autorun。然后右键那个 Autorun, 选 Modify..., 在 Value data 输入 c:\windows\hole\holedoskey.bat (就是刚才 notepad 新建的文件 path)。OK。
(8) win+r+c 打开新的 cmd (旧的还没有 refresh), 输入 centftp, 就等同于 ftp -A 10.8.7.144(-A 是 login as anonymous)。


3. 
如果是  centos 那边, 可以在 ~/.bashrc 增加 alias winftp="ftp 10.8.7.158"
. ~/.bashrc refresh 即可。不过跟 windows 不一样, 似乎没有 -A。不过我们可以使用 ~/.netrc 文件 (man 5 netrc)。 这个文件坐落在 home directory (如果没有就新建吧)。

default login anonymous password love

那么登入 anonymous 用户就会自动输入 love 密码。由于 anonymous 不需要密码,所以 love 是放爽的。

如果要暂时阻止 ~/.netrc, 可以放 -n argument。

4. File may not have transferred correctly. 错误信息。默认下不 expect 传送图片/binary。收到的图片是 corrupted "有时"不能打开的。



换去 bin (binary mode) 即可解决。(注: 只发生在 CentOS,  Windows 没有这个必要)



5. 要在不储存的情况下,窥看文件的内容, 可以使用 get 文件名字 减号




最后,来一只 pikachu 镇楼。