12

Often a question can only be answered with a command that must be run as root. Depending on your distribution of choice you might be used to doing this with sudo. However, it is probably undesirable to post the command with sudo attached since someone who is looking for a quick fix might just run the command (which might not be what they need to run) without actually looking at the command and what it does. Additionally, sudo isn't necessarily used on the user's system. Should we have a conventional way for denoting when something needs to be run as root?

Here are some possible conventions:

  • Place a # before the command, denoting the common character used at the root prompt. The problem with this is that many who are new to unix might not recognize this.
  • Placeing something like "(as root):" before the command. The problem with this (and the above option) is that the user may not know how to run something as root.

Any ideas?

  • 1
    If the user doesn't recognize the #, at least copy/pasting # something won't cause any system damage. Besides, this is easy. Some new syntax may mostly be unused. – Kevin Cantu Oct 13 '10 at 18:05
5

I think we should have a convention for this and that we should try to implement both of your suggestions.

The first one being there for purely conventional reasons and for the regulars... However, using '#' might not be most suitable because it could also indicate a commented line in a bash piece of code.

The second one for new people, accompanied with a link (run this as root) to a google search or another question that addresses running stuff as root.

update: I created a question that you can link to for this purpose.

  • just my two cents – Stefan Oct 13 '10 at 15:00
  • This seems the best way; I put an example in my answer – Michael Mrozek Oct 13 '10 at 15:15
  • Have we any possibility to include "run this as root" automatically by some criteria chosen by us? – echox Oct 13 '10 at 15:22
  • 1
    I really like the idea of having some way to include "run this as root" easily. Some special markup perhaps? – Steven D Oct 13 '10 at 15:33
  • official markup for code/commands seems \n, 4 spaces for each line, \n ... What about something like \n, root:, 4 spaces for each line, \n ? Do the mod tools have any possibilities to include such "templates" or would this be a feature request for the SE software? – echox Oct 13 '10 at 16:12
  • @echox I'm generally not a fan of that, because it's impossible to get everyone on the site to use it, so it doesn't do anything in the way of enforcing consistency. It's also currently not supported in the engine, but we can always try to get that changed if the community thinks it's a good idea. We were talking with the AU mods a few days ago about it, they want the same feature so they can have a template for including package installation links; I'll check in with them and see if they found out anything – Michael Mrozek Oct 13 '10 at 17:02
  • @echox I talked to AU; they posted this feature request yesterday about it (an apparent duplicate of a feature request from a few months ago). They're also going to bring it up tomorrow in the network-wide mod/dev meeting to see what the chances are of actually getting it added, so I'll find out then. I'm still not totally convinced it's a good idea, but at least we'll know if it's possible :) – Michael Mrozek Oct 13 '10 at 17:26
  • I'm also not really satisfied by this solution but right now I don't have another solution. Sure, we could have a look at # in code scripts, but this would maybe bring some unwanted side effects? – echox Oct 14 '10 at 9:42
  • 1
    In my mind, the unix community already has a convention for this, i.e. the '#' before a command. If this site is about anything, it should be about helping people who are new to unix learn the existing conventions. – gabe. Oct 22 '10 at 4:02
6

I'm not sure if there really is a need for a convention like this.

If root privileges are required, in most of the cases you will get an appropriate error message. Its often also clear from the context of the command/question. Even if you stuck sometime with a "command not found" error because /sbin isn't in your path, its likely that you won't repeat that fault.

However, it is probably undesirable to post the command with sudo attached since someone who is looking for a quick fix might just run the command (which might not be what they need to run) without actually looking at the command and what it does.

Nobody should never ever run any commands without having a clue what they are doing, especially if they are prefixed with sudo. If this happens and somebody breaks his system he learned a important lesson.

sudo and root are the essential basic concept of linux/unix and I don't see the point to support someone who didn't do his homework about that.

If we decide we need a convention, I really like the first one (append a # to the shell).

3

I'm definitely not a fan of including sudo, I think it gets people in the habit of using it without knowing what it is, and it's not part of the command -- if you need to run X as root, the command is X; sudo X is just one way to run it as root. I would prefer using # to explicitly saying as root (I probably should've used # instead of $ on this question), but you're right that some people might not realize what it means, so I suppose something like this will work:

To solve your problem, run the following as root:

# ./do-stuff-as-root
  • @echox Oh. I actually hadn't noticed that yet, I was in the middle of writing this and got distracted; sounds like Stefan had the same idea as me but 15 minutes earlier – Michael Mrozek Oct 13 '10 at 15:14
  • mm... wait a second! I just had this horrible twist in my gut when I realised that '#' indicates a comment!! This is bound to confuse the new.... – Stefan Oct 13 '10 at 15:59
  • 1
    This is probably by design. Copy/paste a line like # break stuff and the shell still doesn't try anything funny. – Kevin Cantu Oct 13 '10 at 18:03
  • @Kevin ...but the shell doesn't try anything at all :). Including running ./do-stuff-as-root; to someone who doesn't know what they're doing it looks like the command finished silently. I don't know if it would be a problem or not; maybe we should just use $ instead. I don't think it will be that big of a deal, honestly; I think most people will know what it means or be able to figure it out pretty fast – Michael Mrozek Oct 13 '10 at 18:06
0

This answer exists to list suggested improvements to the canonical answer to “How do I run a command as root”. Please add todo items here and remove them when done. Discuss the current contents and planned changes in the comments here or on the unix.se chat.

  • sudo

    • Older versions of sudo didn't have -e or -i.
  • su

    • Are there OS-dependent su options worth mentioning?
    • What variants use the wheel group?
  • other programs

    • calife: give a usage example; website link; where is it found?
    • op: give a usage example; website link; where is it found?
    • super: give a usage example; website link; where is it found?
    • kstuss: give a usage example; is it in fact a wrapper around su?
    • beesu: give a usage example; website link
  • Running X programs

    • There are frontends like gksu and gksudo that do everything automatically, but they're not available everywhere. Discuss the general case (there might already be something on unix.se or superuser).
  • Editing files

    • briefly discuss vipw and friends on various OSes (e.g. mention OSes where the password database is generated by vipw and won't be updated if you just edit /etc/passwd)
  • single user mode

    • Should we include a section on single user mode? How detailed should it be with regards to how to edit your kernel parameters via grub?
  • There's already a lot of material, so I think information for programmers and security analyses should not be included in this answer. In particular I've removed PolicyKit, which I think is irrelevant in the context of this question. – Gilles Oct 13 '10 at 23:52
  • what about sux? – Stefan Oct 14 '10 at 8:30
  • is there anything special about going into root at single user mode? – Stefan Oct 14 '10 at 8:32
  • See this Server Fault Q/A for a more legible example of a question to rule them all. – Gilles Feb 21 '11 at 23:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .