How to change file permissions and ownership settings while in SSH

In this tutorial, we’ll show you how you can edit file permissions and adjust file ownership settings while in SSH. This is not the only way to change permissions settings however, it can be done within your cPanel’s File Manager as well.

Changing File Permissions

The chmod command is used to change file permissions. The basic syntax is:

chmod ### file/folder

We’ll now show you the command in action. We’ll first log into our account via SSH and use the pwd command to see which folder we’re in. We’ll then run the ls command to see the current files and their permissions, run the chmod command to edit permissions, and then finally use ls again to see that the permissions have been changed.

[email protected] [~/testa]# pwd
/home/user5/testa

[email protected] [~/testa]# ls -alh
total 8.0K
drwxr-xr-x 2 user5 user5 4.0K Dec 7 18:43 ./
drwx–x–x 10 user5 user5 4.0K Dec 6 08:16 ../
-rw-r–r– 1 user5 user5 0 Dec 7 18:43 file1.txt
-rw-r–r– 1 user5 user5 0 Dec 7 18:43 file2.txt
-rw-r–r– 1 user5 user5 0 Dec 7 18:43 file3.txt

[email protected] [~/testa]# chmod 755 file2.txt

[email protected] [~/testa]# ls -alh
total 8.0K
drwxr-xr-x 2 user5 user5 4.0K Dec 7 18:43 ./
drwx–x–x 10 user5 user5 4.0K Dec 6 08:16 ../
-rw-r–r– 1 user5 user5 0 Dec 7 18:43 file1.txt
-rwxr-xr-x 1 user5 user5 0 Dec 7 18:43 file2.txt*
-rw-r–r– 1 user5 user5 0 Dec 7 18:43 file3.txt

As you can see, file2.txt was changed from 644 to 755.

Changing File Ownership Settings

The chown command is used to change file ownership settings. The basic syntax is:

chown user:user file/folder

The act of changing permissions is much more common than changing ownership. One reason is that most of the time you’ll need root access to use chown, and root access is not given by default to accounts.

In the example below, we’ll log into our account via SSH and change the ownership settings from user5:user5 to user5:nobody for the file file2.txt.

[email protected] [/home/user5/testa]# pwd
/home/user5/testa

[email protected] [/home/user5/testa]# ls -alh
total 8.0K
drwxr-xr-x 2 user5 user5 4.0K Dec 7 18:43 ./
drwx–x–x 10 user5 user5 4.0K Dec 6 08:16 ../
-rw-r–r– 1 user5 user5 0 Dec 7 18:43 file1.txt
-rwxr-xr-x 1 user5 user5 0 Dec 7 18:43 file2.txt*
-rw-r–r– 1 user5 user5 0 Dec 7 18:43 file3.txt

[email protected] [/home/user5/testa]# chown user5:nobody file2.txt

[email protected] [/home/user5/testa]# ls -alh
total 8.0K
drwxr-xr-x 2 user5 user5 4.0K Dec 7 18:43 ./
drwx–x–x 10 user5 user5 4.0K Dec 6 08:16 ../
-rw-r–r– 1 user5 user5 0 Dec 7 18:43 file1.txt
-rwxr-xr-x 1 user5 nobody 0 Dec 7 18:43 file2.txt*
-rw-r–r– 1 user5 user5 0 Dec 7 18:43 file3.txt

There are many more ways you can use chown and chmod other than the basic examples we’ve listed above. Usually if you need a way to get something done, there are additional options you can use to get it done. If you need assistance, feel free to post in the comments and we’ll show you how you may be able to do it!

8 thoughts on “How to change file permissions and ownership settings while in SSH

  1. Hello  I have a problem when access my website, it run well in localhost but when I put it into the server it just show the homepage and when I clicks the menu it will appear 404 error requested url not found.

    Some friends advice me that I should change the apache server permission and the others said that the problem is permission and related to chmod. Actually I’m new in this case I dont know what should I do. 

    Do I need a root access to change the permission? I just have an access using my username

  2. The permission of my public_html folder was my_username:nobody. I could not access this folder from s file outside it. So I change the ownership using the command

    # chown my_username:my_username public_html

    and ownership changed, but still I couldn’t access that. Now when I reverting it back to the my_username:nobody using the command

    # chown my_username:nobody public_html

    It gives me the error

    chown: changing ownership of âpublic_htmlâ: Operation not permitted

    What I need to do to revert it back?

    Thanks 🙂

     

    1. Hello Saiid,

      Thank you for contacting us. You may have to modify the permissions of a file above (parent) the file you are editing.

      Since you are having trouble accomplishing this via SSH, you may want to try changing file permissions in cPanel.

      Thank you,
      John-Paul

  3. what we need to do, because our server gets hacked, is: change the ownership of the files residing in public_html, as well as the ownership of public_html. So: the apache server can only read, but not write into the folders or change the files. I have root access and am logged in. Now, if I change the ownership or group to anything else than the user of that said server, then i get a 500 error when opening that php file. Say I create a php file test.php in public_html I change ownership to nobodoy:nobodoy i chmod to 777. I open the testfile, it gives an error. I change the ownership back to myuser:myuser and it works.

  4. Hi,

    I am actually facing problem of changing the permission and ownership of this folder/directory below, i will be so glad if you can advice me on what to do. Sadly for me i have no root access, only my username access is what i have.

    drwx -xr -x 24 root 1005 4.0k Jul 3 11:38 mod

    Thanks

    Samuel

    1. Hello Samuel,

      Sorry for the problem with the permissions. Unfortunately, if you’re trying to change the ownership of the file, you will not be able to do that unless you have root access. You can change the permissions of the file in SSH (with chmod), or through cPanel File manager, but accessing the file. However, if the file ownership is not yours, then you need to submit a ticket to our live technical support department. Explain in your ticket what you’re trying to do and provide verification of your account when you do so.

Was this article helpful? Join the conversation!