How to Fix Permission Denied in Terminal Mac

If you’ve used the Terminal application on your Mac for any length of time, you undoubtedly have received a permission denied message when trying to work with a file or directory. In most cases, you can fix the permission denied problem on Mac Terminal using the sudo command.

The sudo command is placed before the command you wish to run on the file or directory. If you have an administrator login and the password, you can successfully perform that action without the permission denied problem.

My name is Eric, and as a long-time software engineer, I often run into and deal with issues that give a permission denied message. I have found ways to fix or get around issues like this, but I have also learned that you must use caution.

Keep reading below if you would like to see some options you may have in dealing with a permission denied message. I will also discuss why you need to be a little careful when dealing with files or directories that are protected like this.

A Variety of Problems

Before we get started, I just want to mention that there can be a variety of situations that can produce a permission denied message. Generally, you will see this message if you are trying to read, edit, delete, copy or move a file or directory for which you don’t have permission.

If you are dealing with a one-time situation in which you are trying to do something, the sudo command is often the go-to fix. If there is a repetitive action or something you will be regularly doing, it may be best to modify the item’s permission, so this is not an ongoing problem.

Just use the Sudo Command

You can usually use the sudo command to fix any permission denied problems. Sudo stands for substitute or superuser do. This means you can run commands as another user or as the superuser, which generally has permission to modify any files or directories on the system.

To use the sudo command, you will first need administrator privileges on your Mac and also need to know your password. If both are true, you only need to place the sudo command in front of the command you are trying to run.

sudo <command>

For example, if I want to view a file called test.txt using the cat command and get a permission denied error. I can run it using the sudo command, as shown below.

sudo cat test.txt

At this point, you will be prompted for your password. Just enter your password and hit the return key. The system will keep your password active for a set period of time, so if you continue running commands with sudo you will not have to keep entering the password.

You can use the sudo command with just about any other commands you can run in the Terminal application. When running the other commands, you will be running them as the superuser or the root user, which has permission to do nearly anything on your system.

Long Term Fixes

If you have files or directories that you need to edit often, you may not want to use the sudo command constantly. As long as it is appropriate (depending on who the files need to be owned by and security concerns), you can change the ownership or file permissions.

Ownership

Changing the file or directory so that you own it will allow you, for the most part, to do anything you want to do with it. If it is a file owned by another user and not a system file that is part of the macOS, it should be safe to do so by using the chown command.

Since you don’t initially own the file, you’ll need to run the chown command with the sudo command, as shown below.

sudo chown <userid> <filename>

For example, if a user with the username ericwinkler wants to change a file called test.txt to their ownership, they would run the following command.

sudo chown ericwinkler test.txt

As in the example above, using sudo, you will also need to enter your password here. Once complete, the file will be owned by ericwinkler, and they can then do anything they want to with the file.

File or Directory Permissions

There are cases where you may own the file or directory or have just taken ownership of it using the chown command as shown above but still get a permission denied error for whatever you are trying to do. 

In this case, it can sometimes be possible to just change the file permissions. File permissions can be a bit tricky. We could write a whole article just going over them, but for our purposes here, we can open the file up so that it has all possible read, write and execute permissions.

To do this, we use the chmod command with the 777 parameter, which gives all users read/write/execute permissions. Using our same example file from above, the command would be as shown below.

chmod 777 test.txt

As you can see in the example, the file now has rwxrwxrwx, which shows that the owner, group, and everyone have read, write and execute permissions. This should allow you to do whatever you need with the file.

I have used a file in the example above, but keep in mind that these methods can also work for directories. If you have permission denied messages when trying to access or copy something to a directory, you can use these same methods to fix the problem.

Use Caution

Files owned by the root user and set with certain permissions are created that way for a reason. It doesn’t mean you can use the methods above to do things with them, but you should always use caution since they can often be essential files for your macOS.

If necessary, do some research on the files or directory first to ensure that you know what the file or directory is used for and that you can return them to their original state if needed. 

If it is a file, it may be wise to make a copy of it as a backup. Also, make sure your entire system is backed up with Time Machine or some other backup utility to ensure that you can return your system to its previous state if something does go wrong.

Conclusion

You can use the sudo command to override most permission denied problems. Still, if it is an issue you often see with the same file or directory, you may want to change the ownership and permissions to ensure you can work with the file without the need to use sudo.

I hope the information provided can help you with your permission problems. Please let me know if you have questions or comments. I would love to hear from you.

Leave a Reply

Your email address will not be published. Required fields are marked *