哎呀呀呀暈暈暈暈暈暈!先來嘆氣一波,花費了一天的時間
環境:虛擬機
搭建原因:虛擬機上掛載U盤失敗后就想到了使用ftp文件上傳下載,於是行動
搭建過程:ubuntu安裝ftp很簡單:
sudo apt-get update sudo apt-get install vsftpd [vsftpd --version 檢測是否安裝]
ftp配置是很有學問的,必須要強調的是如果你的ftp服務無法啟動,十有八九就是配置文件的配置錯誤
配置參考如下:
listen=NO listen_ipv6=YES # Allow anonymous FTP? (Disabled by default). anonymous_enable=NO # Uncomment this to allow local users to log in. 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 # Activate directory messages - messages given to remote users when they # go into a certain directory. dirmessage_enable=YES # # If enabled, vsftpd will display directory listings with the time # in your local time zone. The default is to display GMT. The # times returned by the MDTM FTP command are also affected by this # option. use_localtime=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 # You may override where the log file goes if you like. The default is shown # below. xferlog_file=/var/log/vsftpd.log # # 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 fully customise the login banner string: ftpd_banner=Welcome to FTP service. # 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 # This option should be the name of a directory which is empty. Also, the # directory should not be writable by the ftp user. This directory is used # as a secure chroot() jail at times vsftpd does not require filesystem # access. secure_chroot_dir=/var/run/vsftpd/empty # # This string is the name of the PAM service vsftpd will use. # pam_service_name=vsftpd pam_service_name=ftp # This option specifies the location of the RSA certificate to use for SSL # encrypted connections. rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key ssl_enable=NO # # Uncomment this to indicate that vsftpd use a utf8 filesystem. utf8_filesystem=YES
上面的配置文件中,我不是很熟悉,我挑一些我稍微知道的講。 listen=NO不了解,我查看了很多博客,這個設置的是YES,但是我設置成YES,登陸直接被拒絕。 anonymous_enable=NO拒絕匿名登陸 write_enable=YES設置可以上傳文件,這個設置看需要個人需要 xferlog_enable=YES開啟日志記錄 xferlog_file=/var/log/vsftpd.log設置日志文件路徑 xferlog_std_format=YES設置日志格式為標准輸出 connect_from_port_20=YES綁定20端口 ftpd_banner=Welcome to FTP service.歡迎語句,在使用shell時可以看到 下面這幾個的設置比較重要: chroot_local_user=YES chroot_list_enable=YES chroot_list_file=/etc/vsftpd.chroot_list 上面的這幾個配置實現的功能是:用戶被限制在自己的主目錄下。用戶名單來源於/etc/vsftpd.chroot_list。 具體可以參考:http://blog.csdn.net/bluishglc/article/details/42398811 一個重要的配置 pam_service_name=ftp原配置中為vsftpd,ubuntu用戶需要更改成ftp 關於編碼: utf8_filesystem=YES 不知道這項有沒有起作用,上傳的文件不亂碼,用瀏覽器打開是亂碼(瀏覽器編碼問題?),使用windows自帶的文件資源管理器是沒有亂碼的,使用filezila亂碼。 創建用戶: mkdir /home/username sudo useradd username -g ftp -d /home/username -m username sudo passwd username's password mkdir /home/username/pub chmod 777 -R /home/username/pub新建一個pub目錄用於存放文件,並且賦予全部訪問權限 usermod -s /sbin/nologin username限制用戶username只能通過ftp登陸,而不能直接登陸服務器 重要的一點: 新建/etc/vsftpd.chroot_list將username放進去 啟動vsftpd或者重啟 systemctl start vsftpd或者service vsftpd start systemctl restart vsftpd或者service vsftpd restart 登陸: 在windows的文件資源管理器或者在瀏覽器中打開ftp://your_server_ip輸入賬號密碼,即可用登陸。瀏覽器中只能查看,文件操作如新建等需要在window的文件資源管理器中或者filezila中進行。
測試
使用filezilla等工具
現在在windows下打開cmd窗口,執行命令:
ftp [你的linux機器的ip地址]
- 1
linux下查看ip地址的命令為:
ifconfig
- 1
執行完ftp命令后,如果連接成功,會讓你輸入用戶名和密碼,輸入我們剛剛添加的用戶uftp的用戶名和密碼即可:
命令pwd和lcd分別可以查看遠程linux下和本地windows下的當前目錄:
現在在linux下的目錄/home/uftp下新建文件test_ftp_download.txt,在windows下的家目錄(我的為C:\Users\40696)下新建文件test_ftp_upload.txt,分別用來測試下載和上傳功能:
下載文件使用命令:get [文件名] 上傳文件使用命令:put [文件名]
- 1
- 2