Change the ownership in linux

How to change the file owner and group in Linux?

To change the file owner and group, we use the chown command in the Linux operating system.

We know that Linux is a multiuser operating system so every file or directory belongs to an owner and group.

To change ownership of files or directories we use chown command in the Linux system. This command is also available in the IBM i operating system. The chgrp command is also used to change only the group ownership of the file in the Linux system.

Syntax

The general syntax of the chown command is as follows

A brief description of options available in the chown command —

Sr.No. option & Description
1 -c, —changes
Gives a diagnosis for all the files that actually changed.
2. -f, —silent, —quite
It suppresses most of the error messages.
3 -v, —verbose
Give a diagnosis for all the processed files.
4 -R, —recursive
It changes files and directories recursively.
5 —help
Displays a help message and then exits.
6 —version
It gives info about the version and then exits.

Checking the ownership

To check the ownership of a file in the Linux system, we use the ls -l command as shown below.

Changing the ownership of a file

To change ownership of a file in the Linux system we need to administrative permission or sudo privilege.

The general syntax for changing ownership of a file is as follows:

Here, we will change the ownership of a file ‘file.txt’ Vikash to Gautam using the chown command.

First, we will check the ownership of ‘file.txt’ using the below command.

We can see that the owner of ‘file.txt’ is vikash and the group ownership of the ‘file.txt’ is vikash. To change the ownership, we will execute command as shown below.

After changing the ownership of the file, we will again check again the ownership of files to ensure that ownership is changed or not.

To check more information and available options in the chown command, we use the —help option with the chown command as follows:

Conclusion: In this article, we learned to change the ownership of files using the chown command in the Linux operating system with available options and suitable examples. To change only the group ownership of the file, we use the chgrp command in the Linux system.

Источник

Chown Command in Linux (Change File Ownership)

In Linux and Unix-like operating systems, all files, directories and processes (which are again files) are owned by users. The group is a set of users that share the same access permissions (i.e read, write and execute) for that objects (files/directories).

The chown command is used in Linux to change the user and group ownership of files, directories and symbolic links.

In this tutorial, I will show you how to use the chown command with some practical examples.

Chown syntax

Let check the basic syntax of chown command as follows:

You can run ls -l command to print file ownership details.

In the following output, you can see the file ‘myfile.txt’ is owned by user ‘tom’ and the group is owned by ‘developers’:

Change Ownership of a file

To change the ownership of a file, use chown with new owner name and the file name for which the owner has to be changed.

The following command change ownership of the file named ‘myfile.txt’ to a new user ‘tom’:

If the command successfully executed it won’t display any output on the terminal. Let verify using ls -l command as follows:

Changing the owner and group of a file

If the owner is followed by a colon ( : ) and a group name (without spaces), the group name is changed as well.

The following example shows how to change owner and group for the file named ‘myfile.txt’:

Now the new owner of the file is ‘tom’ and the new group owner is ‘developers’ group.

If you remove the group name after the colon the group of the file is changed to the specified user’s login group:

If the colon (:) and group are mentioned, only the group of the file is changed. In this case, the command works like chgrp command.

Change ownership for directories recursively

To apply ownership recursively on all files and directories under a given directory use -R option

In the following example, the ownership of all the files and directories under ‘/var/www/html’ will be recursively changed to ‘linoxide’ and group ownership to ‘www-data’:

Verbose output

The —verbose option shows all the ownership changing on the terminal. It outputs the diagnostics for each file processed.

The verbose option outputs the processing of each file even when the changes are not made. But with -c or —changes option, the output is reported only when changes are made.

By default chown command won’t change ownership of symbolic links rather it changes ownership on the target file. Chmod has an option -h that can be used to change ownership for symbolic links.

Check the following example:

Change ownership same as reference file

You can reference a file from which chown copy the same user and group ownership to a new file.

In following command —reference=myfile1.txt option to provide reference file and ownership assigned to file named ‘myfile2.txt’:

Silent operation

A normal user cannot change the ownership of files owned by others. So an error is displayed when a normal user tries to change the ownership.

But if we use -f or —silent or —quiet option, the error is not displayed.

How to preserve the root

Chmod command has an option —preserve-root to prevent chmod from acting recursively on / . This option should be used with -R option to take effect.

Only the permissions of the / will be changed and ownership of files and directories inside / will remain the same.

Conclusion

In this tutorial, we learned how to change ownership of files and directories in Linux. Thanks for reading and let us know your comments.

Источник

Changing File Ownership and Permissions in Linux

As Linux was designed to support many users on a system, permissions and ownership are in place to ensure authorized access to certain files. This prevents general users from modifying system and administration level files, accessing other users’ private files, or allowing some users to read a file. Still, only one or few have access to write to it. Two of these systems are Linux’s file ownership and permissions policies.

Every file in Linux (including directories) has an owning user and group and read/write/execute flags to allow or deny such types of file access to the owner, owning group, and all other users, respectively.

Finding Current File Ownership and Permissions

To list a file’s current ownership and permissions policies, the commandВ ls -lВ can be used.

Supplying a file to the command withВ ls -l filenameВ will output details about the file. These details include an indicator of the type of file it is, the read (r)/write (w)/execute (x) flags for the user, group, and other users, the number of links to the file, the size of the file, and the date the file was last modified.

In this example, the file is owned by the user fooВ and the group bar. The user fooВ has read, write, and execute permissions, the group barВ has read and write permissions, andВ any other users only have read access.

Simply runningВ ls -lВ without supplying a filename will list the same output for all current directory contents.

If the file provided is actually a directory, the command will list the same output for all contents of that directory.

More information on the ls command can be found here, or with the commands ls –help or man ls.

Changing File Ownership and Permissions

Because onlyВ the root user can change the ownership and permissions of a file in Linux, all of the following commands must be run as root or withВ sudoВ if logged in as any other user withВ sudo command permissions.

Changing Ownership

In Linux, when a file is created, ownership over the file defaults to the user who created it and that user’s primary group. Sometimes though. There are instances where the ownership of a file or directory must be changed. To do so,В there are two useful commands in changing user or group ownership of a file:В chownВ andВ chgrp.

The command chown is used to modify the ownership of a file. The command chgrp is used to modify the group ownership of a file. However, because chown also has the functionality to modify group ownership, we will only use chown in this guide. For more details and instructions for using chgrp, you can look here or use the commands chgrp –help or man chgrp.

To useВ chownВ to change file ownership, supply the user’s name you want to transfer ownership to, followed by which file you wish to transfer:

Instead of a username, to change the group ownership, enter a:В followed by the group name:

To change both the user and group ownership simultaneously, enter both the username and group name, with a:В separating them:

You may also need to supply the command with one of its available argument options, depending on what is being changed.

For example, if changing the ownership of an entire directory, theВ -RВ option should also be supplied to run the command recursively, changing the ownership of the directory itself and all of its contents:

It is highly suggested to utilize the full path of the file or folder when using this flag and have a solid understanding of absolute and relative paths. This could harm your file system’s ownership.

Without this option, only the owner of the directory itself would be updated, while the ownership of its contents would not.

More details, and a full list of the available options, forВ chownВ can be found here, or with the commands

Changing Permissions

In Linux, the access permissions for a file are split between theВ user, group, and others. The user is the file owner, while the group is the owning group of the file, and others are simply all other users.В It is then further split into what’s basically a simple yes/no for each type of access is available: read, write, and execute. Read (r) means they can read data from the file, write (w) means they can write data to the file, and execute (x) means they can run the file as a program.

To change these permissions, the commandВ chmodВ is available, with which there are two primary ways to adjust the permissions.

The first way is to enable or disable specific permissions for specific roles. To do this, you would run chmod and follow it with either u for adjusting user permissions, g for group permissions, or o for other users, then either a + or – to indicate either adding or remove permissions, and finally either a r for reading, w for write, or x for execution permissions. You can also combine the options for who to change permissions for and which permissions.

In the example above, the first command gives execution permissions to the user that owns the file. The second command gives read and writes permissions for both the user and group that own the file. And finally, the last example takes away read permissions for the file from all other users.

The second way to useВ chmodВ to change file permissions is to set all permissions at once using a number to represent all permissions. This number is a 3-digit number where the first digit represents the permissions for the user, the second digit represents the group permissions, and the last digit represents the permissions for other users.

The value for each digit is the sum of the numbers representing which permissions to enable for that role. The numbers representing each type of permission is as follows:

Read = 4

Write = 2

Execute = 1

This means that the number representing no permissions would be zero. Four would be just read. Six would be read and write. Seven would be all permissions, etc.

We then concatenate these numbers into our 3-digit number to represent all roles at once. For example, to give the user read and write access, the group only read access, and other users no access, the number to represent that would beВ 740.

In the example above, the first command sets all roles to have no permissions. The second command gives all roles all permissions. The third gives read and write access to only the user, and the last command gives read and executes permissions to both the user and other users.

More details and options available forВ chmodВ can be found here or with the commands

Written byВ Hostwinds TeamВ / В August 29, 2018

Источник

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.

Источник

Читайте также:  Создать сетевой мост linux
Оцените статью