16

I am a new Ubuntu user and am still getting used to the OS. I have been using it primarily for local testing of a site that I am developing. I downloaded the LAMP stack for this.

When I first downloaded Ubuntu, whenever I edited the files I was developing, I needed to enter sudo nano filename. It was not like this for files in my home directory, only for files where I was testing.

When I logged on today, I didn't have to enter sudo, I was able to just enter nano filename and edit the files. I am not sure if I somehow disabled the need for a sudo password, which I would not want to do.

I ran grep root /etc/passwd then grep root /etc/shadow which denied access without entering sudo. When I did, root:! showed. From what I read, that means the account is disabled.

I don't know if that means I disabled the password or if the account is locked and everything is ok.

Is there a way to make sure that I am not running as root and if I currently am how to disable it so I am running only as admin.

Also, why did the sudo password suddenly stop needing to be entered for editing my files?

Thanks for your answers, if my question is unclear, please let me know so that I can try to clarify.

9
  • This isn't a complete answer to your question, but if you want to know who you are logged in as use the whoami command. Use ls -l <file> to list the user which owns a file (to see who can edit it). Side note: to stop root from owning configuration/temporary files in your home directory, you may want to use sudoedit instead of sudo nano; sudoedit works exactly like sudo nano except that it won't let nano write files in your home directory as root.
    – daboross
    Nov 20, 2015 at 4:53
  • Thank you for your answer. Using the ls -l <file> shows that I am the owner of the file with permissions. I assume this is why I can now edit without sudo permission, but am still curios as to why it changed.
    – Chris
    Nov 20, 2015 at 5:00
  • I think the only reason a file would be owned by you is if you created it without sudo. Where exactly on the filesystem is the file owned by you? It might be in a directory which allows editing by all users (the directory would look like drwxrwxrwx in ls), which would allow you to create a file owned by yourself.
    – daboross
    Nov 20, 2015 at 5:03
  • When I opened the folder in the file explorer and checked properties permissions it says that I am the owner. I did an ls -l of the directory in the terminal and it lists me as the owner of most of the files, but root as the owner of some. The files I am the owner of are ones that I overwrote when I copied from another folder. I think what happened is I created those other files elsewhere without sudo which makes me the owner, but the files I created inside the folder still have root ownership. Is my thinking correct on this?
    – Chris
    Nov 20, 2015 at 5:13
  • Yes - that would cause this! When you copy/move a file from one directory to another, chances are you are keeping permissions, and the owner of the file (definitely so if you use a file manager to do this, or you are moving the file). If you want to change the permissions on these files so that they are all owned by root, you can use sudo chown root:root <file> or sudo chown -Rc root:root <directory>. -R is recursive, -c lists all changes. Also check out the answers below from other people, they describe file permissions more in depth if you need that.
    – daboross
    Nov 20, 2015 at 5:18

4 Answers 4

17

If you are using bash (the default), your prompt will tell you if you are acting as root. If it ends in a '$' you are running as a normal user. If it ends in a '#' you are running as root.

Additionally,

whoami

will show who you are.

If you can suddenly edit files that used to require sudo access, my first suspicion would be that you've somehow managed to change the permissions on a file. If you type:

ls -l <filename>

and examine the results, the rwx sections show permissions.

-rw-rw-r--  1 user user     77338 Oct 21 17:59 filename.odt
|\ /\ /\ /
| |  |  +-- permissions for "other": every user on the system.
| |  +----- permissions for "group"
| +-------- permissions for "owner"
+---------- filetype (- is regular file)

If you belong to a group that has write permissions or if "other" has write permissions then you don't need sudo access to write to the file.

You can read an introduction to linux permissions for more details on file permissions.

2
  • 1
    Thanks for your answer, the information about the $ or # running as normal vs root was very helpful.
    – Chris
    Nov 20, 2015 at 5:27
  • whoami prints the name of the user, but it's possible to create a second user named root. (See askubuntu.com/a/836621/1070384) Aug 13, 2020 at 15:54
7

To show you in detail "who you are", you may use the command id:

$ id
uid=1000(alphawolf) gid=1000(alphawolf) groups=1000(alphawolf),4(adm),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),107(netdev),113(lpadmin),128(sambashare),132(wireshark),2000(humans-bc-aw)

As you see, depending on your account the output can get pretty long.

  • It does not only display your user name and ID "uid=1000(alphawolf)",
  • but also the name and ID of your primary group "gid=1000(alphawolf)"
  • and names and IDs of all other groups your account is a member of. This list by default only contains the primary group if you're a normal/restricted user, and a longer list containing at least adm and sudo if you're an admin user.

    groups=1000(alphawolf),4(adm),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),107(netdev),113(lpadmin),128(sambashare),132(wireshark),2000(humans-bc-aw)
    

Now, how can we determine our permissions on a file with this information?

We take the output of ls -l /path/to/file-or-directory, like for example:

$ ls -l /usr/bin/dumpcap
-rwxr-xr-- 1 root wireshark 85632 Aug 16 01:43 /usr/bin/dumpcap*

This file is an executable used by Wireshark to capture network packages with elevated privileges. I chose it because of the different owner and group permissions.
Let's look at the important details:

  • Owner: root
    This is the user owning the file.

  • Group: wireshark This is the user group this file is assigned to.

  • Permission string: -rwxr-xr-- Those are the file permissions for the user, the group members and all others, as described below:

    - rwx r-x r--
    | \ / \ / \ /
    |  V   V   V
    |  |   |    \__ Other permissions: [r] 'r'ead access granted
    |  |   |                           [-] 'w'rite access denied
    |  |   |                           [-] e'x'ecuting denied
    |  |    \__ Group permissions: [r] 'r'ead access granted
    |  |                           [-] 'w'rite access denied
    |  |                           [x] e'x'ecuting granted
    |  \__ Owner permissions: [r] 'r'ead access granted
    |                         [w] 'w'rite access granted
    |                         [x] e'x'ecuting granted
    \__ File type: [-] normal file 
                       (alternatives: 'd'irectory, sym'l'ink,
                       'c'haracter device, 'b'lock device, ...)
    

Now, what could my user alphawolf with the id output above do with this file?

  • I am not the user root who has full read, write and execute permissions.
  • But I am a member of the wireshark group, therefore I have the permissions set for the file's group: I may read the file and execute it, but not modify it (no write access).
  • Others could only read the file, but neither write to it nor execute it.

This will help you to determine you permissions on different system files.

6

In most shells this is possible

if [ $USER = root ] ; then echo Im root; fi

Bash also support this

if [ $UID = 0 ] ; then echo Im root; fi
2
  • $USER expands to the name of the user, but it's possible to create a second user named root. (See askubuntu.com/a/836621/1070384) Aug 13, 2020 at 15:57
  • 1
    In that case the $UID will still work. Recomendation, do not have a second root user. It also makes security harder. Aug 15, 2020 at 7:34
4

You only need to use root to edit files owned by root or other users. Any files that you create or own can be edited by you without using root.

E.g. run the following commands

cd $HOME
touch test.txt
nano test.txt

The above commands navigated to your home directory (your personal area), created a file called test.txt, then entered the file using an editor called nano.

Now if you try:

nano /etc/apt/sources.list

You should be able to see the file but get a warning saying that you cannot edit the file.

enter image description here This is because even though the file is not owned by you, the file has read access for everyone. This can be shown in the picture below:

enter image description here

To easily view the permissions in Ubuntu, just type:

ll /path/to/parent/directory

or alternatively for other systems such as debian

ls -al /path/to/directory

There are lots of great on-line tutorials for reading about user permissions, such as this, that I recommend reading. I quite often see people do silly things such as set everything in /var/www to 777 to get their webserver to "work" because they don't understand linux permissions and thus their servers are less secure.

I am a new ubuntu user...

Good luck and welcome to Linux! Spread the word.

0

You must log in to answer this question.

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