Root User in Ubuntu- Important Things You Should Know

Содержание
  1. How to use the sudo command to become a root user
  2. Using the “sudo su –”
  3. Using the “sudo su”
  4. Using the “sudo -i”
  5. Using “sudo -s”
  6. How to use su command to become a root user
  7. Using “su –”
  8. Using “su -l”
  9. Using “–login”
  10. Bonus Tip
  11. Conclusion
  12. About the author
  13. Change user in CLI
  14. Change user using su
  15. Change user using sudo
  16. Change users in GUI
  17. Change user in GNOME
  18. Final thoughts
  19. About the author
  20. Method #1 – Graphical Environment
  21. Method #2 – The su command
  22. Basic su Command Usage
  23. How to use the su command to switch to a different user
  24. How to use the su command to log in with a different shell
  25. How to use the su command while preserving the environment
  26. How to use the su command to run a command as a different user
  27. Method #3 – SSH
  28. Conclusion
  29. About the author
  30. How to Run Commands in the Terminal as a root User?
  31. Using the “su” Command
  32. Using the “-s” Command
  33. How to Set a Password for root?
  34. Become root Using the “-login” Command
  35. Become root Using the “-l” Command
  36. What is root user? Why is it locked in Ubuntu?
  37. How to run commands as root user in Ubuntu?
  38. How to become root user in Ubuntu?
  39. How to enable root user in Ubuntu?
  40. Change user in Linux command line
  41. Change users in Linux graphically (for desktop Linux)
  42. How to login as a root in the GNOME desktop
  43. Step 1: Enable root account
  44. Step 2: Change GDM configuration
  45. Step 3: Configure PAM authentication
  46. Step 4: Log in as root
  47. Things you should know when running the system as a root user
  48. Su Command Syntax
  49. su Command Options
  50. Switch to a Different User
  51. Run Specific Command as a Different User
  52. Use a Different Shell
  53. Use a Different User in the Same Environment
  54. Command Comparison
  55. Syntax
  56. Options
  57. Granting sudo Privileges
  58. RedHat and CentOS
  59. Debian and Ubuntu
  60. Using visudo and the sudoers Group
  61. Examples of sudo in Linux
  62. Basic Sudo Usage
  63. Run Command as a Different User
  64. Switch to Root User
  65. Execute Previous Commands with sudo
  66. Run Multiple Commands in One Line
  67. Add a String of Text to an Existing File
  68. How to Use the su Command
  69. How Does the su Command Work?
  70. How Does the su – [hyphen] Command Work?
  71. How to Use the sudo Command
  72. Adding a User to the Sudoers Group
  73. How sudo and su Work on Different Linux Distributions
  74. Enabling the Root Account
  75. Additional Features

How to use the sudo command to become a root user

Using the “sudo su –”

Using the “sudo su”

A screenshot of a computer Description automatically generated with medium confidence

Using the “sudo -i”

The output shows that the dollar sign($) has been replaced by the pound key(#) which also directs that you are currently logged in using the root privileges.

Using “sudo -s”

Text Description automatically generated

How to use su command to become a root user

Using “su –”

A screenshot of a computer Description automatically generated with medium confidence

Using “su -l”

A screenshot of a computer Description automatically generated with medium confidence

Using “–login”

A screenshot of a computer Description automatically generated with medium confidence

During the authentication of su command, you have to put the root password.

Дополнительно:  What is root cause analysis (RCA)?

Text Description automatically generated

Bonus Tip

The output shows that you are not logged in using the root privileges.

Conclusion

About the author

  • System: System processes are run under this user.
  • User: The account that general users login to by typing the username and password.
  • Root: A special user that has unrestricted privilege over every part of the system.

Change user in CLI

Change user using su

Root User in Ubuntu- Important Things You Should Know

Root User in Ubuntu- Important Things You Should Know

Root User in Ubuntu- Important Things You Should Know
Verify the change.

Root User in Ubuntu- Important Things You Should Know
It’s strongly recommended not to log in as root unless you have to. If it’s just for running commands with root privilege, then the sudo tool offers more flexibility and security.

Root User in Ubuntu- Important Things You Should Know

Change user using sudo

Root User in Ubuntu- Important Things You Should Know
Verify the action.

Root User in Ubuntu- Important Things You Should Know

Change users in GUI

Change user in GNOME

GNOME is one of the most popular desktop environments out there. It’s available on almost any popular Linux distro. Its interface is pleasing, modern, and fluid. Of course, you can tweak it to give it a more pleasant vibe. Check out some of the best GTK3 themes for Linux. These are GNOME-compatible themes and should work on any modern version of GNOME.

Final thoughts

About the author

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.

Method #1 – Graphical Environment

Root User in Ubuntu- Important Things You Should Know

Root User in Ubuntu- Important Things You Should Know

Method #2 – The su command

Basic su Command Usage

The su command is easy to use with straightforward syntax.

Using the su command with no option does not change the current working directory to /root.

The most common use case of the su command is with the – or -l option. The -l allows su to changes the home directory and spawn a shell session similar to a real-login (set in /etc/passwd).

How to use the su command to switch to a different user

How to use the su command to log in with a different shell

By default, su uses the environment variables stated in the /etc/passwd file. Such options include the default shell. However, you can change the login shell using the -s option.

ubuntu bin

How to use the su command while preserving the environment

In that case, we can use the command:

Root User in Ubuntu- Important Things You Should Know

How to use the su command to run a command as a different user

Method #3 – SSH

Root User in Ubuntu- Important Things You Should Know

Conclusion

About the author

  • How to Run Commands in the Terminal as a root User?
  • How to Become Root In Linux?
  • How to Set a Password for root?

How to Run Commands in the Terminal as a root User?

There are some scenarios where you want to use the terminal as a root but don’t want to switch, which you’ve done many times, as seen below.

Using the “su” Command

Using the “-s” Command

How to Set a Password for root?

Activating the root account raises security concerns, especially if a service is exposed to the web and you’re using it, so it is not recommended to activate it. Professionals should only use this account, and to deactivate it, remove the password using this command.

Дополнительно:  Root of the title

If you see the message “password expiry information changed.” Then the root account is locked again.

Become root Using the “-login” Command

Become root Using the “-l” Command

Either you run commands with root privilege like this:

sudo any_command
sudo su

What is root user? Why is it locked in Ubuntu?

Root User Ubuntu

You don’t need to have root privilege for your daily tasks like moving file in your home directory, downloading files from internet, creating documents etc.

Take this analogy for understanding it better. If you have to cut a fruit, you use a kitchen knife. If you have to cut down a tree, you have to use a saw. Now, you may use the saw to cut fruits but that’s not wise, is it?

Does this mean that you cannot be root in Ubuntu or use the system with root privileges? No, you can still have root access with the help of ‘sudo’ (explained in the next section).

How to run commands as root user in Ubuntu?

apt update
Reading package lists... Done
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)

So, how do you run commands as root? The simple answer is to add sudo before the commands that require to be run as root.

sudo apt update
[email protected]:~$ sudo apt update
[sudo] password for abhishek: 

If you are absolutely new to Linux, you might be surprised that when you start typing your password in the terminal, nothing happens on the screen. This is perfectly normal because as the default security feature, nothing is displayed on the screen. Not even the asterisks (*). You type your password and press enter.

Bottom line:
To run commands as root in Ubuntu, add sudo before the command.
When asked for password, enter your account’s password.
When you type the password on the screen, nothing is visible. Just keep on typing the password and press enter.

How to become root user in Ubuntu?

The sudo command allows you to simulate a root login shell with this command:

sudo -i
[email protected]:~$ sudo -i
[sudo] password for abhishek: 
[email protected]:~# whoami
root
[email protected]:~# 

You’ll notice that when you switch to root, the shell command prompt changes from $ (dollar key sign) to # (pound key sign). This makes me crack a (lame) joke that pound is stronger than dollar.

sudo su

If you try to use the su command without sudo, you’ll encounter ‘su authentication failure’ error.

exit

How to enable root user in Ubuntu?

sudo passwd root

Again, this is not recommended and I won’t encourage you to do that on your desktop. If you forgot it, you won’t be able to change the root password in Ubuntu again.

sudo passwd -dl root

I hope you have a slightly better understanding of the root concept now. If you still have some confusion and questions about it, please let me know in the comments. I’ll try to answer your questions and might update the article as well.

Дополнительно:  Samsung gt i9082 root права
su another_username
Switching Users in Ubuntu Linux

Change user in Linux command line

Switch Users Linux
su another_username
su - another_username
Switch User Ubuntu Linux

Note: though -is more popular, it is advised to use –login option.

Change users in Linux graphically (for desktop Linux)

  • Switch User: You get to keep your session active (applications keep on running) for current user. Good for temporarily switching users as you won’t lose your work.
  • Log out: Current session ends (all applications are closed). Good when you want to switch to the other user for a long time.

You can choose whichever option is more suited for your need.

Switch User in Ubuntu

By default, Ubuntu disables the root account. You must use the sudo command for any tasks requiring root privileges.

This is for your own security, of course. Using the system as root all the time is like running around with a sword in your hand. It increases the chances of messing up things.

Logging in as root is still common in the servers. On the desktop side, it’s quite rare to log in as root. Even Kali Linux has changed it.

in this guide, I will show you how to log in as a root in your GNOME desktop using Ubuntu.

How to login as a root in the GNOME desktop

I won’t advise login as root on the desktop. You have sudo mechanism for all your root needs. Do it only if you have a good enough reason. This tutorial is for demo purposes only. You have been cautioned.

Step 1: Enable root account

You want to log in as root. But the root account is disabled by default. The first step is to enable it.

Change the root account password that will eventually enable the root account for you:

sudo passwd root
change the password of root account in ubuntu

It goes without saying that you should not forget the root password.

Step 2: Change GDM configuration

Ubuntu uses GNOME by default and GNOME uses the GDM display manager.

To allow log in as root into GNOME, you need to make some changes in the GDM configuration file located at /etc/gdm3/custom.conf.

Make a backup of the config file:

cp /etc/gdm3/custom.conf /etc/gdm3/custom.conf~

In the worst case, if you somehow mess things up, the back up file can be used to replace the existing one from the TTY.

sudo nano /etc/gdm3/custom.conf
AllowRoot=true
configure GDM on ubuntu

Press Ctrl+X to exit Nano while saving it.

Step 3: Configure PAM authentication

sudo nano /etc/pam.d/gdm-password
auth   required        pam_succeed_if.so user != root quiet_success
Root User in Ubuntu- Important Things You Should Know

Save changes and exit from the nano text editor.

Step 4: Log in as root

Now, reboot your system:

reboot
Login as a root in ubuntu desktop
Logging as root (Click to enlarge)
logged in as a privileged user in Ubuntu

That’s it! Now, you are running your Ubuntu system as a root.

Things you should know when running the system as a root user

There is a reason why Ubuntu disables a root account by default. Want to know why? Here you have it:

Root User in Ubuntu- Important Things You Should Know

Root User in Ubuntu- Important Things You Should Know

In this guide, you will learn how to use the su command, with practical examples.

A tutorial on how to use the su command in Linux ubuntu or centos
  • A system running Linux
  • Access to a terminal window/command-line (Ctrl-Alt-T, Ctrl-Alt-F2)

Su Command Syntax

su [options] [username [arguments]]

su Command Options

su –h
Display a list of su commands in Linux.

Here are some common options to use with the su command:

  • Username – Replace username with the actual username you want to log in with. This can be any user, not just root.
  • –c or –command [command] – Runs a specific command as the specified user.
  •  or –l or –login [username] – Runs a login script to change to a specific username.  You’ll need to enter a password for that user.
  • –s or –shell [shell] – Allows you to specify a different shell environment to run in.
  • –h or –help – Show the help file for the su command.
  • –p or ––preserve–environment – Preserve the shell environment (HOME, SHELL, USER, LOGNAME).

Switch to a Different User

su –l [other_user]

Run Specific Command as a Different User

su –c [command] [other_user]

When you enter this example, the system will use the specified account to run the ls (list directory contents) command.

Use a Different Shell

su –s /usr/bin/zsh

Use a Different User in the Same Environment

su –p [other_user]

Command Comparison

sudo su -

su is an older but more fully-featured command. It can duplicate the functionality of sudo by use of the –c option to pass a single command to the shell.

Next, consider checking out our Linux Command Cheat Sheet Tutorial With Examples.

tutorial explains how to use the sudo command in linux
  • A system running Linux
  • Access to a command line/terminal window (Activities > Search > Terminal)
  • A user account with sudo or root privileges

Syntax

sudo [command]

Options

sudo can be used with additional options:

  • -h – help; displays syntax and command options
  • -V – version; displays the current version of the sudo application
  • -v – validate; refresh the time limit on sudo without running a command
  • -l – list; lists the user’s privileges, or checks a specific command
  • -k – kill; end the current sudo privileges

Additional options can be found under the -h option.

Options related to the sudo command in Linux.

Granting sudo Privileges

RedHat and CentOS

usermod -aG wheel [username]

Debian and Ubuntu

usermod -aG sudo [username]

Using visudo and the sudoers Group

1. Use the visudo command to edit the configuration file:

sudo visudo
[username] ALL=(ALL:ALL) ALL
Image of the visudo configuration file in Linux.

3. Save and exit the file.

Here’s a breakdown of the granted sudo privileges:

[username] [any-hostname]=([run-as-username]:[run-as-groupname]) [commands-allowed]

Examples of sudo in Linux

Basic Sudo Usage

apt-get update

2. You should see an error message. You do not have the necessary permissions to run the command.

example of an action in Linux that requires elevated permissions

3. Try the same command with sudo:

sudo apt-get update

4. Type your password when prompted. The system executes the command and updates the repositories.

system updating the official repositories when sudo invoked

Run Command as a Different User

whoami
sudo -u [different_username] whoami
Using the sudo -u command to run a command as another user

Switch to Root User

sudo bash

Your command line should change to:

root@hostname:/home/[username]
Using the sudo bash command to switch root user

Execute Previous Commands with sudo

The Linux command line keeps a record of previously executed commands. These records can be accessed by pressing the up arrow. To repeat the last command with elevated privileges, use:

sudo !!
sudo !6

This example repeats the 6th entry in history with the sudo command.

To learn about how to efficiently use history command, check out our article on sudo history command with examples.

Run Multiple Commands in One Line

String multiple commands together, separated by a semicolon:

sudo ls; whoami; hostname
Running multiple sudo commands.

Add a String of Text to an Existing File

echo ‘string-of-text’ | sudo tee -a [path_to_file]
echo "deb http://nginx.org/packages/debian `lsb_release -cs` nginx" \ | sudo tee /etc/apt/sources.list.d/nginx.list
Invoking the sudo command to add the Nginx repository

Note: This would add the Nginx software repositories to your system.

You should now understand the sudo command, and how to use it. Next, learn the difference between the sudo and su command.

In this tutorial, learn all about the difference between su and sudo

The difference between sudo and su explained.

How to Use the su Command

The main syntax is:

su [user_name]
su - [user_name]

How Does the su Command Work?

su phoenixnap

Then, provide the password for the phoenixnap account and hit Enter.

How to use the su command.

How Does the su – [hyphen] Command Work?

su - phoenixnap

How to Use the sudo Command

The main syntax is:

sudo [command]

Adding a User to the Sudoers Group

usermod -aG sudo [user_name]

For instance, to add the account phoenixnap, you would type:

sudo usermod -aG sudo phoenixnap

To see a list of accounts that belong to the sudoers group run:

sudo getent group sudo

The output should display the account added in the previous step:

List users in the sudoers group.

How sudo and su Work on Different Linux Distributions

su is an older but more fully-featured command included in all Linux distributions. It is the traditional way to switch to the root account.

Linux discourages working as root as it may cause unwanted system-wide changes and suggests using sudo instead. For this reason, all Ubuntu-based releases are sudo-only, meaning the root account is not active by default.

Enabling the Root Account

Authentication failure when trying to move to root user in Ubuntu.
sudo passwd root
How to enable the root user on Ubuntu.

Additional Features

sudo -i
Move to root shell using the sudo command.

Likewise, su can also function as sudo and run a single command as the root:

su -c [command]

After reading this article, you should understand the difference between the sudo and su command. Remember to use sudo whenever you can to prevent potential security and system-wide issues.

Оцените статью
Master Hi-technology
Добавить комментарий