Chapter 3: Ownership and Permissions

3.1 whoami

Ah the big existential questions...

whoami

3.2 $HOME

the variable $HOME stores the home directory for the current user

echo $HOME

3.3 Unix Groups

Really applies to shared servers.

group

Returns a list of groups you belong to.

MacOS users belong to the "staff" group by default.

3.4 chown - change ownership

Change file and directory ownership for user and group

chown user:group filename
 
chown user:group directoryname

Change file and directory ownership for user

chown user filename
 
chown user directoryname

Change file and directory ownership for group

chown group filename
 
chown group directoryname

To change ownership of a directory and its contents recursively:

chown -R user:group directoryname

3.5 chmod - change permissions

3.5.1 Alpha Notation

Permissions can be read in the first 10 characters of an ls -la listing

This permission notation is called Alpha notation.

1st character indicates the type of file

d = directory

- = file

l = symlink

The remaing 9 characters indicate permissions. There are three sets of triplets. One set of triplets each for: "user", "group" and "other".

Each triplet gives the permission for :"read", "write" and "execute" (r w x)

Execute on a file means be able to run the file.

Execute on a directory means be able to search inside it.

Setting permissions using alpha notation

chmod mode filename

chmod stands for "change mode". What is mode? First we use letters to represent the User, group and other...

  • u = user

  • g = group

  • o = other

So...

chmod ugo=rwx somefile.txt

gives read, write, and execute permissions to the user, and the group and other.

3.5.1.1 Examples

1) Give the user the read, write and execute permission, give the group the read and write permission and give other the read permission for the file called some file.txt

chmod u=rwx, g=rw, o=r somefile.txt

2) To keep permissions as they are and just add the write permission to user and group

chmod ug+w somefile.txt

or take away the write permsission from other

chmod o-w somefile.txt

Instead of typing ugo all the time you can substitute "a" for "all"

To change permissions recursively into a directory use -R

To add the write permission for the group to all files in the test directory:

chmod -R g+w test

3.5.2 Octal Notation

Setting permissions using Octal notation

  • r = 4
  • w = 2
  • x = 1

replace r, w, x with numbers and add them up

Example 1) The User, Group and Other all have read, write and execute permissions.

  • user has r,w,x => 4+2+1 = 7
  • group has r,w,x => 4+2+1 = 7
  • other has r,w,x => 4+2+1 = 7
chmod 777 filename

Example 2) The User has all permissions, Group has read and write and Other has the read permission.

  • user has r,w,x => 4+2+1 = 7
  • group has r,w => 4+2 = 6
  • other has r => 4
chmod 764 filename

Example 3) The user has r,w,x everyone else has r,w

  • user has r,w,x => 4+2+1 = 7
  • group has r,x => 4+1 = 5
  • other has r,x => 4+1 = 5
chmod 755 filename

3.6 The Root User

The root user is a superuser that can do anything on a unix system. Not bound by any permissions we set up or change

The superuser is the user that installed the unix system and then created the first user account.

3.7 Sudo

Root is disabled on mac by default. But you can temporarily become the superuser.

sudo stands for:

  • s = substitute
  • u = user
  • do = execute something

This enables you to do something as root user

sudo whoami

Running the whoami with sudo tells you that you are temporarily the "root" user

To switch to another user:

sudo -u yourname whoami

now you are "yourname"

As the root user you can exectute any command and accomplish things you would normally be restricted from doing

sudo make me a sandwich

Not everyone can become "root". Only users who have admin status.

In OSX that is something you set up in preferences.