Saturday, May 4, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 5042  / 3 Years ago, sat, july 3, 2021, 6:17:15


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?


More From » command-line

 Answers
1

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.


[#34916] Sunday, July 4, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
suitman

Total Points: 487
Total Questions: 105
Total Answers: 98

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
;