- How to Give Root Privileges to a User in Linux
- Method 1: Adding to Root Group using usermod
- Method 2: Adding to Root Group using Useradd Command
- Method 3: Editing /etc/passwd file
- Method 4: Setting as Sudo User
- HowTo: Grant Root Access to User – Root Privileges – Linux
- Grant Root Privileges To New User
- Grant Root Privileges To Existent User
- Add User To Root Group
- Delete User With Root Privileges
- 39 Replies to “HowTo: Grant Root Access to User – Root Privileges – Linux”
- How to manage Linux permissions for users, groups, and others
- Linux security
- Setting up a playground
- How do I create directories and files?
- How do I manage ownership and groups?
- How do I manage permissions?
- How do I use absolute mode?
- More Linux resources
- How do I use symbolic mode?
- Special permissions and Access Control Lists
- Wrap up
How to Give Root Privileges to a User in Linux
The “root” super user is the king of users in Linux/Unix. Having root access grants full and unlimited access to the Linux box.
In this tutorial, I will show you how to allow root access to a user in a Linux system. Typically, root level access is used in system administration. So it is always a pain to give root access to other users. You need to be careful and should withdraw the access once the need to do so is over.
According to Linux file system permissions, root or super user has full permission to read(r), write (w) and execute(x) any file. By default root user id is ‘0’.
I am going to create two users namely user1 and user2. Then I will show you how to give root access to ‘user1’.
Method 1: Adding to Root Group using usermod
Let see how we can grant normal user root access by adding to root group.
These are the groups I have in my Linux box.
I am going to add user1 to root group as follows:
The command given below provides the existing user with the root privilege
Method 2: Adding to Root Group using Useradd Command
I have added a new user, ‘user3’ to the root group using one single command:
Another option using useradd command
Method 3: Editing /etc/passwd file
Edit /etc/passwd for the particular user. Change the user’s UID and GID to ‘0’. This will give root permissions to user.
Now, temproot user should have root privilege:
Note: This is not the recommended method for granting root access
Method 4: Setting as Sudo User
The sudo configuration file is /etc/sudoers and you can edit this file using visudo command: # visudo.
Using visudo protects from conflicts and guarantees that the right syntax is used.
To give full access to specific users
Add the entry given below in the file:
Following this method is not a good idea because this allows both bob and tom to use the su command to grant themselves permanent root privileges. Thereby skipping the command logging features of sudo.
Granting access to specific files to one particular user
This entry allows bob and all the other members of the group operator to gain access to all the program files in the /sbin and /usr/sbin directories, as well as the privilege of running the command /usr/oracle/backup.pl.
If you have any questions or thoughts to share on this topic, use the feedback form.
Источник
HowTo: Grant Root Access to User – Root Privileges – Linux
From this article you’ll learn how to create a user in Linux and grant root access to him or how to grant root privileges to already existent user.
This can be easily done by changing UID (user id) and GID (group id) in /etc/passwd file.
Also you will learn how to just add user to root group and i will explain how to delete user with root privileges.
Actually it is not a good idea to give all the privileges of root to a non-root user and outside the test environment i would not recommend to have multiply superusers.
Warning: Giving a non-root user all the permissions of root is very dangerous, because the non-root user will be able to do literally anything that could cause a big trouble if account is hijacked.
Check SSH Server Settings: If you have disabled root access in SSH server settings, by setting PermitRootLogin no in /etc/ssh/sshd_config – you won’t be able to login if your user has UID 0 .
Warning: Before moving forward, read the comments below and DON’T FOLLOW THE STEPS FROM THIS ARTICLE if you don’t understand of what you are doing and what impact this may have!
Grant Root Privileges To New User
To create a user with exactly the same privileges as root user, we have to assign him the same user ID as the root user has ( UID 0 ) and the same group ID ( GID 0 ).
Use the following commands to create a user john , grand him the same privileges as root and set him a password:
Grant Root Privileges To Existent User
Cool Tip: Dot the i’s and cross the t’s on file and folder permissions in Linux! Make it more clear! Read more →
Perhaps you already have a user john and you want to grant him root privileges (make him a second root user):
For this, it is required to edit the file /etc/passwd and just change UID and GID to 0 :
Add User To Root Group
If you just want to add john to root group, without granting him all root privileges, run the following command:
Delete User With Root Privileges
Cool Tip: Log in to a remote Linux server without entering password! Set up password-less SSH login! Read more →
You won’t be able to delete a user with UID 0 using userdel command:
To delete him, firstly open the /etc/passwd file and change his UID .
For example, change the line:
to something like:
After this, you’ll be able to delete user john with userdel command:
39 Replies to “HowTo: Grant Root Access to User – Root Privileges – Linux”
“For this, it is required to edit the file /etc/passwd and just change UID and GID to 0”
WRONG.
Baaaad idea. Much regret. Undo, undo.
$ ssh 192.168.88.16 -l pi
pi@192.168.88.16‘s password:
Permission denied, please try again.
pi@192.168.88.16‘s password:
Permission denied, please try again.
– – –
Nice explanation?)
Since noone elaborate how to undo this broken way of adding users, I will. The only way is to successful login to the linux machine, if you login via ssh or tty.
If you add a user with uid 0 to group uid 0, and you set in sshd_config AllowRootLogin NO, you`re access will be denied. So you will have to login to the linux machine via tty (this is the only way if you don`t have a correctly added backup user on the linux machine). Via tty you can login with any of the users, even root.
The correct way to add a user with root privileges is adding the user the normal way, useradd -m user, and then add privileges with visudo to the user.
So if you have a backup user that haves root privileges in visudo. you will be able to login to the linux machine via ssh, and you will be able to change the uid and group to the “broken” user.
I think this article explains more likely how to add a user with root privileges “the hacker way (which is a broken way)”, and repair the broken way after.
Also if you don`t have access to a tty console, and you want to test the commands of this article and be sure you did not broke something, STAY LOGGED in current session and open another one and test if all things are OK. If you can`t login in the new session, you still have opened the previous session and you can repair what have you broken.
Hope it helps to repair further mistakes.
Источник
How to manage Linux permissions for users, groups, and others
Photo by Min An from Pexels
Managing access to resources is a fundamental task for sysadmins. This responsibility consists of three components: identities, resources, and permissions. This article covers several user, group, and file management commands to control access to resources. The article uses a «How do I…?» format, and it assumes you have a few resources to work with. Specifically, I cover the following topics:
- Creating directories and files
- Managing ownership and associated groups
- Setting permissions with absolute and symbolic modes
Linux security
Setting up a playground
I’ve been in IT for about 25 years, and most of that time was spent as a technical trainer. That means that the things that I write are usually structured as some sort of lab or other hands-on opportunity. It’s just how I cover material. With that in mind, I’ll assume you have a couple of identities and resources to experiment with as you read the rest of the article. You can use the following commands to set up a playground. It’s best to do this on a virtual machine rather than your personal Linux box, but these tasks are relatively harmless.
Create two new users and two new groups to work with. Note that you do not need to configure passwords for the users in this exercise, as you won’t log on with those accounts.
Note: You would use the passwd user01 command to set the user’s password.
In your home directory, create a new directory named playground :
Change into the
/playground directory by using the cd command. You are ready to work with the commands and concepts below.
When you’ve completed the article and learned the techniques I’ve covered, delete the two user accounts, the groups, and the playground directory. Use rm -fR /playground , userdel user01 , and groupdel groupA to remove the resources.
How do I create directories and files?
Use the mkdir command to create directories. The touch command is one of many ways to create files.
How do I create a directory named Resources ?
How do I create a directory path (a series of directories that don’t yet exist)?
Note: The goal here is to create the 2020data directory, but the given path’s data directory does not yet exist. The -p option creates parent directories as needed to complete the path.
How do I create a file named file1 ?
How do I create several files at once?
How do I manage ownership and groups?
In the playground directory, display the current owner and group associated with the Resources directory and the files.
How do I display permission, owners, and groups?
The ls -l command displays directory contents in long format. The long format contains both permissions and ownership. You can see that the user account that created the resources also owns those resources. The group association is also that user’s primary group.
How do I change the user/owner associated with file1 ?
How do I change the group associated with file1 ?
How do I change the owner and group at the same time for file2 ?
There is a specific chgrp command, but I prefer only to memorize one command ( chown ) and apply it to both functions (user and group associations) rather than chown for the user and then have to recall chgrp for the group.
So how do I use chgrp ?
How do I change the user/group for a directory and all of its contents?
The above task provides a recursive configuration. Technically, recursive commands are repeated on each specified object. Effectively, recursive means «this and everything in it.» In the above example, you are configuring the related user/group for the Resources directory and everything in it. Without the -R option, you would only affect the Resources directory itself, but not its contents.
How do I manage permissions?
The change mode or chmod command sets permissions. The syntax is straight-forward:
Here are two examples of manipulating permissions for file2 :
But wait! Those appear to be radically different examples (they’re not, actually). What are all those letters and numbers?
We need to discuss absolute mode and symbolic mode.
How do I use absolute mode?
Absolute mode is one of two ways of specifying permissions. I’ve seen this mode referred to as octal or numeric mode, but the term I learned was absolute. That term also makes the most sense to me because it’s an absolute statement of the desired permissions. I always told my students that this seemed like the most complex of the two modes but is actually the simplest. Usually, they agreed.
Each access level (read, write, execute) has an octal value:
Access level | Octal value |
Read | 4 |
Write | 2 |
Execute | 1 |
Each identity (user, group, others) has a position:
Identity | Position |
User | First or left-most |
Group | Middle |
Others | Last or right-most |
More Linux resources
The absolute mode syntax states the desired permissions from left to right.
How do I grant the user (owner) read, write, and execute, the group read-only, and all others no access to file2 by using absolute mode?
The three permissions values are associated with identities:
ugo
740
- The 7 is assigned to the user and is the sum of 4+2+1 or read+write+execute (full access)
- The 4 is assigned to the group and is the sum of 4+0+0 (read-only)
- The 0 is assigned to others and is the sum of 0+0+0 (no access)
In this example, the user has rwx, the group has r only, and all others have no access to file2 .
Let’s look at one more example.
How do I grant the user (owner) read and write, the group read-only, and all others read-only to file2 ?
- The user has 6 (read and write)
- The group has 4 (read-only)
- All others have 4 (read-only)
I find this easier because there are no calculations involved. I’m not concerned with adding or subtracting specific permissions based on the current settings. Instead, I say, «set the permissions to be this,» and that’s the end result I get. It’s an absolute statement.
How do I set permissions for the Resources directory and all of its contents by using absolute mode?
How do I use symbolic mode?
Symbolic mode uses more symbols, but the symbols are simpler to understand. That’s attractive to sysadmins that are new to standard Linux permissions.
Each access level has a symbol:
Access level | Symbol |
Read | r |
Write | w |
Execute | x |
Each identity has a symbol:
Identity | Symbol |
User | u |
Group | g |
Others | o |
There are also operators to manipulate the permissions:
Task | Operator |
Grant a level of access | + |
Remove a level of access | — |
Set a level of access | = |
The general chmod command syntax is the same:
Here is an example:
How do I remove the read permissions from others for file2 by using symbolic mode?
This example removes ( — ) the read ( r ) permission from others ( o ) for file2 .
Here’s another simple example:
How do I grant the read and write permissions to the group for file2 ?
This one gives ( + ) read and write ( rw ) to the group ( g ) for file2 .
How do I set permissions for a directory and all of its contents by using symbolic mode?
Special permissions and Access Control Lists
The above discussion covers standard Linux permissions—applying rwx to the user, group, and all others. Linux has far more flexibility, however. Special permissions permit users to run applications with other credentials, control the inheritance of group associations, and keep files from being changed accidentally. Check out this great article on special permissions.
Linux also has a way of enforcing different permissions for different users and groups. Access Control Lists (ACLs) permit sysadmins to define permissions for more than just one user and one group, which adds a great deal more flexibility to standard permissions. For example, user01 can be granted rw- to file1 , while user02 can be granted r— to file1 . Here is a great article on ACLs.
Wrap up
Creating resources, managing users, and setting permissions are fundamental tasks for Linux users. My goal was to provide a quick and easy guide based on common questions or tasks that we must all accomplish regularly. If you’re new to Linux, having a solid grasp of the eight commands discussed above will make your sysadmin life much easier.
Источник