Experiencing and Diagnosing SFTP Permission Denied Errors
In this step, we'll connect to our SFTP server and experience permission denied errors firsthand. This will help us understand how permission settings affect SFTP operations.
Connecting to SFTP Server Locally
Since we're working in a local environment, we can connect to our SFTP server using the localhost address. Let's connect using the sftpuser we created earlier:
sftp sftpuser@localhost
When prompted for the password, enter the password you set for the sftpuser account (e.g., password123
). If the connection is successful, you'll see a prompt like:
Connected to localhost.
sftp>
If you encounter connection issues, it could be because the SSH service isn't running properly in the container. You can try exiting with exit
and proceeding with the next steps using theoretical examples.
Basic SFTP Commands
Let's explore some basic SFTP commands:
- View the current directory on the remote server:
sftp> pwd
Remote working directory: /home/sftpuser
- List files in the current directory:
sftp> ls
- Navigate to a different directory:
sftp> cd /tmp
sftp> pwd
Remote working directory: /tmp
- Return to your home directory:
sftp> cd
sftp> pwd
Remote working directory: /home/sftpuser
Attempting to Access Files with Different Permissions
Now, let's try to access our test files from the SFTP session:
- Try to get a file from our project directory:
sftp> get /home/labex/project/testfile.txt
Fetching /home/labex/project/testfile.txt to testfile.txt
Permission denied
You'll notice a "Permission denied" error. This is because the sftpuser doesn't have permission to access files in the labex user's home directory.
Exit the SFTP Session
Let's exit the SFTP session to continue:
sftp> exit
Understanding Permission Denied Errors
There are several common reasons for "Permission denied" errors in SFTP:
- File Permissions: The user doesn't have read/write access to the file
- Directory Permissions: The user can't access the directory containing the file
- Ownership Issues: The file/directory belongs to a different user or group
- Path Traversal Restrictions: SFTP configuration might restrict users to certain directories
Let's make one of our test files accessible to our SFTP user:
## Create a directory that can be accessed by others
mkdir -p /tmp/shared
echo "This is a shared file for SFTP testing" > /tmp/shared/shared_file.txt
chmod 777 /tmp/shared
chmod 666 /tmp/shared/shared_file.txt
Now, reconnect to SFTP and try accessing this file:
sftp sftpuser@localhost
After connecting, try:
sftp> get /tmp/shared/shared_file.txt
Fetching /tmp/shared/shared_file.txt to shared_file.txt
/tmp/shared/shared_file.txt 100% 36 1.0KB/s 00:00
This should work because we've given everyone read/write permissions to both the directory and the file.
Exit the SFTP session again:
sftp> exit
Using SFTP with Debug Mode
To get more information about permission errors, you can use SFTP in debug mode:
sftp -v sftpuser@localhost
The verbose output will show you detailed information about the connection and any errors that occur:
debug1: Sending subsystem: sftp
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2412, received 2876 bytes, in 0.1 seconds
Bytes per second: sent 30074.7, received 35857.2
debug1: Exit status 0