Difference between revisions of "Linux admin notes - users and permissions"

From Helpful
Jump to: navigation, search
m (Created page with "...particularly in the context of filesystems ===Filesystem permission basics=== Most people know the below to some degree, and should. 'nix file permissions are handled with a...")
 
m
Line 1: Line 1:
 +
{{Shellrelated}}
 +
 
...particularly in the context of filesystems
 
...particularly in the context of filesystems
  

Revision as of 15:32, 3 June 2011

Template:Shellrelated

...particularly in the context of filesystems

Filesystem permission basics

Most people know the below to some degree, and should.

'nix file permissions are handled with a system simpler than ACLs. Sometimes this is considerably less bother than ACLs, sometimes it requires a little contortionism, which is probably one major reason ACLs are slowly being introduced into unices here and there.


Each filesystem entry (Meaning anything that can be contained by a directory, including files, other directories, and about half a dozen special things.) has a set of properties. The most interesting are:

  • the user id (ususally shown as the name, as it is looked up on the current computer)
  • the group id (ususally shown as the name, as it is looked up on the current computer)
  • read, write, execute permissions bits for the owning user
  • read, write, execute permissions bits for the owning group
  • read, write, execute permissions bits for other - things that apply for everyone.

You can also get and set these numerically, as explained further down.


Permission bits are shown in various ways. The form form is the output used by
ls -l
. Its output for a regular file could look like:
-rw-r--r--  1 sarah users 2414 Nov 21  2004 afile.txt

The first bit shows permission bits (and more, explained later). You should read this as a special flag/filetype field, followed by three group of rwx, one for each of user, group, and other. For example:

- rw- r-- r--

The permission is present if the letter is present. In this case, sarah can read and write, anyone in the users group can read, and anyone else can also read this file.

'Other' is used for minimal permissions, since it always applies (regardless of what user or group id a visiting user comes along with), so -rw----r-- has a very similar effect in to -rw-r--r--.


Details explained later:

  • setgid and setuid are also be indicated in those letters
  • rights work slightly differently on directories

Changing permissions

When changing permissions on a file there are various ways of describing permissions. One common distinction is whether you specify what should change or whether you specify exactly what the new permission set would be.

There are also different ways of describing each permission, and different operations to the permission bits. Choose the one or two you like.


Common usage:

chmod o-rwx afile.txt            # takes all permissions away from other
chmod +x afile.txt               # gives everyone execute permission
                                 # (short for a+x or the equivalent ugo+x)
chmod ug+w -R dataDir            # Gives user and group write permission


It is technically also possible to combine operations:

chmod a-rwx,u+wr,g+r -R dataDir  # A much-at-once style (not seen used much, but useful)
chmod u=rwx,go=rx dataDir        # Set-to-exactly-this style, in this case rwxr-xr-x

The numeric way sets permission bits as octal numbers, and is the shortest way to set all bits in one go. Where r=4, w=2, x=1:

chmod 0777 somefile              # gives full permission to all, like a=rwx.
chmod 0620 somefile              # sets rw- r-- ---
chmod 0750 somefile              # sets rwx r-x ---

...and any other combination. Most people remember just a few cases, such as 0777 as 'allow all', 600 (owner readwrite) or 644 (owner readwrite, others read-only) for files, 755 or 700 for directories, or such.


The leading zero is usually not necessary for command line utilities, since they know to interpret numbers as octals. In code you will usually need to use them, as otherwise they won't be understood as octal numbers.


Note that execute permissions on a file will include it in command autocompletion in shells. It will also allow execution as a binary, according to the hashbang, or using /bin/sh (as a fallback).


Changing ownership

You can change the user, group and each of these permissions, with chown and chmod respectively:

chown frank:goats afile.txt  #sets user to frank, group to goats
chown :goats afile.txt       # ...or group  
 
chown -R frank dataDir      #recursive: changes for everything in and under the directory


Note that there is a fundamental difference between adding and omitting the colon when specifying only a user and not a group:

#change only owner, not the group
chown frank afile.txt
 
# change owner, and also the group (to the login group for the specified user)
chown frank: afile.txt

Accounts

Account management makes altering whatever files store the details (often at least <ttt>/etc/passwd</tt> (user list) and /etc/group (group list), and usually also the shadow files. (passwd and group used to store the password hash, but since these had to be system-readable, that made brute forcing easy. Shadow files make it easy for only the system to access the password hashes.) See also details on PAM.

Note that these system files are what the user name on a file is based on, and that the file entry itself only stores the UID for this lookup (same goes for groups). File permissions are inherently local to a computer unless those IDs are centralized.


Adding a user: useradd

useradd can create a user and edit an existing user's details. (This may alternatively/additionally be adduser)


The home directory will be set to the configured base directory (usually /home) plus the username - unless you override this with a specific home directory.

You usually want to add the -m option to have the implied home directory actually be created. The contents of a generated home directory will be based on /etc/skel/.


The initial group a user is a part of either a group with the same name or the user, or something like users or students, if you have an admin that likes it that way, or are that admin.


Note that not having a home directory means the account cannot log in, as does not having a valid shell. This can be usefil to e.g. have users for samba to map to, but to not allow them to shell into the same server.

Not having a password set usually also disallows logins, at least remotely(verify).

Changing passwords

You can change your password with
passwd
, and if you're root you can change other people's passwords. On systems that integrate with networked systems like NIS, you may need to use another executable (like
yppasswd
). A plain
passwd
tells you so, or is symlinked to it.


Group management

group logic

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

A user can be a member of more than one group. However, because of the way processes historically work, there are two sides to this:

There is always a current UID and GID. Lazy and older programs may test for only that, while programs can also see the other groups you are a member of. (verify)

To see the effective uid, gid, and group membership, run
id
. For root this can look like:
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),
6(disk),10(wheel),11(floppy),18(audio),20(dialout),26(tape),27(video)

Root is an extreme case, though. For a regular user it is often something simpler, such as:

uid=1004(sarah) gid=100(users) groups=100(users)


To change one's own currently effective group, use
newgrp groupname
. If you are a member, it will switch. If not, it will either deny you, or ask for a password, depending on how the group admin has set up the group.


Note there is a
sg
, analogous to
su
but for groups: it sets a different group for executing a particular command.

Group administration

Create or remove a group:

  • groupadd group
  • groupdel group
  • groupmod
    to change the name, or gid (group ID)

(note that useradd may be set up to create a group with the same name as the user it creates)

Membership:

  • gpasswd -a user group
    to make a user the member of a group
  • gpasswd -d user group
    to remove a user's group membership

Further group management:

  • gpasswd group
    to set the group password.
  • gpasswd -r group
    to remove the password.
  • gpasswd -R group
    disables the ability for non-members to use password login.
This can be done by a group admin and root. Group admins are one or more (non-root) users who are allowed to change group membership. They are added to groups by root, using
gpasswd -A user group
.


Guts you can usually avoid

mode

Programmers may find it interesting that the permission bits mentioned are a subset of the mode, which is a larger integer.

Most things only show the lower so-many bits:

  • permission bits (lowest 9 bits)
  • sticky (S_ISVTX, usually 1000)
  • setgid (S_ISGID, usually 2000)
  • setuid (S_ISUID, usually 4000)

The mode also records the file type (obviously unchangable), is often 32-bit, and may store various OS-specific bits you can only get at via OS-specific libraries.

The file types are generally:

  • named pipe, a fifo (S_IFIFO, 0010000)
  • character special device (S_IFCHR, 0020000)
  • directory (S_IFDIR, 0040000)
  • block special device (S_IFBLK, 0060000)
  • regular file (S_IFREG, 0100000)
  • symbolic link (S_IFLNK, 0120000)
  • socket (S_IFSOCK, 0140000)


You can get the full mode with a stat(), which is what library functions like is_dir() use, but even the command-line stat command masks out some bits unless you specifically ask for the mode - which shows it in hex for some reason, so you'ld have to do something like the following to get it in octal:

$ stat --format %f /tmp | tr [a-f] [A-F] | xargs --replace=@ echo "ibase=16; obase=8; @" | bc
41777

directory permissions, read and execute

In terms of permissions, directories are seen as entires in the directory they sit in, like any other. At a lower level, they are files that record the directory's listing - more exactly, (name,inode) pairs. It is interpreted as a directory because it marked so in its mode.

This makes the following easier to follow and swallow:

  • read rights allow: reading references to fetch file details (verify)
  • write: create, rename, and remove files in it
  • execute rights allow: searching/listing its entries (the names)

At first sight, r and x are only useful in combination, and o+w is scary.


Instead of reading the below, you can just remember that in most any case, you want to assign/remove both r and x.


Having x withour r means you can't list the directory itself, but this is not recursive: if permissions on contained files allow and you can guess their names, you can read them fine, and if they're directories, change to them fine.

For example, if you have:

  • a directory ./1 with d-wx--x--x (result of chmod -r ./1)
  • a directory ./1/2 with drwxr-xr-x (regular permissions)
  • a file ./1/2/contents.txt

Then:

$ ls 1
ls: cannot open directory 1: Permission denied
$ ls 1/2
contents.txt
$ cat 1/2/contents.txt
(whatever that file contains)


Having r without x means you can list that directory's entries and see their names, but not see file details (specifically, stat() them) or read contents, which therefore also disallows descending into directories.

For example, if you have

  • a directory ./1 with drw-r--r-- (result of chmod -x ./1)
  • a directory ./1/2 with drwxr-xr-x (regular permissions, same as before)
  • a file ./1/2/contents.txt
$ ls -l 1
ls: cannot access 1/2: Permission denied
total 0
?????????? ? ? ? ?            ? 2
$ cd 1
bash: cd: 1: Permission denied
$ ls  -l 1/2
ls: cannot access 1/2: Permission denied
$ cat 1/2/contents.txt
(whatever that file contains)
cat: 1/2/contents.txt: Permission denied


directory permissions, write

o+w permission is scary since it means that anyone can alter directory entries, even if those entries don't list the user as their owner.

The same applies to g+w if the group is something like 'users' or 'students' and that includes people you may not want to trust implicitly.


Sometimes you may want a directory which can be used to give each other files, or a generally shared directory.


directory permissions, sticky

This is where the sticky bit comes in handy: when sticky is set on a directory, only the directory's owner and each entry's actual owner can rename and remove entries (anyone can create files/directories). This means you can do most anything other than accidentally or purposefully mess with files and directories you don't own. /tmp is often a stickied directory for this reason.

The sticky bit was named for its historical use on regular files, where it was a signal to the OS to keep this around in memory (or in local rather than remote storage) as it would probably be used regularly. Modern caching and drive speed has made this mostly obsolete. (I'm not sure whether modern kernels still listen to the sticky bit(verify))

setuid, setgid, and sticky on files

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

Permissions have been extended by:

  • setuid (SUID), shown in the user's execute position
  • setgid (SGID), shown in the group's execute position
  • sticky bit, shown in other's execute position

They are probably shown there for some compatibility/parsability reason; they are separate bits, octally 4000, 2000 and 1000, respectively.

  • In chmod, use u+s, g+s, o+t to change these.
  • They show up as those same letters in place of the x field. They are capitals when the x permission itself is missing.


Setuid and setgid are organisational security tools.

Any program runs with an effective user and group, which under regular circumstances is the user that executes the executable. This also means the program can read exactly what the user can.

This is sensible and simple, but doesn't serve all cases. For example, svgalib needs root access to access graphics hardware directly, which would mean anything that needs svgalib can only be run by root (or using sudo).

With SUID and SGID, once a program starts executing it takes the user ID or group ID from the file, which implies different access. Sometimes more (such as setuid root, e.g. for svgalib programs), sometimes less (e.g. starting a service as root but having it run as an unprivileged user).

(These (setuid and setgid, and the related seteuid and setegid) are system calls which you can generally use, it's just that the file permission flag is simple and serves many purposes.)

Some actions that users may do but are technically low-level or restricted are often setuid or setgid (or allowed via sudo). This includes passwd, mount, umount, ping, and su.


Since you can't give away ownership (to, say, root), this isn't directly a security problem. However, you and particularly root should be careful of what they take ownership of; whatever files are owned by root, generally accessible, o+x and setuid or setgid will effectively run as root when executed by anyone, and therefore be able to do anything. There are a few fairly simple ways around this, such as to disallow directory access, which can be done by moving files you take ownership of to /root, when it has something like 0700 permissions - like it usually does.


The letter summary for the non-permission part of a mode (rwx... thing in a long listing) isn't complete. This first character can be any of:

  • - regular file
  • d directory
  • l symbolic link (symlink)
  • p named pipe
  • s socket
  • c character device
  • b block device

Then again, you usually only need to care about regular files, directories, and the occasional symlink.

umask

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

Files you create (or that programs you run create) have certain default permissions, or rather, have certain permissions removed by default, according to the umask.


For example, if the umask is 0022:

  • 0022 itself means --- -w- -w-
  • ...which are the things that are removed from permissions
  • in this case meaning that on newly created files, write permissions will never appear for group or other


This makes it safer to let programs create files. Note that you can always set permissions more permissive later if you need to.


The more paranoid would probably set their umask to 0027 (no write on group, nothing on other) or 0077 (depending on whether users do or don't get their own group).

ACLs

On newer systems, people are starting to use ACLs, in which case files and directories can have an ACL to them.

ls
shows presence of ACLs with a + after the permissions string, like in
drwxr-xr-x+  2 root root      6 Dec  3  2003 opt
dr-xr-xr-x  43 root root      0 Nov 14 07:28 proc

I've not used these yet myself, but you can apparently use getfacl and setfacl to get and set these.


This article is an attempt at a decent overview, introduction, summary, or such.

It may currently just contain a mess of information. If it is likely to keep sucking it will probably be removed later.

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)


Running with different credentials / reduced permissions

This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)

Know the permission details above.

See also su, sudo, sudoers


SUID, SGID


chroot jails


The graphical run-as-root wrapper is
gksu
, or
kdesu
(GNOME and KDE variants). May be injected into the command when the system can guess you'll need it (particularly in Alt-F2 style launching).