The error message “cp: Permission denied” typically occurs when the user doesn’t have permission to access the source file or the destination directory.
Here are the errors we usually meet with “cp Permission denied” error in Linux:
Error | Description |
---|---|
cp: cannot create regular file ‘destination/path’: Permission denied | Indicates that the user does not have permission to create the destination file. |
cp: cannot stat ‘source/path’: Permission denied | Indicates that the user does not have permission to read the source file. |
cp: cannot access ‘source/path’: Permission denied | Indicates that the user does not have permission to access the source file. |
cp: cannot open ‘howtouselinux.txt’ for reading: Permission denied | Indicates that the user does not have permission to read the source file named “howtouselinux.txt”. |
Here are 2 ways to fix the error.
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!Table of Contents
Change file permissions or ownership
The “cp Permission denied” error message usually indicates that the cp command is unable to access the source file or the destination directory due to insufficient permissions.
We can use the following steps to check it.
- Check the permissions of the source file and destination directory using the ls -l command.
- Ensure that you have permission to read the source file and write to the destination directory.
- If the destination directory is owned by another user, try changing the ownership to your user using the chown command or change the permission with the chmod command.
Note: in many case, you need to switch to root account to change the file ownership or permissions.
You can use the ls -l command to check the permissions of both the source file and destination directory. Here’s an example:
$ ls -l /path/to/source/file
-rw-r--r-- 1 user user 4096 May 10 10:00 /path/to/source/file
$ ls -ld /path/to/destination/dir
dr-xr-xr-x 2 user user 4096 May 10 10:00 /path/to/destination/dir
In this example, the source file has read and write permissions for the owner (user). The destination directory has read and execute permissions for the owner, group, and others.
In this case the destination directory doesn’t have write permissions for the user, you can use the following command to add write permissions:
$ chmod u+w /path/to/destination/dir
Once the permissions are set correctly, you should be able to run the cp command without the “cannot stat” error.
Here is another example of how to fix the error by changing the file ownership:
This error occurs because the user does not have the necessary permissions to read the file or write to the “newdir” directory.
To fix this error, we can use the “chown” command to change the ownership of the “newdir” directory to our user.
Once we have changed the ownership, we can try copying the file again using the “cp” command, and this time it should work without any errors.
$ ls -l file.txt newdir
-rw-r--r-- 1 user user 1234 May 1 12:00 file.txt
drwxr-xr-x 2 root root 4096 May 1 12:00 newdir
$ cp file.txt newdir/
cp: cannot stat 'file.txt': Permission denied
$ chown user:user newdir
# Change ownership to your user
$ cp file.txt newdir/
# Try copying again
$ ls -l newdir
-rw-r--r-- 1 user user 1234 May 1 12:00 file.txt
Run cp command with sudo or as root user
If you don’t have permission to access the source file or the destination directory, running the cp command with sudo or as the root user can help.
Here’s an example:
sudo cp /path/to/source/file /path/to/destination/directory/
This will run the cp command with elevated privileges, allowing you to access the necessary files and directories.
However, it’s important to exercise caution when using sudo or the root user, as it can potentially cause unintended consequences if used improperly.
The disadvantage is that we have to add cp command to sudo configure file.
If cp command is not listed in the sudoers file, the error message would typically be “command not allowed” or “command not found”.
This means that the user does not have permission to run the specified command with sudo.
Let’s see what is sudo.
The sudo command in Linux is used to give users the ability to perform certain tasks that require root or administrative privileges.
It allows a user to execute a command with elevated privileges temporarily, rather than logging in as the root user, which is not recommended for security reasons.
When a user executes a command with sudo, they are prompted to enter their own password (not the root password), and if their credentials are verified, the command is executed with elevated privileges.
Sudo is configured using the /etc/sudoers file, which determines which users or groups are allowed to use sudo and what commands they are allowed to run.
By default, the sudoers file is edited using the visudo command, which provides syntax checking and prevents multiple users from editing the file simultaneously.
Using sudo can help prevent accidental damage to the system and is considered a best practice for Linux system administration.
However, it should be used with caution and only for necessary tasks, as running commands with elevated privileges can be risky if not properly executed.