9

Can you tell me what the difference is between cat and sudo cat?

All I know so far is that cat is used for displaying contents of file and concatenation.

Braiam
  • 67,791
  • 32
  • 179
  • 269
Edison
  • 367
  • 1
  • 6
  • 16

3 Answers3

25

For a little humour I would say that cat is an animal and sudo cat is a feline with superpowers. :D

sudo is a command that you use to obtain root privileges. root is a special user that manages the machine, and for this he/she has superpowers. For example, if there is a file that only root can see its contents, and you are logged in as a normal user, you can use

$ sudo cat name_of_the_file

to read it. Also if there is a program that only root can run, like the reboot command:

$ reboot
warning: must be root!
$ sudo reboot
rebooting...........

THE CATCH IS: you must be specially (and manually) assigned by root to have permission to use sudo. The permission is given in a file called /etc/sudoers. In Ubuntu, the first user, the one created during install, is automatically a sudoer. But the subsequent users are not. You have to add them manually to the group sudo whose members are allowed to use the command sudo.

By the way, /etc/sudoers is a file that only root can see. So if you do

$ cat /etc/sudoers

you will not be able to see its contents. But if you do:

$ sudo cat /etc/sudoers

you are good.

Hope this helps.

guntbert
  • 13,134
Henrique
  • 697
  • 7
  • 15
  • 2
    +1 for explaining cat with an impressive ways. Indeed an amazingly constructed answer. :) – AzkerM Mar 05 '14 at 18:03
  • Surely sudo cat is something masquerading as a cat? :oP – Emmet Mar 05 '14 at 19:00
  • 8
    I can't believe nobody has yet posted a link to http://xkcd.com/149/ – tobyink Mar 05 '14 at 23:00
  • Note for all newcomers: Do not directly edit /etc/sudoers. Any changes to this file must be made via visudo. You can grant administrator privilege to users from the System Settings > User Accounts; no need to manually edit /etc/sudoers. – Paddy Landau Mar 12 '14 at 13:14
  • Will I go to jail for editing /etc/sudoers directly? I really loathe visudo (as I despise vi/vim in general). Didn't know about the sudo group, though, thanks. – Henrique May 12 '14 at 01:11
  • @Henrique The point is you can get locked out of sudo if you cause a syntax error in /etc/sudoers and then you can't fix it because you can't use sudo. visudo won't let you save the file if there is a syntax error. Also you can set visudo to editor of your choice with sudo update-alternatives --config editor – curusarn Feb 07 '18 at 09:29
  • Thanks for the clarification, never knew that. But, still, if you know the guy that owns the root password, you can Always cry for help. – Henrique Feb 01 '19 at 21:36
  • 1
    Just as a personal anecdote, some months after this thread I was locked out of the system for neglecting to use visudo, and I instantly remembered @curusarn 's warning. – Henrique Mar 23 '20 at 17:58
6

Cat is a standard unix utiliy and a most frequently used command which concatenate files and print on the standard output.

You may open a terminal (press CTRL+ATL+T) & type man cat to know more about the command and its usage.

Further, the difference between cat & using sudo cat;

  • cat - Frequently & the standard command in use to print an output
  • sudo cat - Which prints an output with root privilege. This is mostly needed when a file doesn't have read access for certain user/users but not limited to root user.

Example;

-rw------- 2 root root 4096 996 Feb  6 20:39 log.txt

Above seen is a file which only a root user (or a user who's within root group) can read/write. In such situation you will need to use sudo cat filename to print the output.

Assume it helped you to understand more.!

AzkerM
  • 10,270
  • 6
  • 32
  • 51
2

cat is used to read a file; sudo is used for super user privileges. So sudo cat means read the file with super user (that is, root) privileges.

TRiG
  • 1,910
Qasim
  • 22,092