How to change groups in linux

How to change my current unix group

I belong to several user groups (say coder, projectmanager, sudo), for some reason I may not be in a particular group. This happens when I am in a particular directory that belong to a particular group where I need the write access to create new files. It is quite annoying that I am in a different group. When I run the groups command, I am listed as in groupA, how do I become groupB? When I do a search, the top link goes to how to create groups.

2 Answers 2

If you’re an admin, just do it as root or as a user with the appropriate permissions.

If you’re trying to avoid that, you can add yourself to the group. Most systems will let you do that with a simple command:

sudo usermod -a -G newgroup username 

Older systems require editing /etc/group . First try sudo vigr («vi group,» much like vipw for the passwd file) and if that isn’t present, you may edit it directly via sudo vi /etc/group .

Find the relevant group and append yourself to it. Users are in the last colon-delimited section and are comma-delimited within that section. For example, you can add yourself to the «cdrom» and «games» groups:

root:x:0: users:x:100:kemin cdrom:x:24: games:x:60:adam 
root:x:0: users:x:100:kemin cdrom:x:24:kemin games:x:60:adam,kemin 

A very quick programmatic approach —which lacks safeties— could look like:

sudo sed -i '/^games:/s/$/,kemin/; s/:,/:/g' /etc/group 

I only added one safety above, which accounts for an empty group (no comma needed). This does not check to ensure you’re initially absent from the group, and I didn’t use a regex alternation ( /^\(games\|cdrom\):/… ) because non-GNU sed doesn’t support it.

(This may not work on systems that lock /etc/group to vigr. The fact that it’s hard to get all the safeties correct is why such locking is justified.)

This will not immediately work! You need to log out and log back in, or else sudo as yourself (which is a good way to test without the hassle of logging out), e.g. sudo -u $USERNAME groups , or get a new login through a virtual terminal ( Alt + F2 ) or something like ssh localhost .

Источник

Chown Command in Linux (File Ownership)

The chown command allows you to change the user and/or group ownership of a given file, directory, or symbolic link.

In Linux, all files are associated with an owner and a group and assigned with permission access rights for the file owner, the group members, and others.

In this tutorial, we will show you how to use the chown command through practical examples.

Читайте также:  Kali linux android apk

How to Use chown #

Before going into how to use the chown command, let’s start by reviewing the basic syntax.

The chown command expressions takes the following form:

chown [OPTIONS] USER[:GROUP] FILE(s) 

USER is the user name or the user ID (UID) of the new owner. GROUP is the name of the new group or the group ID (GID). FILE(s) is the name of one or more files, directories or links. Numeric IDs should be prefixed with the + symbol.

  • USER — If only the user is specified, the specified user will become the owner of the given files, the group ownership is not changed.
  • USER: — When the username is followed by a colon : , and the group name is not given, the user will become the owner of the files, and the files group ownership is changed to user’s login group.
  • USER:GROUP — If both the user and the group are specified (with no space betwen them), the user ownership of the files is changed to the given user and the group ownership is changed to the given group.
  • :GROUP — If the User is omitted and the group is prefixed with a colon : , only the group ownership of the files is changed to the given group.
  • : If only a colon : is given, without specifying the user and the group, no change is made.

By default, on success, chown doesn’t produce any output and returns zero.

Use the ls -l command to find out who owns a file or what group the file belongs to:

-rw-r--r-- 12 linuxize users 12.0K Apr 8 20:51 filename.txt |[-][-][-]- [------] [---] | | | +-----------> Group +-------------------> Owner 

Normal users can change the group of the file only if they own the file and only to a group of which they are a member. Administrative users can change the group ownership of all files.

How to Change the Owner of a File #

To change the owner of a file use the chown command followed by the user name of the new owner and the target file as an argument:

For example, the following command will change the ownership of a file named file1 to a new owner named linuxize :

To change the ownership of multiple files or directories, specify them as a space-separated list. The command below changes the ownership of a file named file1 and directory dir1 to a new owner named linuxize :

chown linuxize file1 dir1

The numeric user ID (UID) can be used instead of the username. The following example will change the ownership of a file named file2 to a new owner with UID of 1000 :

If a numeric owner exists as a user name, then the ownership will be transferred to the user name. To avoid this prefix the ID with + :

How to Change the Owner and Group of a File #

To change both the owner and the group of a file use the chown command followed by the new owner and group separated by a colon ( : ) with no intervening spaces and the target file.

Читайте также:  Лабораторная работа серверы linux

The following command will change the ownership of a file named file1 to a new owner named linuxize and group users :

chown linuxize:users file1

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

How to Change the Group of a File #

To change only the group of a file use the chown command followed by a colon ( : ) and the new group name (with no space between them) and the target file as an argument:

The following command will change the owning group of a file named file1 to www-data :

Another command that you can use to change the group ownership of files is chgrp .

When the recursive option is not used, chown command changes the group ownership of the files to which the symlinks points, not the symbolic links themselves.

For example, if you try to change the owner and the group of the symbolic link symlink1 that points to /var/www/file1 , chown will change the ownership of the file or directory the symlink points to:

Chances are that instead of changing the target ownership, you will get a “cannot dereference ‘symlink1’: Permission denied” error.

The error occurs because by default on most Linux distributions symlinks are protected, and you cannot operate on target files. This option is specified in /proc/sys/fs/protected_symlinks . 1 means enabled and 0 disabled. We recommend not to disable the symlink protection.

To change the group ownership of the symlink itself, use the -h option:

How to Recursively Change the File Ownership #

To recursively operate on all files and directories under the given directory, use the -R ( —recursive ) option:

chown -R USER:GROUP DIRECTORY 

The following example will change the ownership of all files and subdirectories under the /var/www directory to a new owner and group named www-data :

If the directory contains symbolic links pass the -h option:

Other options that can be used when recursively changing the directory ownership are -H and -L .

If the argument passed to chown command is a symbolic link that points to a directory, the -H option will cause the command to traverse it. -L tells chown to traverse each symbolic link to a directory that is encountered. Usually, you should not use these options because you might mess up your system or create a security risk.

Using a Reference File #

The —reference=ref_file option allows you to change the user and group ownership of given files to be same as those of the specified reference file ( ref_file ). If the reference file is a symbolic link chown will use the user and group of the target file.

chown --reference=REF_FILE FILE 

For example, the following command will assign the user and group ownership of the file1 to file2

chown --reference=file1 file2

Conclusion #

chown is a Linux/UNIX command-line utility for changing the file’s user and/or group ownership.

Читайте также:  Где хранятся драйвера линукс

To learn more about the chown command visit the chown man page or type man chown in your terminal.

If you have any questions or feedback, feel free to leave a comment.

Источник

How to create, delete, and modify groups in Linux

Groups are an essential part of the Linux permission structure and a powerful way to manage file access on your system.

Hands pointing at a laptop screen while another person uses the mouse

In Linux, groups are collections of users. Creating and managing groups is one of the simplest ways to deal with multiple users simultaneously, especially when dealing with permissions. The /etc/group file stores group information and is the default configuration file.

[ Keep your most commonly used commands handy with the Linux commands cheat sheet. ]

Linux admins use groups to assign access to files and other resources. Every group has a unique ID listed in the /etc/group file, along with the group name and members. The first groups listed in this file are system groups because the distribution maintainers preconfigure them for system activities.

Each user may belong to one primary group and any number of secondary groups. When you create a user on Linux using the useradd command, a group with the same name as the username is also created, and the user is added as the group’s sole member. This group is the user’s primary group.

Training & certification

Create and modify groups

To add a group in Linux, use the groupadd command:

When a group is created, a unique group ID gets assigned to that group. You can verify that the group appears (and see its group ID) by looking in the /etc/group file.

If you want to create a group with a specific group ID (GID), use the —gid or -g option:

$ sudo groupadd -g 1009 demo1

If group ID 1009 is already allocated to another group, you’re alerted that the GID is unavailable and the operation aborts. Rerun it with a different group ID number:

$ sudo groupadd -g 1010 demo1

Change the group ID

You can change the group ID of any group with the groupmod command and the —gid or -g option:

$ sudo groupmod -g 1011 demo1

Great Linux resources

Rename a group

You can rename a group using groupmod with the —new-name or -n option:

$ sudo groupmod -n test demo1

Verify all these changes from the /etc/group file.

Add and remove users from a group

Suppose you have existing users named user1 and user2, and you want to add them to the demo group. Use the usermod command with the —append —groups options ( -a and -G for short):

$ sudo usermod --append --groups demo user1 $ sudo usermod -aG demo user2

Look in the /etc/group file or use the id command to confirm your changes:

$ id user1 uid=1005(user1) gid=1005(user1) groups=100(users),1009(demo)

To remove a specific user from a group, you can use the gpasswd command to modify group information:

$ sudo gpasswd --delete user1 demo

Alternatively, manually edit the /etc/group file and remove the user from any number of groups.

Источник

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