- How To Use chmod and chown Command in Linux
- Understanding file permissions for chmod and chown command
- read (r), write (w), and execute (x) permission
- Viewing Linux/Unix file permissions and ownership
- – rw-r–r– file and d rwxr-xr-x directory permission explained
- Displaying file permission using the stat command
- chown command
- changing the owner of folder in linux [closed]
- 1 Answer 1
- How to change permissions for a folder and its subfolders/files in one step
- 20 Answers 20
How To Use chmod and chown Command in Linux
Understanding file permissions for chmod and chown command
One can use file permissions to control access to their files. Sysadmins can enforce a security policy based upon file permissions. All files have three types:
- Owner – Person or process who created the file.
- Group – All users have a primary group, and they own the file, which is useful for sharing files or giving access.
- Others – Users who are not the owner, nor a member of the group. Also, know as world permission.
read (r), write (w), and execute (x) permission
We can set the following permissions on both files and directories:
Permission | File | Directory |
---|---|---|
r | Reading access/view file | Users can read file. In other words, they can run the ls command to list contents of the folder/directory. |
w | Writing access/update/remove file | Users can update, write and delete a file from the directory. |
x | Execution access. Run a file/script as command | Users can execute/run file as command and they have r permission too. |
— | No access. When you want to remove r, w, and x permission | All access is taken away or removed. |
Please note that permission priority decided as follows by the kernel:
User permissions -> Group permissions -> Other permissions
It means user permission overrides group permission and group permissions overrides other permission.
Viewing Linux/Unix file permissions and ownership
Run the ls command:
ls -l
# Show information about a file named file1 #
ls -l file1
ls -l /path/to/file1
# Get information about a directory named dir1 #
ls -ld dir1
ls -l -d /path/to/dir1
For example, we can list permissions for /etc/hosts and /etc/ directory as follows:
ls -l /etc/hosts
Pass the -d option to ls to list directories themselves, not their contents:
From above outputs it is clear that the first character indicate the file type in drwxr-xr-x and -rw-r–r– and the next 9 characters are the actual file permissions.
– rw-r–r– file and d rwxr-xr-x directory permission explained
First character | Description |
---|---|
— | Regular file. |
b | Block special file. |
c | Character special file. |
d | Directory. |
l | Symbolic link. |
p | FIFO. |
s | Socket. |
w | Whiteout. |
Next nine characters are the file permissions divided into three sets/triad of three characters for owner permissions, group permissions, and other/world permissions as follows:
Three permission triads defined what the user/group/others can do | First triad defines what the owner can do | Second triad explains what the group members can do | Third triad defines what other users can do |
---|---|---|---|
— rw- r— r— | Owner has only read and write permission ( rw- ) | Group has read permission ( r— ) | Others has read permission ( r— ) |
d rwx r-x r-x | Owner has full permission ( rwx ) | Group has read and execute permission ( r-x ) | Others has read and execute permission ( r-x ) |
Displaying file permission using the stat command
Run the following command:
stat file1
stat dir1
stat /etc/passwd
stat /etc/resolv.conf
GUI displaying file permissions:
chown command
The chown command changes the user and/or group ownership of for given file. The syntax is:
Источник
changing the owner of folder in linux [closed]
Want to improve this question? Update the question so it’s on-topic for Stack Overflow.
Closed 7 years ago .
I have a folder in my subdomain which is created through WHM so the owner of that subdomain is not the owner of main domain.
I want to change the owner of one of the folders of subdomain to domain owner. I tried this, but when I check with winscp it shows owner as 500.
I’ve tried to change from winscp also, but there is no option for winscp, so I’ve logged in as root using putty and ran the command from above, but it doesn’t help and I am unable to upload any file to subdomain from the main domain, as it returns error «permission denied».
I want to give the ownership of rohan to sujit to have rights to upload file from sujit domain to subdomain rohan
Update:
Now it is changing owner to 500
1 Answer 1
Use chown to change ownership and chmod to change rights.
use the -R option to apply the rights for all files inside of a directory too.
Note that both these commands just work for directories too. The -R option makes them also change the permissions for all files and directories inside of the directory.
will change ownership (both user and group) of all files and directories inside of directory and directory itself.
will only change the permission of the folder directory but will leave the files and folders inside the directory alone.
you need to use sudo to change the ownership from root to yourself.
Note that if you use chown user: file (Note the left-out group), it will use the default group for that user.
Also You can change the group ownership of a file or directory with the command:
You must be a member of the group to which you are changing ownership to.
You can find group of file as follows
User 500 is just a normal user. Typically user 500 was the first user on the system, recent changes (to /etc/login.defs) has altered the minimum user id to 1000 in many distributions, so typically 1000 is now the first (non root) user.
What you may be seeing is a system which has been upgraded from the old state to the new state and still has some processes knocking about on uid 500. You can likely change it by first checking if your distro should indeed now use 1000, and if so alter the login.defs file yourself, the renumber the user account in /etc/passwd and chown/chgrp all their files, usually in /home/, then reboot.
But in answer to your question, no, you should not really be worried about this in all likelihood. It’ll be showing as «500» instead of a username because o user in /etc/passwd has a uid set of 500, that’s all.
Also you can show your current numbers using id i’m willing to bet it comes back as 1000 for you.
Источник
How to change permissions for a folder and its subfolders/files in one step
I would like to change the permissions of a folder and all its subfolders and files in one step (command) in Linux.
I have already tried the below command but it works only for the mentioned folder:
Is there a way to set chmod 755 for /opt/lampp/htdocs and all of its content including subfolders and files?
Also, in the future, if I create a new folder or file inside htdocs , how can the permissions of that automatically be set to 755 ?
I had a look at this Stack Overflow question too:
20 Answers 20
The other answers are correct, in that chmod -R 755 will set these permissions to all files and subfolders in the tree. But why on earth would you want to? It might make sense for the directories, but why set the execute bit on all the files?
I suspect what you really want to do is set the directories to 755 and either leave the files alone or set them to 644. For this, you can use the find command. For example:
To change all the directories to 755 ( drwxr-xr-x ):
To change all the files to 644 ( -rw-r—r— ):
Some splainin’: (thanks @tobbez)
- chmod 755 <> specifies the command that will be executed by find for each directory
- chmod 644 <> specifies the command that will be executed by find for each file
- <> is replaced by the path
- ; the semicolon tells find that this is the end of the command it’s supposed to execute
- \; the semicolon is escaped, otherwise it would be interpreted by the shell instead of find
Check the -R option
In the future, you can save a lot of time by checking the man page first:
So in this case:
If you want to set permissions on all files to a+r , and all directories to a+x , and do that recursively through the complete subdirectory tree, use:
The X (that is capital X , not small x !) is ignored for files (unless they are executable for someone already) but is used for directories.
You can use -R with chmod for recursive traversal of all files and subfolders.
You might need sudo as it depends on LAMP being installed by the current user or another one:
The correct recursive command is:
-R : change every sub folder including the current folder
To set to all subfolders (recursively) use -R
And use umask to set the default to new folders/files cd /folder umask 755
→ umask -S u=rwx,g=rx,o=rx λ
chmod 755 -R /opt/lampp/htdocs will recursively set the permissions. There’s no way to set the permissions for files automatically in only this directory that are created after you set the permissions, but you could change your system-wide default file permissions with by setting umask 022 .
You might want to consider this answer given by nik on Super User and use «one chmod» for all files/folders like this:
Here’s another way to set directories to 775 and files to 664.
It may look long, but it’s pretty cool for three reasons:
- Scans through the file system only once rather than twice.
- Provides better control over how files are handled vs. how directories are handled. This is useful when working with special modes such as the sticky bit, which you probably want to apply to directories but not files.
- Uses a technique straight out of the man pages (see below).
Note that I have not confirmed the performance difference (if any) between this solution and that of simply using two find commands (as in Peter Mortensen’s solution). However, seeing a similar example in the manual is encouraging.
Example from man find page:
However, be careful with that. It can really hurt you if you change the permissions of the wrong files/folders.
chmod -R 755 directory_name works, but how would you keep new files to 755 also? The file’s permissions becomes the default permission.
For Mac OS X 10.7 (Lion), it is:
And yes, as all other say, be careful when doing this.
You want to make sure that appropriate files and directories are chmod-ed/permissions for those are appropriate. For all directories you want
And for all the images, JavaScript, CSS, HTML. well, you shouldn’t execute them. So use
But for all the logic code (for instance PHP code), you should set permissions such that the user can’t see that code:
For anyone still struggling with permission issues, navigate up one directory level cd .. from the root directory of your project, add yourself (user) to the directory and give permission to edit everything inside (tested on macOS).
To do that you would run this command (preferred):
Note: for currently unsaved changes, one might need to restart the code editor first to be able to save without being asked for a password.
Also, please remember you can press Tab to see the options while typing the username and folder to make it easier for yourself.
but as mentioned above, you need to be careful with the second method.
I think Adam was asking how to change the umask value for all processes that are trying to operate on the /opt/lampp/htdocs directory.
The user file-creation mode mask (umask) is used to determine the file permissions for newly created files. It can be used to control the default file permissions for new files.
so if you will use some kind of FTP program to upload files into /opt/lampp/htdocs you need to configure your FTP server to use the umask you want.
If files / directories need be created, for example, by PHP, you need to modify the PHP code:
If you will create new files / folders from your Bash session, you can set umask value in your shell profile
Or you can set up a umask in /etc/bashrc or /etc/profile file for all users.
Add the following to the file:
And to change permissions for already created files, you can use find.
There are two answers of finding files and applying chmod to them.
The first one is find the file and apply chmod as it finds (as suggested by @WombleGoneBad).
The second solution is to generate a list of all files with the find command and supply this list to the chmod command (as suggested by @lamgesh).
Both of these versions work nice as long as the number of files returned by the find command is small. The second solution looks great to the eye and more readable than the first one. If there are a large number of files, the second solution returns error: Argument list too long.
So my suggestion is
- Use chmod -R 755 /opt/lampp/htdocs if you want to change permissions of all files and directories at once.
- Use find /opt/lampp/htdocs -type d -exec chmod 755 <> \; if the number of files you are using is very large. The -type x option searches for specific type of file only, where d is used for finding directory, f for file and l for link.
- Use chmod 755 $(find /path/to/base/dir -type d) otherwise
- Better to use the first one in any situation
Источник