SFTP – Connections and File Permissions – Part 1

We did SSH setup as mentioned in SSH setup – Key based authentication

In this article lets talk about “umask” and file permissions while doing SFTP.

When a “user” is created – default “umask” is 0002(—w) – meaning no write permission for others. When the “user” creates a file : the default file permission subtract umask value from 666 (rw-rw-rw) resulting 664. When the “user” creates folder :  the default folder permission subtract umask value from 777 (rwxrwxrwx) resulting 775. In the example below – test.txt file permission ( rw-rw-rw – delete write for others = rw-rw-r– ) and test_folder permission ( rwxrwxrwx – delete write for others = rwxrwxr-x ).

Remember it is not mathematical subtraction – For example with default mask 0002 – if the file permission is already missing write permission for others (664) it wont make 662.

SSH_umask

We can update umask using “umask <id>”. In the example below – updated umask to 0022 – delete write permission for group and others . Now default file permissions changed to 0644 (see test2.txt) and folder permissions changed to 0755 (see test2_folder).

SSH_umask2.JPG

Now lets SFTP these files from Bastion to Client but before that check the SFTP server binary on Client.

SFTP_Client

On Client the “test” user has default umask 0002 as we have not changed since the creation of user. There is no change in the permissions as the files and folders already missing write permission for others.

SSH_umask3.JPG

Now as an example we want files and folders should not have write permission to user, group and others which comes “umask” as 0222. To make this permanent I’ve placed the umask in .bashrc which takes effect as soon as we create shell session for test user. I’ve also did some cleaning by deleting the old files..

SSH_umask4.JPG

Lets try SFTP again.. Files copied to Client missing write permission for everyone as expected.

SSH_umask5.JPG

With SFTP we cannot copy directories directly..

SSH_umask7.JPG

Created couple of directories.. Check the permissions of folders which also missing write permission for everyone.

SSH_umask6.JPG

After copying the folder permissions of folder changed to the ones present on Bastion..

SSH_umask8.JPG

Lets create a file inside “test2_folder” on Bastion host. “test2_folder_file.txt” has permission 664 when created on Bastion host and it is 444 permission on Client host following “umask”.

SSH_umask9.JPG

On Client host..

SSH_umask10.JPG

Lets try one more scenario.. as we have test2 user on Client we will change the umask to 0022 and SFTP the files.

SSH_umask11.JPG

SFTP files from Bastion to Client now as test2 user..

SSH_umask12.JPG

On Client host..

SSH_umask13.JPG

Now let me delete the files and try again with “-P” option which should retain the permissions as present on Bastion host.

SSH_umask16.JPG

On Client host..

SSH_umask17.JPG

Advertisements

2 thoughts on “SFTP – Connections and File Permissions – Part 1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s