Changing owner directory linux

How do I change the owner of a directory in Linux?

Here is the list of few scenarios in which you want to do this:

  • For example, you want an already created directory or file to be accessible to a particular user.
  • When a user leaves an organization, all of his data comes under the responsibility of another employee. Therefore, you want to change the ownership of the new team member.
  • Changing directory or file ownership is also necessary when you are writing a script that has to be used by only a specific person.
  • Transferring files between different Linux systems also requires changes in files and directories ownership.

Now let’s check out the syntax of the chown command.

chown command syntax

Utilize the “User” for the username or replace it using the user ID, also known as UID. Add the group name in the “Group” part of the GID (group ID). At the end of the chown command, add files or directories for which you want to change the ownership.

Changing the owner of a directory

We have created a separate user named “utest” and a group “ugroup” for demonstrating the examples. To change the ownership of any directory, utilize the chown command with the username and path of the directory.

Now, write out the “ls” command and check if your directory ownership is updated or not.

Changing the ownership of all sub-directories

Utilize the “-R” option of the chown command for changing the owners of all the files or folders present in a directory.

List out the directory content to view the results.

Changing the owner of a file

First of all, list out the file content to know about its ownership.

After that, specify the name of the user who you want to be the new owner of this file. In our case, we have chosen “utest” to avail the ownership of “samplefile.txt.”

Again, verify the updated ownership changes.

You can also use the “User ID” or “UID” of any user for this purpose. For that, retrieve the user ID of the new user by utilizing the “id” command with the “-u” option.

Add the User ID instead of the username to make the ownership changes.

Changing the owners of multiple files

Specify the file names at the end of the chown command to change the ownership of multiple files at once.

Write out the below-given command to confirm the changes.

Changing the owner of directory and file at once

Follow the below-given method for changing the owners of the file and directory instantly.

This command will make “utest” the new owner of the “test directory” and the “samplefile.txt.”

Changing the owner using wildcards

Wildcards are used to select a specific file group according to the given pattern. The chown command will then change the owner of the files after retrieving them from the wildcard execution.

Conclusion

For data security concerns, you may want to specify the ownership of files and directories. In Linux, we use the chown command-line utility for changing the ownership of directories or files. You can also utilize this command for changing directories and multiple file ownership at once. In this post, all of these statements are justified by providing practical examples.

Читайте также:  Formatting time in linux

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.

Источник

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:

  1. Owner – Person or process who created the file.
  2. Group – All users have a primary group, and they own the file, which is useful for sharing files or giving access.
  3. 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.

Читайте также:  Служба регистрации ошибок windows отключена

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.

Источник

Chown Command: Change Owner of File in Linux

Home » SysAdmin » Chown Command: Change Owner of File in Linux

The chown command changes user ownership of a file, directory, or link in Linux. Every file is associated with an owning user or group. It is critical to configure file and folder permissions properly.

In this tutorial, learn how to use the Linux chown command with examples provided.

  • Linux or UNIX-like system
  • Access to a terminal/command line
  • A user with sudo privileges to change the ownership. Remember to run the commands with sudo to execute them properly.

Linux Chown Command Syntax

The basic chown command syntax consists of a few segments. The help file shows the following format:

  • [OPTIONS] – the command can be used with or without additional options.
  • [USER] – the username or the numeric user ID of the new owner of a file.
  • [:] – use the colon when changing a group of a file.
  • [GROUP] – changing the group ownership of a file is optional.
  • FILE – the target file.

Superuser permissions are necessary to execute the chown command.

In this guide, we tested the command examples with the chown version 8.28 in Ubuntu 18.04.2 LTS.

To check the chown version on your machine, enter:

The output will look similar to this:

How to Check Ownership of a File in Linux

First, you need to know the original file owner or group before making ownership changes using the chown command.

To check the group or ownership of Linux files and directories in the current location, run the following command:

An example output of the ls command looks like this:

How to Change the Owner of a File

Changing the owner of a file with chown requires you to specify the new owner and the file. The format of the command is:

The following command changes the ownership of a file sample from root to the user test:

Use the same format to change the ownership for both files and directories.

Change the Owner of a File With UID

Instead of a username, you can specify a user ID to change the ownership of a file.

Make sure there is no user with the same name as the numeric UID. If there is, the chown command gives priority to the username, not the UID.

Note: To check a user’s ID, run id -u USERNAME from the terminal.

Change Ownership of Multiple Linux Files

List the target file names after the new user to change the ownership for multiple files. Use single spaces between the file names.

In the following example, root will be the new owner of files sample2 and sample3.

Combine file names and directory names to change their ownership with one command. For example:

Do not forget that the commands are case sensitive.

Читайте также:  Как управлять телефоном windows через компьютер

How to Change the Group of a File

With chown, you can change a group for a file or directory without changing the owning user. The result is the same as using the chgrp command.

Run the chown command using the colon and a group name:

The following example changes the group of the file sample3 from grouptest to group3.

List multiple names of files or directories to make bulk changes.

Change the Group of a File Using GID

Similar to UID, use a group ID (GID) instead of a group name to change the group of a file.

Change Owner and the Group

To assign a new owner of a file and change its group at the same time, run the chown command in this format:

Therefore, to set linuxuser as the new owner and group2 as the new group of the file sample2:

Remember that there are no spaces before or after the colon.

Change Group to a Users Login Group

The chown command assigns the owner’s login group to the file when no group is specified.

To do so, define a new user followed by a colon, space, and the target file:

The following example changes the group ownership to the login group of linuxuser:

Transfer Ownership and Group Settings from One File to Another

Rather than changing the ownership to a specific user, you can use the owner and a group of a reference file.

Add the —reference option to the chown command to copy the settings from one file to another:

Remember to type in the names of the files correctly to avoid the error message:

Check Owner and Group Before Making Changes

The chown command —from option lets you verify the current owner and group and then apply changes.

The chown syntax for checking both the user and group looks like this:

The example below shows we first verified the ownership and the group of the file sample3:

Then chown changed the owner to linuxuser and the group to group3.

Check Owner Only

The option —from can be used to validate only the current user of a file.

Check Group Only

Similar to the previous section, you can validate only the group of a file using the option —from .

Here is an example where we verified the current group before changing it:

Remember to use the colon for both group names to avoid error messages.

How to Recursively Change File Ownership

The chown command allows changing the ownership of all files and subdirectories within a specified directory. Add the -R option to the command to do so:

In the following example, we will recursively change the owner and the group for all files and directories in Dir1.

To change the owner of a symbolic link, use the -h option. Otherwise, the ownership of the linked file will be changed.

The following image shows how symbolic links behave when -h is omitted.

The owner and group of the symbolic link remain intact. Instead, the owner and the group of the file textfile changed.

To push the changes to the link, run the chown command with the -h flag:

In the following example, we changed the owner and group of a symbolic link.

Display Chown Command Process Details

By default, the terminal does not display the chown process information. To see what happens under the hood, use one of the two command line flags:

  • The option –v produces the process details even when the ownership stays the same.
  • The option –c displays the output information only when an owner or group of the target file changes.

For example, if we specify the current owner as a new owner of the file:

The terminal produces the following output:

Switch from -v to -c and there will be no messages in this case. This happens because there are no owner or group changes.

The information is particularly useful with the recursive chown command:

In this example, the output lists all objects affected after running the command.

Suppress Chown Command Errors

To avoid seeing potential error messages when running the chown command, use the -f option:

The example below shows the error message for a non-existent file or directory:

Adding the -f flag suppresses most error messages. However, if you specify an invalid username, the error message appears:

Now you know how to use chown command in Linux to change a file’s user and/or group ownership.

Take extra caution when changing the group or ownership of a file or directories.

Источник

Оцените статью