Linux command line basics: sudo
https://www.redhat.com/sysadmin/sudo
What is sudo
?
Sudo stands for "superuser do" and is the master key to your high-privilege admin tasks. Have you ever tried to edit a config file only to receive "Permission Denied?" (The
/etc/hosts
file comes to mind.) If so, that was because your user account did not have access to that file. You need root or sudoer access. Previously, back when I was doing things "my way," I used to always use thesu
command (switch user) and would log in as root for these tasks. While this method works, it isn’t the best way to accomplish the needed task in most cases.Think about this: You
su
to root to edit a file, but you forget to switch back to your user account afterward. At this point, a simple command line error could cost you dearly as an administrator. I saw a colleague blow away the root directory of a back-end storage server for the state of New York due to this simple oversight. Thankfully, the data and his career were recoverable!
So how do I use sudo
?
Simply preface the intended command with
Imagesudo
. You will then be prompted for a password (you need to enter your user account password, not root's). For example, if you want to edit an important configuration file, you might usevi /etc/sudoers
:When you do, though, you would be met with "Permission Denied" error, or an equivalent:
ImageNow, try that same command prefaced with
Imagesudo
:You would be prompted for your user account's password. After entering a correct credential, you would then be given read/write access to the file:
ImageNow that you know how to use this command, let’s look at how to configure the
sudoers
file.
Becoming a super user
The
sudo
privilege is given on a per-user or per-group basis. To ensure that your account has this privilege, you must be added to thesudoers
file. The file is located at/etc/sudoers
and requires root permissions. To make yourself a superuser, enter the following into the bottom of the file:username ALL=(ALL) ALL //gives user "username" sudo access %wheel ALL=(ALL) ALL //Gives all users that belong to the wheel group sudo access
Doing this will give your user account all root privileges, so use them wisely.
Options and takeaways
Now that you understand the basic command and how to add your user account to the superuser config file, you are probably ready to look into options and flags. There is far too much information in the man pages to cover here, so I will simply leave a link so you can look over the information for yourself.
Remember that
sudo
is the preferred way to use elevated privileges to securely accomplish a task. This tool is simple to use and configure, and it doesn’t leave you exposed in a high-privilege account. Sometimes, there really is a better way.Want to try out Red Hat Enterprise Linux? Download it now for free.
Managing sudo access
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_basic_system_settings/managing-sudo-access_configuring-basic-system-settings#doc-wrapper
System administrators can grant
sudo
access to allow non-root users to execute administrative commands that are normally reserved for theroot
user. As a result, non-root users can enter such commands without logging in to theroot
user account.
Su Command in Linux (Switch User)
https://linuxize.com/post/su-command-in-linux/
Why doesn't "sudo su" in a shell script run the rest of the script as root?
https://unix.stackexchange.com/questions/70859/why-doesnt-sudo-su-in-a-shell-script-run-the-rest-of-the-script-as-root
标签:basics,sudo,access,command,user,file,line,root From: https://www.cnblogs.com/lightsong/p/17486725.html