Linux change folder owner

How to Change Permissions and Owners via Command Line

In this tutorial, you will learn how to change permissions and owners in Linux using chmod and chown commands. By doing so, you’ll have better management in team-based projects.

Why You Need to Change Permissions and Owners in Linux

Linux is a multi-user operating system, so more than one person can work on the same computer at the same time. What’s great, the system can be accessed locally or remotely. That’s why developers often use this OS for group projects.

In such a large environment, we need to set file permissions and ownership, so only specific users can access our data. This way, we can protect sensitive information and prevent unwanted changes from happening.

Fortunately, thanks to chmod and chown commands, it is easy to change permissions and owners in Linux. But before we begin to learn how to use them, make sure you have access to the command line. You can launch it by pressing Ctrl + Alt + T.

How to Change File and Folder Permissions

We will be using the chmod command to change file and folder permissions in Linux. But first, you need to be aware that there are three types of users who can interact with a file:

  • Owner — the user who creates and owns a file or folder.
  • Group — all users who are members of the same group.
  • Others — all other users on the system who are neither the owner nor members of a group.

To see permissions and owners of a specific file, you can run this command:

The result will look like this:

Let’s break the output down to see what each field means:

  • “-rwxrw-rw-“ — this part of the line represents the file permissions. To understand it better, we have to divide it into four groups: (), (rwx), (rw-), and (rw-).
    • The first group indicates the file type. Our example shows a hyphen, which represents a regular file. If we are inspecting a directory, the hyphen will be replaced by d.
    • The three characters after the file type represent the owner’s file permissions. In this example, we can see that the owner can read (r), write (w), and execute (x) the file.
    • The next three characters are the group’s file permissions. We can conclude that the group can read (r) and write (w), but cannot execute the file. This is because the last character is a hyphen instead of the letter x.
    • The last group is others’ file permissions. Based on our example, this type of user cannot execute the file, but they are allowed to read and write.
  • 1 – the number of hard links. A hard link is an additional name for an existing file.
  • user user – the owner and group owner of the file.
  • 0 – the size of the file in bytes.
  • Jan 19 12:59 – the last modification date.
  • myfile.txt – the name of the file/folder.
Читайте также:  Перезагружается панель задач windows 10

How to Use chmod Command

Let’s say we want to change Linux file permissions from -rwxrw-rw- to -rwx-r–r–. Simply enter this line:

By executing this command, the owner can read, write, and execute the file (rwx). However, group and others are only allowed to read (r–).

At this point, you might wonder why we are using a three-digit number (744) after the chmod command.

The number determines the file permissions. Read, write, and execute are represented by a numerical value:

  • r (read) – 4
  • w (write) – 2
  • x (execute) – 1

So if you want to give all permissions (rwx) to a user, we need to add read (4), write (2), and execute (1). Therefore, rwx is equal to 7.

Meanwhile, since group and others are only allowed to read the file, we give them 4.

Remember, the owner’s permissions always come first, then followed by group and others. That’s why we enter 744.

If you don’t want to give any permission to a user, enter 0 into the corresponding spot.

Here is a list of the most common file permissions:

Common permissions for directories:

Changing the Owners of Files and Folders

To change the owner of a file and folder, we will be using the chown command. This is the basic syntax:

Let’s say we have a file named “myfile.txt.” If we want to set the owner of the file to “hostinger,” we can use this command:

However, if we want to change the group owner of the file to “clients,” we’ll enter this line instead:

Notice that we use a colon (:) before “clients” to indicate that it is a group owner.

Now, to change both the owner and group owner at the same time, the syntax would be like this:

The main rule is that the owner should come before the group owner, and they have to be separated by a colon.

Using Options with chmod and chown Commands

Option is an additional command to change the output of a command.

One of the most popular options that you can combine with chmod and chown is -R (Recursive). This Linux option allows you to change permissions or owners of all files and subdirectories inside a specific directory.

If you want to use an option, you have to place it right after the chmod/chown command.

Take a look at this example:

After you enter the above command, the owner can read, write, and execute all files and subdirectories inside the /etc/myfiles directory. The command also gives read and execute permissions to group and others.

Be extra careful with this option. Improper use of the command may cause critical failure, and it requires a great deal of work to reverse the changes.

Aside from -R, the following options are often used with chmod and chown commands:

  • -f or force. The command line will ignore any errors and apply the chmod and chown commands.
  • -v (verbose) option gives you diagnostics of all files that are processed by the command.
  • -c (changes) is similar to the -v option. However, it will only provide information when changes were successfully made.

Conclusion

In this tutorial, you have learned how to use chmod and chown commands to change permissions and owners in Linux. We also provided the basic syntax and several useful options that you can combine with either of these commands.

To learn more about Linux command line, you can read our article on basic bash commands.

Читайте также:  Linux mageia системные требования

If you have any questions, feel free to comment below!

Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.

Источник

Ubuntu Documentation

Understanding and Using File Permissions

In Linux and Unix, everything is a file. Directories are files, files are files and devices are files. Devices are usually referred to as a node; however, they are still files. All of the files on a system have permissions that allow or prevent others from viewing, modifying or executing. If the file is of type Directory then it restricts different actions than files and device nodes. The super user «root» has the ability to access any file on the system. Each file has access restrictions with permissions, user restrictions with owner/group association. Permissions are referred to as bits.

To change or edit files that are owned by root, sudo must be used — please see RootSudo for details.

If the owner read & execute bit are on, then the permissions are:

There are three types of access restrictions:

Permission

Action

chmod option

There are also three types of user restrictions:

User

ls output

Note: The restriction type scope is not inheritable: the file owner will be unaffected by restrictions set for his group or everybody else.

Folder/Directory Permissions

Directories have directory permissions. The directory permissions restrict different actions than with files or device nodes.

Permission

Action

chmod option

(view contents, i.e. ls command)

(create or remove files from dir)

(cd into directory)

read restricts or allows viewing the directories contents, i.e. ls command

write restricts or allows creating new files or deleting files in the directory. (Caution: write access for a directory allows deleting of files in the directory even if the user does not have write permissions for the file!)

execute restricts or allows changing into the directory, i.e. cd command

» height=»16″ src=»/moin_static198/light/img/icon_cool.png» title=»Info » width=»16″/> Folders (directories) must have ‘execute’ permissions set (x or 1), or folders (directories) will NOT FUNCTION as folders (directories) and WILL DISAPPEAR from view in the file browser (Nautilus).

Permissions in Action

Using the example above we have the file «/etc/hosts» which is owned by the user root and belongs to the root group.

What are the permissions from the above /etc/hosts ls output?

Changing Permissions

The command to use when modifying permissions is chmod. There are two ways to modify permissions, with numbers or with letters. Using letters is easier to understand for most people. When modifying permissions be careful not to create security problems. Some files are configured to have very restrictive permissions to prevent unauthorized access. For example, the /etc/shadow file (file that stores all local user passwords) does not have permissions for regular users to read or otherwise access.

Источник

How to Change File/Directory User and Group Ownership in Linux

Linux is a multi-user system that can be accessed simultaneously by multiple users.

File permissions are a way to restrict users to access other user’s files on Linux.

It is controlled by file ownership and file permissions.

Each file in Linux has 3 types of owners, it’s user, group, and others.

A user is the owner of the file, and group is the one to which the user belongs to.

Each file and directory has three permissions for all three owners, such as read, write, and execute.

This can be done using the chown command and the chgrp command.

By default these commands do not display any output, so use the “-v” option to get information about what’s being done.

Читайте также:  Creative webcam nx ultra драйвер windows 10

Use the ls command to find out who owns a file and which group the file belongs to.

What’s chown Command

The chown command is used to change the user and/or group ownership of each given file. This allows the user to perform various actions and the details are described below.

The common syntax is as follows:

  • USER – If you provide only a username, the group of files will not be changed, becoming the owner of a given user file.
  • USER: – If you provide a username followed by a colon, the given user will become the owner of the files and the group of files will be converted into that user login group.
  • USER:GROUP – If you provide a username followed by a colon and group name, the given user will become the owner of the files and the group of files.
  • :GROUP – If you provide a group name after the colon, and owner is excluded, only the group of files will be transferred. It works the same way as chgrp.
  • : – If you only provide a colon, no action is performed.

Alternatively, you can use “-c” switch instead of “-v” switch, it works like verbose but report only when a change is made.

1) How to Change the Owner of a File/Directory on Linux Using the chown Command

In this example, we are going to change the ownership of the “passwd-up.sh” file from “daygeek” to “root”.
Before

2) How to Change the Group Ownership of a File/Directory on Linux Using the chown Command

In this example, we are going to change the group ownership of the file “mysql_backup.sh” from “daygeek” to “root”.

3) How to Change the User and Group Ownership of a File/Directory on Linux Using the chown Command

Use the example below to change the file’s user and group ownership. In this example, the user and group ownership of the “test.txt” file will be changed from “daygeek” to “root”.

4) How to Change the User and Group Ownership Recursively on Linux Using the chown Command

In this example, the ownership of all the files and its sub-directories is changed from “daygeek” to “root”, which includes the parent directory rights.

5) How to Change User and Group Ownership of Multiple Files on Linux Using the chown Command

To change the user and group ownership of multiple files, use the example below.

6) How to Change the User and Group Ownership of Link Files on Linux Using the chown Command

Use the following examples for changing ownership of link files.

When you use the chown command as usual in link files, it only changes the ownership of the source file, not the link file itself. In this example, it changes the ownership of the “output.txt” file from “daygeek” to “root”.

Use the “-h” option with the chown command to change the ownership of the link file instead of the source file.

Note: “-L” and “-H” options can be used when recursively changing the symbolic link directory ownership.

7) How do i Change the User and Group Ownership of a File Using the “–from=” option Using the chown Command?

Alternatively, you can use the “–from=” option to change ownership of files.

8) How to Copy User and Group Ownership from One File to Another on Linux Using the chown Command

In some cases, if you want to copy ownership from one file to another, use the example below.
In this example, the owner permission is copied from the “2.txt” file to the “renu.txt” file.

9) Bonus Tips: How To Find And Change Ownership of Files Based On Specific Extension

In this example, the ownership of the “.sh” extension files will be changed to “daygeek”.

Here are some helpful find command tutorials for you.

Источник

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