Linux create user with permissions

Add new user with permissions same as existing user

Do you want a different user with permissions such as granted by the groups the existing user has. Or do you want the new user to be able to access everything as the existing user but have a different login name? Maybe a bit more information on what you try to achieve and why you think ‘creating a new user with permissions exactly as an existing user’ is a solution makes things more clear.

@Anthon, I want the new user to be able to access everything as the existing user but have a different login name.

2 Answers 2

You should create a new user as Hauke is right in indicating that creating two with the same UID is going to be confusing ( you could do that with useradd -u EXISTINGUID . )

You probably just want to make a new user and make sure they are in the same group and that the group permissions are so that they can work with the data in the same group in the same way. Most installations now seem to make a group per user, so if your existing user is named exus you have a groupname exus in /etc/group as well, note the gid (group id) and all the other groups that exus is a member of ( vboxuser , dialout etc -> `gid2, gid3). Create the new user with:

useradd -N -g gid -G gid2,gid3 -m 

(You could have them use the same home directory by replacing -m with —home ~exus , not sure if that is what you want)

Make sure that all of the group permissions of the new files created by both users are based on a umask of 002 or 007 and that permissions on existing files owned by the exus have the group permissions the same as the user permissions:

find / -user exus -print0 | xargs -0 chmod g=u 

Источник

User Groups and Permissions in Linux

From smartphones to cars, supercomputers and home appliances, home desktops to enterprise servers, the Linux operating system is everywhere.

Introduction

To create a secure environment in Linux, you need to learn about user groups and permissions. For example, if you work in a company and you want the finance department to read a file but not make any modification to it, then you need to use permissions in Linux. It is a must for every programmer working with Linux nowadays.

Prerequisites

To follow along with this tutorial, you should have:

Table of contents

File permissions

Let’s start by talking about the ownership of Linux files.

  1. User: the owner of the file (person who created the file).
  2. Group: the group can contain multiple users. Therefore, all users in that group will have the same permissions. It makes things easier than assign permission for every user you want.
  3. Other: any person has access to that file, that person has neither created the file, nor are they in any group which has access to that file.
Читайте также:  Linux count directories in directory

When you perform the following command:

Then you will see the file’s permissions, like the following:

Permissions Example

We will work with this part “-rw-r–r–”.

Explain permissions

As we see above, the empty first part means that it is a file. If it were a directory then it will be the letter “d” instead. The second part means that the user “Home” has read and write permissions but he does not have the execute one. The group and others have only the read permission.

Let’s change the permissions using the chmod command.

This command will add the write permission for other users to my text file “section.txt”.

Now if you try to execute ls -l then you will see -rw-r—rw- .

“o” refers to others, “g” for the group, “u” for the user, and “a” for all.

Now let’s add the execute permission to the user with:

The permissions will be -rwxr—rw- .

If you want to remove the permission, you can use the same method but with “-” instead of “+”. For example, let’s remove the execute permission from the user by:

And the permissions now are: -rw-r—rw- .

Also, you can use Symbolic Mode to modify permissions like the following:

Number Permission
0 No permission
1 Execute
2 Write
3 Execute and Write
4 Read
5 Read and Execute
6 Read and Write
7 Read, Write and Execute

For example, let’s give every permission for all with:

Symbolic Mode Example

Then the permissions will be: -rwxrwxrwx .

Let’s remove the execute from the group and the write from other by:

The permissions will be: -rwxrw-r-x .

User accounts

Create a user

We can create a new user account by issuing the following command:

We can make sure that the user has been created in two ways:

And the output will be something like this:

uid=1007(testuser) gid=1009(testuser) groups=1009(testuser) 

This will show the user id and the groups that the user is currently in, usually, a new group with the same username is assigned to the user.

So we can issue cat /etc/passwd and we will see the new user that has been created.

After creating the user using the command above, you notice that no user directories have been created inside /home directory, which is not good since the user cannot log in to his account.

To create a new user with its directories, we can issue:

sudo useradd -m -s /bin/bash testuser 

If you navigate to the /home directory, you notice that a new directory with the name testuser is created.

Afterwards, you need to set a new password to the testuser by:

We noticed that creating a new user takes a lot of commands to accomplish, so there is a command that automates everything:

After creating a new user and setting a password to it, you can log in in two ways:

Читайте также:  Linux acer aspire one 725

Delete a user

Like the process of adding users, there are two commands that delete a user.

If you try that command, you will notice that the user directory has not been deleted and you need to delete it by yourself.

You can use this automated command to do everything for you:

sudo deluser --remove-home testuser 

User groups

A group is a collection of users. The primary purpose of the groups is to define a set of privileges like read, write, or execute permission for a given resource that can be shared among the users within the group.

Create a group

You can see all of the groups you have by opening the following file:

Let’s create a group with the name of section by:

Add user to a group

We will add the testuser user to the section group by:

sudo usermod -aG section testuser 

Delete user from a group

You can delete the testuser from the group with:

sudo gpasswd -d testuser section 

Delete a group

Let’s delete the previous group by:

Conclusion

Linux is one of the most secure systems because it allows an admin to create multiple users with different permissions in the same hardware.

And now you know exactly how to do it!✨

Further reading

Peer Review Contributions by: Odhiambo Paul

Источник

Creating a new user and modifying its privileges in Linux

If you use Linux with a graphical desktop environment like Ubuntu, for example, you can add or remove new users through its Settings. But this is simplified and I have to say a limited way to do that. The right way is to use the command line. And it is the only way if you work with a server Linux system. Below, you will learn all you need to know about how to create, delete, and modify users in the Linux command line.

Simple way to create a new user in Linux.

Many Linux systems have a user-friendly command to add a new user. The command is adduser. You type this command and the username of a new user.

The command will prompt you to create a user password, full name, and some additional information which can be skipped if you want.

adduser command

And that’s it. The new user is created. This command also creates a home directory for new user ( /home/username )

However, if you also need to grant this user the administrative privileges, you need to add it to the sudo group:

sudo adduser username sudo 

Now, this new user will be able to execute the administrative commands with sudo .

This was the simplest way to create a new user in Linux. The adduser command is available in many Linux ditros, but in fact, it is a more user-friendly type of the command useradd , which is more advanced. Below, I would like to show you how to use this more advanced command too.

Universal way to create a new user in Linux.

If adduser is not available in your distro or you want to have little more control over the new user. You need to use the command useradd . I know the names are similar and easy to mix. But try to remember that useradd is a more important command. Basically, adduser just points to useradd .

Читайте также:  Linux разметка загрузочный диск

To create a new user with the default options, run:

To check what default options were used to create a user, run:

These are the default rules on my Debian VPS (they may differ for your system. ):

Default options of the command useradd

You can change these options and use some more. To see all available options of the useradd command, check its help:

Based on these options, a more complete command would be:

sudo useradd -g users -G sudo -s /bin/bash -m -c "Full name" username 

Finally, you need to set a password for this user with passwd.

an example of the useradd command

New user’s system privileges

As you have seen I added a new user to the sudo group and granted it administrative privileges. This is what I did on my server and this what you would want to do if you are an admin of the system. But if you create a user on your Linux system for someone else, you probably do not want them to have administrative privileges. So, do not add them to the sudo group.

Setting password and account expiration

If you are a system administrator and you have many uses in your system, besides not including them in the sudo group, you may also want to enhance the security of your system by the expiration time on the passwords and accounts of these new users.

You can do this with the command chage . Note, it is without n . The command is short of change age.

You can see all the available options of this command:

To check if there are any limitations set on a user, run:

check account and password expiration

Usually, there are no expiration dates by default. But you can set some limits with the command chage :

sudo chage -M 90 -W 30 -E 2020-06-07 username 

The above command will set a password expiration date to 90 days and warning about the need to update the password to 30 days before the expiration. And the account will expire on June 6, 2020.

You can see that if you check the status of the user:

set account and password expiration in Linux

You can also do some manipulation with users using the command usermod . But I have to skip it because this post will be too long.

How to delete a user

Finally, to delete a user, run this command:

If you also want to remove the home directory of this user, add option -r. But be careful, because it will remove all the data of this user:

Summary

  • To create a new user in Linux, you can use the user-friendly command adduser or the universal command useradd . The latter is available in all Linux distros.
  • New users do not have administrative privileges by default, to grant them such privileges, add them to the sudo group.
  • To set time limits on password and account of a user, use the command chage .
  • To delete a user, use the command userdel

Average Linux UserFollow I am the founder of the Average Linux User project, which is a hobby I work on at night. During the day I am a scientist who uses computers to analyze genetic data.

Источник

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