263

I am trying to set up FTP on Amazon Cloud Server, but without luck. I search over net and there is no concrete steps how to do it.

I found those commands to run:

$ yum install vsftpd
$ ec2-authorize default -p 20-21
$ ec2-authorize default -p 1024-1048
$ vi /etc/vsftpd/vsftpd.conf
#<em>---Add following lines at the end of file---</em>
    pasv_enable=YES
    pasv_min_port=1024
    pasv_max_port=1048
    pasv_address=<Public IP of your instance>
$ /etc/init.d/vsftpd restart

But I don't know where to write them.

Blue
  • 22,608
  • 7
  • 62
  • 92
SharkTheDark
  • 3,089
  • 4
  • 24
  • 29

12 Answers12

576

Jaminto did a great job of answering the question, but I recently went through the process myself and wanted to expand on Jaminto's answer.

I'm assuming that you already have an EC2 instance created and have associated an Elastic IP Address to it.


Step #1: Install vsftpd

SSH to your EC2 server. Type:

> sudo yum install vsftpd

This should install vsftpd.

Step #2: Open up the FTP ports on your EC2 instance

Next, you'll need to open up the FTP ports on your EC2 server. Log in to the AWS EC2 Management Console and select Security Groups from the navigation tree on the left. Select the security group assigned to your EC2 instance. Then select the Inbound tab, then click Edit:

enter image description here

Add two Custom TCP Rules with port ranges 20-21 and 1024-1048. For Source, you can select 'Anywhere'. If you decide to set Source to your own IP address, be aware that your IP address might change if it is being assigned via DHCP.

enter image description here



Step #3: Make updates to the vsftpd.conf file

Edit your vsftpd conf file by typing:

> sudo vi /etc/vsftpd/vsftpd.conf

Disable anonymous FTP by changing this line:

anonymous_enable=YES

to

anonymous_enable=NO

Then add the following lines to the bottom of the vsftpd.conf file:

pasv_enable=YES
pasv_min_port=1024
pasv_max_port=1048
pasv_address=<Public IP of your instance> 

Your vsftpd.conf file should look something like the following - except make sure to replace the pasv_address with your public facing IP address:

enter image description here

To save changes, press escape, then type :wq, then hit enter.



Step #4: Restart vsftpd

Restart vsftpd by typing:

> sudo /etc/init.d/vsftpd restart

You should see a message that looks like:

enter image description here


If this doesn't work, try:

> sudo /sbin/service vsftpd restart



Step #5: Create an FTP user

If you take a peek at /etc/vsftpd/user_list, you'll see the following:

# vsftpd userlist
# If userlist_deny=NO, only allow users in this file
# If userlist_deny=YES (default), never allow users in this file, and
# do not even prompt for a password.
# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
# for users that are denied.
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
nobody

This is basically saying, "Don't allow these users FTP access." vsftpd will allow FTP access to any user not on this list.

So, in order to create a new FTP account, you may need to create a new user on your server. (Or, if you already have a user account that's not listed in /etc/vsftpd/user_list, you can skip to the next step.)

Creating a new user on an EC2 instance is pretty simple. For example, to create the user 'bret', type:

> sudo adduser bret
> sudo passwd bret

Here's what it will look like:

enter image description here



Step #6: Restricting users to their home directories

At this point, your FTP users are not restricted to their home directories. That's not very secure, but we can fix it pretty easily.

Edit your vsftpd conf file again by typing:

> sudo vi /etc/vsftpd/vsftpd.conf

Un-comment out the line:

chroot_local_user=YES

It should look like this once you're done:

enter image description here

Restart the vsftpd server again like so:

> sudo /etc/init.d/vsftpd restart

All done!


Appendix A: Surviving a reboot

vsftpd doesn't automatically start when your server boots. If you're like me, that means that after rebooting your EC2 instance, you'll feel a moment of terror when FTP seems to be broken - but in reality, it's just not running!. Here's a handy way to fix that:

> sudo chkconfig --level 345 vsftpd on

Alternatively, if you are using redhat, another way to manage your services is by using this nifty graphic user interface to control which services should automatically start:

>  sudo ntsysv

enter image description here

Now vsftpd will automatically start up when your server boots up.


Appendix B: Changing a user's FTP home directory

* NOTE: Iman Sedighi has posted a more elegant solution for restricting users access to a specific directory. Please refer to his excellent solution posted as an answer *

You might want to create a user and restrict their FTP access to a specific folder, such as /var/www. In order to do this, you'll need to change the user's default home directory:

> sudo usermod -d /var/www/ username

In this specific example, it's typical to give the user permissions to the 'www' group, which is often associated with the /var/www folder:

> sudo usermod -a -G www username
clone45
  • 8,952
  • 6
  • 35
  • 43
  • 3
    In `step 3` after adding lines in file, how can I save it? – Sumit Bijvani Feb 26 '13 at 13:16
  • 3
    ok all done, now how can I connect to ftp? – Sumit Bijvani Feb 26 '13 at 13:52
  • 7
    Hi Sumit. Vi is a pertty tricky editor. To save your work, type escape, then ":wq" (without the quotes), then hit enter. As for FTP, that's hard to answer because it will be based on your FTP client. If I have time, I'll try to append some instructions to my answer for setting up some popular FTP clients. Personally, I use Aptana Studio. In Aptana, you create an SFTP site and supply Aptana with the public key authentication file that you got when you created your EC2 instance. If you are using filezilla, try using pageant.exe. Cheers! – clone45 Mar 01 '13 at 03:20
  • 1
    If you are on windows ec2 instance look here http://support.microsoft.com/kb/200475 – joncodo Sep 05 '13 at 14:57
  • Followed these steps to the letter and I still get "Could not read from socket: ECONNRESET - Connection reset by peer" and "Failed to retrieve directory listing" after connecting nad switching to binary mode (PASV command) – Paul Oct 30 '13 at 16:05
  • @Paul - I'm not really sure how to solve that issue. I would check your permissions set on the user's home directory. It sounds like the FTP is successful, but something is preventing you from getting the directory listings. Also try switching your FTP client from active mode to passive mode (or the opposite) and see if that helps. Good luck! – clone45 Oct 30 '13 at 18:07
  • To grant write permission to the new ftp user: sudo chown -R FTPUSERNAME /path/to/folder – Luca Nov 27 '13 at 21:04
  • How to login FTP, i tried through Filezilla, but doen't work after these all step. – Ashok KS Nov 29 '13 at 10:29
  • Just wanted to quickly add a couple steps I had to take to make this work. 1) local_enable=yes 2) one_process_model=no 3) iptables to open firewall to ports 20-21,1024-1048. – trimbletodd Jan 28 '14 at 14:30
  • How can I change the default FTP path? – Lazaro Fernandes Lima Suleiman Jan 31 '14 at 20:28
  • This should be the model for how all stackoverflow questions are asked, clear, concise and it works. – vaene Apr 02 '14 at 22:09
  • Nowadays you also need this article to get it to work properly: http://blog.thefrontiergroup.com.au/2012/10/making-vsftpd-with-chrooted-users-work-again/ – Danny Schoemann May 29 '14 at 14:05
  • I also had create the vsftpd.chroot_list file and add the allowed users. – Sebastian Jul 04 '14 at 09:40
  • 1
    Perhaps you can give the credit to your source: http://cafeandrew.com/archives/2339 – amertkara Jan 27 '15 at 15:24
  • 11
    Actually, perhaps he could give credit to me. He reposed my answer on January 13. – clone45 Jan 28 '15 at 16:16
  • For `Step #4`, I had to use the following: `sudo /sbin/service vsftpd restart` – Jesse Jun 05 '15 at 04:40
  • Thanks Jesse, I've added your alternate restart command to the answer. If anyone else runs into this, let me know. – clone45 Jun 05 '15 at 18:16
  • @clone45 to enable write permission on folder, as it's certainly the use case of most of readers, can you add to appendix B: `sudo usermod -a -G www username` ? – cyrilchampier Jun 29 '15 at 11:29
  • 1
    This doesn't work. FTP user either times out or gets a complaint that there are insufficient authentication methods available. – Dissident Rage Aug 27 '15 at 17:05
  • @clone45 I just have one problem. After I changed the user's home directory as you mentioned in Appendix B then I can't login with user through SFTP nor SSH and getting error for permission denied for public key. immediately after I undo it and set the default home directory then I can use SFTP and SSH login but then the user access is not restricted. I can see any other folders outside of www folder easily through filezilla. Do you have any idea to restrict access of user to www without changing the home directory? – Iman Sedighi Apr 22 '16 at 12:04
  • @ImanSedighi I wish I knew. It seemed strange to me as well that it was necessary to change the user's home directory. Here's a very similar question, but still no really graceful solutions: http://askubuntu.com/questions/73323/how-to-setup-vsftpd-for-multiple-users-including-adding-specific-directories. If you find out, let me know and I'll update my answer. – clone45 Apr 25 '16 at 17:41
  • @clone45 I could find a solution for it but it took an entire day! I have posted it as an answer in below to address this problem – Iman Sedighi Apr 26 '16 at 09:26
  • 1
    @ImanSedighi Fantastic Iman! I've added some text to Appendix B that directs people to your answer. – clone45 Apr 26 '16 at 16:41
  • 1
    This is a wonderfully complete answer and works great for me. I use it all the time. Another tip is to set up an SG in AWS that has the port settings already configured. If you add your instance to that SG (or create your instance with that SG if it's a new one), it saves step 2. You can also combine step 6 back into step 3 so you only need to enter vim/emacs once. – Shawn Sullivan Sep 19 '17 at 17:46
  • This is a great write-up. Note that you will probably need to add the user you created to the "FTP" usergroup: `> gpasswd -a ftp` – MCP Jan 03 '14 at 02:00
  • For systems that use **SysVinit** (Ubuntu since 12.04), the **chkconfig** command is not native. If the system uses **Systemd** replace **Appendix A** with `systemctl enable vsftpd` OR `systemctl enable vsftpd.service`. – guizo Sep 12 '20 at 17:29
27

To enable passive ftp on an EC2 server, you need to configure the ports that your ftp server should use for inbound connections, then open a list of available ports for the ftp client data connections.

I'm not that familiar with linux, but the commands you posted are the steps to install the ftp server, configure the ec2 firewall rules (through the AWS API), then configure the ftp server to use the ports you allowed on the ec2 firewall.

So this step installs the ftp client (VSFTP)

> yum install vsftpd

These steps configure the ftp client

> vi /etc/vsftpd/vsftpd.conf
--    Add following lines at the end of file --
     pasv_enable=YES
     pasv_min_port=1024
     pasv_max_port=1048
     pasv_address=<Public IP of your instance> 
> /etc/init.d/vsftpd restart

but the other two steps are easier done through the amazon console under EC2 Security groups. There you need to configure the security group that is assigned to your server to allow connections on ports 20,21, and 1024-1048

jaminto
  • 3,895
  • 3
  • 32
  • 36
  • I know what those lines means, but I don't know where to type them... That's the problem... – SharkTheDark Aug 13 '11 at 21:18
  • 1
    at the command line on the server that you're installing the ftp server onto? – jaminto Aug 13 '11 at 22:20
  • 2
    Connect to your server via SSH: http://blog.taggesell.de/index.php?/archives/73-Managing-Amazon-EC2-SSH-login-and-protecting-your-instances.html – jaminto Aug 14 '11 at 13:42
  • In case you still get an error (500 OOPS: vsftpd: refusing to run with writable root inside chroot ()), this fixed the issue for me: https://www.benscobie.com/fixing-500-oops-vsftpd-refusing-to-run-with-writable-root-inside-chroot/ – kaore Mar 04 '14 at 15:43
  • I'm shocked that anonymous FTP is ON by default! – jeffkee Jan 13 '18 at 17:49
16

Thanks @clone45 for the nice solution. But I had just one important problem with Appendix b of his solution. Immediately after I changed the home directory to var/www/html then I couldn't connect to server through ssh and sftp because it always shows following errors

permission denied (public key)

or in FileZilla I received this error:

No supported authentication methods available (server: public key)

But I could access the server through normal FTP connection.

If you encountered to the same error then just undo the appendix b of @clone45 solution by set the default home directory for the user:

sudo usermod -d /home/username/ username

But when you set user's default home directory then the user have access to many other folders outside /var/www/http. So to secure your server then follow these steps:

1- Make sftponly group Make a group for all users you want to restrict their access to only ftp and sftp access to var/www/html. to make the group:

sudo groupadd sftponly

2- Jail the chroot To restrict access of this group to the server via sftp you must jail the chroot to not to let group's users to access any folder except html folder inside its home directory. to do this open /etc/ssh/sshd.config in the vim with sudo. At the end of the file please comment this line:

Subsystem sftp /usr/libexec/openssh/sftp-server

And then add this line below that:

Subsystem sftp internal-sftp

So we replaced subsystem with internal-sftp. Then add following lines below it:

 Match Group sftponly
        ChrootDirectory /var/www
        ForceCommand internal-sftp
        AllowTcpForwarding no

After adding this line I saved my changes and then restart ssh service by:

sudo service sshd restart

3- Add the user to sftponly group Any user you want to restrict their access must be a member of sftponly group. Therefore we join it to sftponly by: sudo usermod -G sftponly username

4- Restrict user access to just var/www/html To restrict user access to just var/www/html folder we need to make a directory in the home directory (with name of 'html') of that user and then mount /var/www to /home/username/html as follow:

sudo mkdir /home/username/html
sudo mount --bind /var/www /home/username/html

5- Set write access If the user needs write access to /var/www/html, then you must jail the user at /var/www which must have root:root ownership and permissions of 755. You then need to give /var/www/html ownership of root:sftponly and permissions of 775 by adding following lines:

sudo chmod 755 /var/www
sudo chown root:root /var/www
sudo chmod 775 /var/www/html
sudo chown root:www /var/www/html

6- Block shell access If you want restrict access to not access to shell to make it more secure then just change the default shell to bin/false as follow:

sudo usermod -s /bin/false username
Iman Sedighi
  • 7,624
  • 4
  • 48
  • 55
  • on your ``sudo mount --bind /var/www /home/username/html`` I get told there is no www folder. I assume this is done from root (where the /home folder is)? – elliotrock Jun 19 '17 at 01:10
  • 1
    ``sudo chown root:www /var/www/html`` states chown: invalid group: ‘root:www’ – elliotrock Jun 19 '17 at 01:20
  • 2
    Part 6 should better be `sudo usermod -s /sbin/nologin username` bacause of vsftpd's pam module's default shell restrictions (and it seems to work better in my case). And part 4's `mount` needs to be done on every reboot, so it's a good idea to place it into rc.local. – phy25 Dec 19 '17 at 15:54
11

Great Article... worked like a breeze on Amazon Linux AMI.

Two more useful commands:

To change the default FTP upload folder

Step 1:

edit /etc/vsftpd/vsftpd.conf

Step 2: Create a new entry at the bottom of the page:

local_root=/var/www/html

To apply read, write, delete permission to the files under folder so that you can manage using a FTP device

find /var/www/html -type d -exec chmod 777 {} \;
slavoo
  • 5,798
  • 64
  • 37
  • 39
Ravi Shanker
  • 159
  • 1
  • 4
  • 12
    that chmods every file and folder to 777, which is not secure for a website – sergiogx Apr 21 '14 at 20:37
  • OK. So what change do you suggest on this? – Ravi Shanker Apr 23 '14 at 11:07
  • 3
    You should assign the permissions that are needed, don't just blanket open all permissions to everything or you are asking for trouble. For instance using chmod -R ug+rw /var/www/html will grant read and write permissions for user and group to all files without granting unneeded execute permissions and permissions to other. Then setup users and groups accordingly so that you don't need to modify other. That is if your ftp user can read and write all the files and your webs server can read you are set. Put both users in the same group and add rw to the user and r to the group. – AaronM Jun 30 '14 at 18:57
  • You should apply 775 to the /var/www/html. the rest of sub folders and files can get permissions according to the need. 777 is very unsafe. – Iman Sedighi Apr 23 '16 at 09:27
6

In case you have ufw enabled, remember add ftp:

> sudo ufw allow ftp

It took me 2 days to realise that I enabled ufw.

chbong
  • 61
  • 1
  • 3
6

It will not be ok until you add your user to the group www by the following commands:

sudo usermod -a -G www <USER>

This solves the permission problem.

Set the default path by adding this:

local_root=/var/www/html
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
user1802434
  • 61
  • 1
  • 3
4

Don't forget to update your iptables firewall if you have one to allow the 20-21 and 1024-1048 ranges in.

Do this from /etc/sysconfig/iptables

Adding lines like this:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 20:21 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 1024:1048 -j ACCEPT

And restart iptables with the command:

sudo service iptables restart

Kevin Meek
  • 43
  • 5
4

I've simplified clone45 steps:

Open the ports as he mentioned

sudo su
sudo yum install vsftpd
echo -n "Public IP of your instance: " && read publicip
echo -e "anonymous_enable=NO\npasv_enable=YES\npasv_min_port=1024\npasv_max_port=1048\npasv_address=$publicip\nchroot_local_user=YES" >> /etc/vsftpd/vsftpd.conf
sudo /etc/init.d/vsftpd restart
2

I followed clone45's answer all the way to the end. A great article! Since I needed the FTP access to install plug-ins to one of my wordpress sites, I changed the home directory to /var/www/mysitename. Then I continued to add my ftp user to the apache(or www) group like this:

sudo usermod -a -G apache myftpuser

After this I still saw this error on WP's plugin installation page: "Unable to locate WordPress Content directory (wp-content)". Searched and found this solution on a wp.org Q&A session: https://wordpress.org/support/topic/unable-to-locate-wordpress-content-directory-wp-content and added the following to the end of wp-config.php:

if(is_admin()) {
    add_filter('filesystem_method', create_function('$a', 'return "direct";' ));
    define( 'FS_CHMOD_DIR', 0751 );
}

After this my WP plugin was installed successfully.

CodeBrew
  • 6,457
  • 2
  • 43
  • 48
0

maybe worth mentioning in addition to clone45's answer:

Fixing Write Permissions for Chrooted FTP Users in vsftpd

The vsftpd version that comes with Ubuntu 12.04 Precise does not permit chrooted local users to write by default. By default you will have this in /etc/vsftpd.conf:

chroot_local_user=YES
write_enable=YES

In order to allow local users to write, you need to add the following parameter:

allow_writeable_chroot=YES

Note: Issues with write permissions may show up as following FileZilla errors:

Error: GnuTLS error -15: An unexpected TLS packet was received.
Error: Could not connect to server

References:
Fixing Write Permissions for Chrooted FTP Users in vsftpd
VSFTPd stopped working after update

Community
  • 1
  • 1
Hartmut
  • 725
  • 9
  • 11
0

In case you are getting 530 password incorrect

1 more step needed

in file /etc/shells

Add the following line

/bin/false

-2

FileZila is good FTP tool to setup with Amazon Cloud.

  1. Download FileZila client from https://filezilla-project.org/
  2. Click on File -> Site Manager - >
  3. New Site
  4. Provide Host Name IP address of your amazon cloud location (Port if any)
  5. Protocol - SFTP (May change based on your requirement)
  6. Login Type - Normal (So system will not ask for password each time)
  7. Provide user name and password.
  8. Connect.

You need to do these step only 1 time, later it will upload content to the same IP address and same site.

Pratima
  • 305
  • 2
  • 6