su vs sudo - Linux Command
The Root User
Both su and sudo are used to run commands with root permissions. The root user is basically equivalent to the administrator user on Windows – the root user has maximum permissions and can do anything to the system. Normal users on Linux run with reduced permissions – for example, they can’t install software or write to system directories.
To do something that requires these permissions, you’ll have to acquire them with su or sudo.
Su vs. Sudo
The su command switches to the super user – or root user – when you execute it with no additional options. You’ll have to enter the root account’s password. This isn’t all the su command does, though – you can use it to switch to any user account. If you execute the su bob command, you’ll be prompted to enter Bob’s password and the shell will switch to Bob’s user account.
Once you’re done running commands in the root shell, you should type exit to leave the root shell and go back to limited-privileges mode.
Sudo runs a single command with root privileges. When you execute sudo command, the system prompts you for your current user account’s password before running command as the root user. By default, Ubuntu remembers the password for fifteen minutes and won’t ask for a password again until the fifteen minutes
To get root access, you can use one of a variety of methods:
Run
sudo <command>and type in your login password, if prompted, to run only that instance of the command as root. Next time you run another or the same command without thesudoprefix, you will not have root access.Run
sudo -i. This will give you an interactive root shell. Note that the$at the end of your prompt has changed to a#, indicating that you have root access. But you fall in the root home directory (/root/). From here you can run any sequence of commands as root, or run the commandexitto leave the root shell.Use the
su(substitute user) command to get a root shell. This is effectively the same as usingsudo -i. Note that when you use this command it will ask for the root password and not your login password. These are not the same. You may have to set or change the root password by runningsudo passwd rootfirst.Run
sudo -s. This gives you root access, but maintains your current SHELL. Shell specific settings, including your current directory, are preserved. For instance if you usebash(Ubuntu's default shell), aliases (and any other settings from~/.bashrc) are kept when you switch to the root user. To leave the root access, typeexitas in the cases above.
sudo su -
The command starts a login shell and you will find yourself in root's home directory with root's environment.
Comments
Post a Comment