首页 > 系统相关 >Linux command line basics: sudo

Linux command line basics: sudo

时间:2023-06-16 23:44:19浏览次数:52  
标签:basics sudo access command user file line root

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 the su 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 sudo. 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 use vi /etc/sudoers:

Image vi /etc/sudoers

When you do, though, you would be met with "Permission Denied" error, or an equivalent:

Image permission denied

Now, try that same command prefaced with sudo:

Image sudo vi /etc/sudoers

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:

Image open sudoers file

Now 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 the sudoers 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 the root user. As a result, non-root users can enter such commands without logging in to the root 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

相关文章

  • logrotate command in Linux with examples
    logrotatecommandinLinuxwithexampleshttps://linuxconfig.org/logrotateInLinux,manyapplicationsandsystemserviceswillstorelogfiles.TheselogfilesgiveaLinuxadministratorinsightintohowtheirsystemisperforming,andareinvaluablewh......
  • aapium报错 An unknown server-side error occurred while processing the command.
    现象:1、appium日志存在报错信息:(1)中间:adbfailedtostartdaemon*(2)结尾:POST/wd/hub/session500287ms2、appium图形界面设置参数后,点击startsession报错提示Anunknownserver-sideerroroccurredwhileprocessingthecommand.Originalerror:Couldnotfindaconn......
  • C#Word开发,VSTO中自定义右键菜单,CommandBarButton的Click事件不触发,或者只触发一次
    C#Word开发,VSTO中自定义右键菜单,CommandBarButton的Click事件不触发,或者只触发一次_vsto右键快捷方式word_话与山鬼听的博客-CSDN博客 Word2016不会响应WindowBeforeRightClick事件的Bug问题 ......
  • Compile Unity jslib in command
    CompileUnityjslibincommand(JinQing’sColumn,May.,2023)MyUnityprojecthasajslibfile,whichhasmanysyntaxerrors.ButUnitydoesnotreportthesejsliberrorslikecsfiles,untiltheWebGLbuildfailswithaerrorlikethis:Failedprocessstd......
  • yarn 安装进行时,显现错误 node_modules\gifsicle: Command failed.
    1.错误显示[4/5]Buildingfreshpackages...[7/13]⠠jpegtran-bin[6/13]⠠gifsicle[8/13]⠠mozjpeg[4/13]⠠gifsicleerrorE:\IdeaStudyProjects\ucthings-ui-vue\node_modules\gifsicle:Commandfailed.Exitcode:1Command:nodelib/install.jsArguments:D......
  • WPF 绑定command在触屏上操作时,即使点中button也可能不触发
    现象:WPF程序绑定的command在触摸屏上,点击button后,button绑定的事件偶尔会出现不响应的情况。确认100%点击有效,不会出现误触的情况。但是用鼠标点击,绝对不会出现这种现象。 原因:在非触屏环境,bingdingcommand是由点击来触发,即是使用鼠标等输入设备来执行点击触发command,调用......
  • # command-line-arguments day1/goroute/main.go:10:6: undefined: test_goroute
    在goland中执行gobuildmain.go命令时报错#command-line-argumentsday1/goroute/main.go:10:6:undefined:test_goroute上面的原因是调用的方法没有引用,但用的包名是一样的,照理说应该引用了别人给的文案是把调用的函数的文件也一起放在gorun命令后面执行gorunmain.go......
  • Not Another Linear Algebra Problem 题解
    题意:自己看。首先我们知道我们唯一能找到的题解在hos_lyric的代码里。把它放在这里:(由bikuhiku提供)\[\begin{aligned}&U\subseteq\mathbb{F}_p^n,\text{subspace}\\&a(U):=\#\{p\in\text{Aut}(\mathbb{F}_p^n)|\text{Ker}(p-\text{id})=U\}\\&b(U):=......
  • 小米MIX 2升级Lineage OS 20(Android 13)卡进度0%
    参考lineageoswiki安装时sideload会卡进度0%,电脑终端卡在serving'lineage-20.0-20230608-nightly-chiron-signed.zip'(~0%)手机端卡在Verifyingupdatepackage...wiki步骤基本步骤:#重启到fastbootadbrebootbootloader#连接设备fastbootdevices#刷recoveryf......
  • Newspaper Headline
    非常蛋疼的题,做了俩小时,看题解用了一个半小时给定字符串 s1​ 和 s2​,问至少要用多少个 s1​ 首尾相接拼成新字符串,使得新字符串在删除一些字符后成为 s2​?感谢@barry_wang提供的翻译输入输出样例输入#1复制abcxyz输出#1复制-1输入#2复制abcddabc......