Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have been using the terminal for a while now and sometimes (actually, a lot of times) I use a command, like:

cp /etc/test ~/testFolder/

and I get this error:

cp: cannot open `/etc/test` for reading: Permission denied

That's the moment I remember that I forgot the sudo command before it, so I press Up arrow and then CTRL+A to go to the beggining of the line and insert a sudo there:

sudo cp /etc/test ~/testFolder/

There are even worse scenarios where you use vim to edit a file, you forget sudo command and don't see the warning when the program starts, you edit the entire file and in the end, you can't save it!

When you realize you're doing it so many times, it becomes annoying, specially if you're like me and used to type commands without the sudo. There is a way to solve this problem, when you start the terminal, just type sudo su to use commands as root user, but some say it's more likely to make mistakes that you'll regret forever.

My question is, what's the best practice for executing commands as root? Using sudo and missing it everytime or automatically use sudo bash and having the risk of destroying the entire system?

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

Relax. Firstly, rm -rf * or anything such like run as a regular user in your home directory is equally catastrophic. System can easily be restored, but it's your data that matters. In the fifteen years or so that I use Linux I had no major disaster that could have been avoided by not using root.

(Well, if you are a sysadmin, especially on a production server, then it is another matter. Just clench your teeth and do it properly; see the horror story of @jmartin2279 in another answer to this question).

Secondly, if you have a different prompt (by default root prompt ends with a hash, #, but you can make it colorful), it will remind you that you are not running as a regular user.

Thirdly, you are an intelligent human being, so you do regular backups. You do, don't you? You know what they say: there are two types of people, those who do backups and those who will.

Finally, get off my lawn, in the olde days we had no stinking su or sudo, we were memorizing passwords of twenty random characters every month and we LIKED IT! (and sometimes, we had half an hour chat over a single command line before pressing "enter")

However, vim always warns you that you are editing a read only file ("/etc/passwd" [readonly] in the status line). Also, if you cannot write to a file, you can always :w /tmp/whatever.

share|improve this answer
add comment

Best practice is to only use root when you need it. by using sudo su, you are indeed giving the devil access to the kingdom. it was annoying to me at first, but remembering to use sudo in front of a command has saved me from making massive mistakes. I have had an experience where using sudo su has ruined everything, in a work environment.

Story time

We where using a CentOS server to run an application. the server was old, and had been turned into a VM because its hardware was dead/dying. We had a raid corruption occur and the VM was damaged. our backups were damaged to so only hope to recover data was mount VM and try to pull it all. I used sudo su and got to work. i recovered about 10% when i accidentally ran the wrong command, it wiped the database and we lost the application and all data. I have never run sudo su again, and I won't. lesson learned the hard way.

share|improve this answer
    
(+1) for the story –  Evandro Silva Oct 17 '12 at 19:23
    
You mean, prepending sudo to your incorrect command would have somehow corrected it? –  January Oct 17 '12 at 19:30
1  
no, but if I had to review my command before typing away in root mode, I would have noticed the mistake. I always type my command, review it, then add sudo at the front before running it. obviously not evertime, but when it's serious business, yes –  jmartin2279 Oct 17 '12 at 19:42
3  
So you mean to say, you have been working during a major failure as a root and you have not been reviewing your commands before typing away? Come on, of course you were. Sudo doesn't make you review commands more than working as root. But what is worse, it works in the history environment of the user, which can be quite dangerous, especially if you use shell history substitutions (but not only because of that). Moreover, automatically prepending almost every command with a sudo can also easily become a habit. –  January Oct 17 '12 at 22:23
1  
P.S. Maybe someone is reminded of the danger of running superuser commands by the colors of the root terminal, and another person achieves the same by typing sudo. But the danger of running in the users environment is real (that is why there is an - option to su). –  January Oct 17 '12 at 22:25
add comment

Back in the days before I used a sudo system (read: ancient Red Hat), I used several techniques to remind me when I was running as root: a red prompt, "root" in the title bar, etc. These days I run sudo and have no prompt change. I've never ever had a problem from operating as root that would have been solved by having to prepend sudo to a command.

I've got a shortcut for editing files. I've got a bash script similar to the following in ~/bin/vi (and ~/bin is the first entry in my $PATH):

#!/bin/bash
if [[ ! -w "$1" ]]; then
  exec sudo vim "$@"
else
  exec vim "$@"
fi

I'm typing this from memory, so beware of bugs. The idea is that if the first file named I'd un-writeable then it automatically uses sudo. Obviously this approach isn't perfect, but it's been good enough for a number of years.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.