blogger templates blogger widgets
Showing posts with label user and groups. Show all posts
Showing posts with label user and groups. Show all posts
This is part of a list of blog posts.
To browse the contents go to

Users and Groups

useradd [options] username

The useradd command will update system files (/etc/passwd and /etc/shadow file with password) and may also create the new user's home directory and copy initial files.
Options:
- e {yyyy-mm-dd}
option to set account disable date.
- f {days}
option to set default password expiry.
If 0 is specified, the account is disabled immediately after the password expires. If -1 is specified, the account is not be disabled after the password expires.
- s {shell's absolute path location}
option to associate a starting shell program.
- p xxxxxx
option to speficy the password in the same line.
- m
option to create the user's home directory.
Example:
To add a new user with starting shell as /bin/bash,password of xxxx, home directory of eipe, create home directory (if it doesn't exists) and a login name of tom.
$ sudo useradd -pthomas -d/home/tom -m tom

The default values for useradd command are specified in /etc/default/useradd.


su - Switch User

To switch to another user, we use the su command. This is most commonly used to switch to the root account.
Example: To switch to root account
$su
(When asked for password, enter root's password)
Example: To switch to the user 'john'
$su john
(Enter john's password)
To return to original user, enter exit.

If you are not specifying password at the time of creation (when running useradd -p password), it disables the password on that account. The only way to login into the account is by su-ing from root.
A regular user can log into that account only after the root has set the password for that account.
Preferred method to create/manage users in Ubuntu is the Users & Groups utility in System > Administration.


groupadd newgroupname

The groupadd command is used to create a new group. Every user is a member of a primary group and may or may not be members of many secondary groups.


id

It prints the real and effective user ids and the groups and it's ids.
onie@onie-system:~$ id
uid=1000(onie) gid=1000(onie) groups=1000(onie),4(adm),20(dialout),24(cdrom),46(plugdev),111(lpadmin),119(admin),122(sambashare)
The first group is the primary group and it has the same name as my user id. We can change group names using groupmod command. (it's explained below).


userdel username

It's used to delete a user.
-r
option to remove his home directory.


groupdel groupname

It's used to delete a group.


passwd

It's used to change the user's password.
A username needs to be specified to change it for another user. (only allowed by root).


Where are user account information stored?

User names and primary groups are stored in /etc/passwd. This file can be directly edited using the 'vi' editor, although this is not recommended.
Sample entry from my passwd file is
onie:x:1000:1000:John Eipe,,,:/home/onie:/bin/bash

Format of each entry is as follows:
- User (name normally all lower case)
- Password (encrypted - only contains the letter 'x')
- User ID (a unique number of each user)
- Primary Group ID
- Comment (Normally the person's full name)
- Home directory (normally /home/
- Default shell (normally /bin/bash)
Each field is separated by a colon.


So where are passwords stored?

Passwords for each user are stored in /etc/shadow. This file should only be changed using the passwd command.


Where are group information stored?

Groups information is stored in /etc/group.
This file can be directly edited using the 'vi' editor. A Sample entry from my group file is
egroup:x:1000:

Format of each entry is:
- Group name
- Group password (hardly ever used)
- Group ID
- User names (separated by commas)
Each field is separated by a colon.


What is the default configuration for a new user account if it's not specified by the creator?

Say, the root creates a new user with no options.
#useradd tom

So, is the password set? Home directory created?
Well, it depends on your default configuration for useradd program which is stored in /etc/default/useradd.


How to customize the default files/directories that are created for every account?

When a new user is created, the default files and directories that are created are stored in /etc/skel.
This directory can be modified to fit your needs. Modifications only effect new users and does not change anything for existing users.
For example: In ubuntu (10.10) when a new user is created. We find the below files created automatically.
eipe@eipe-system:/etc/skel$ ls -a
. .. .bash_logout .bashrc examples.desktop .profile

But when we use GUI (nautilus) we don't see this, instead only a folder “Examples” with another folder “Ubuntu_Free_culture_showcase” in it.

Now let's try to read examples.desktop.

riya@eipe-system:~$ cat examples.desktop
[Desktop Entry]
Version=1.0
Type=Link
Name=Examples
Comment=Example content for Ubuntu
URL=file:///usr/share/example-content/
Icon=folder
X-Ubuntu-Gettext-Domain=example-content

we find that it's neither a hard-link nor a soft link but a special type of file used by the desktop environment programs like GNOME/KDE called “desktop entries”.

A desktop entry file is a configuration file that provides information about how a particular program is to be launched, how it appears in menus, etc. The desktop entry file specifies the details for the item such as a name, a command to run, an icon, and so on. It also contains keywords which determine the location of the item in the menu hierarchy.

Desktop entry files must reside in the $XDG_DATA_DIRS/applications directory and must have a .desktop file extension. If $XDG_DATA_DIRS1 is not set, then the default path is /usr/share is used.

To read more about creating/modifying desktop entries: Desktop Entry Specification


usermod -l new old

It's used to change user names.
It's not very simple to change user names. The root or the sudo user who is changing the user name needs to manually rename the old user's home folder. Change ownership of that data. Also add the user to sudoers list, and so on.


usermod -a -G groupname username#1.....

to add users to a group.


groupmod

to make modificatons to a group.
groupmod -n new_group_name old_group_name
is used to rename a group name.
I changed my group name onie to egroup.
$ sudo groupmod -n egroup onie
$ id
uid=1000(onie) gid=1000(egroup) groups=1000(egroup),4(adm),20(dialout),24(cdrom),46(plugdev),111(lpadmin),119(admin),122(sambashare)

In Debian systems, there is a much more interactive way of creating user and group accounts – adduser and addgroup programs.


adduser and addgroup

Adds users and groups to the system according to
command line options and configuration information in
/etc/adduser.conf. Use man pages to more info.

Modes and Permissions - Advanced

umask

When a new file is created on a Unix-like system, its permissions are determined from the umask of the process that created it. It denotes what permissions needs to be removed by means of masking the binary values (of the given octal value) with the default value(value when umask is not used).
eipe@eipe-system:~/temp$ umask
0022

the default value of umask changes from OS to OS. In Ubuntu it's 0022. First, let's see what happens if we turn mask to all 0s. When umask is given value 0000 it's equivalent to turning it off.
eipe@eipe-system:~/temp$ umask 0000
eipe@eipe-system:~/temp$ umask
0000

Let's now create a file and see what it's permissions are.
eipe@eipe-system:~/temp$ > myfile
eipe@eipe-system:~/temp$ ls -l
total 0
-rw-rw-rw- 1 eipe eipe 0 2010-12-10 21:57 myfile

The default permission when no masking is used is read and write for user, group and everyone.
In binay notation it's 110 110 110.

Now let's give masking a value.
eipe@eipe-system:~/temp$ umask 0022
eipe@eipe-system:~/temp$ > myfile
eipe@eipe-system:~/temp$ ls -l
total 0
-rw-r--r-- 1 eipe eipe 0 2010-12-10 22:00 myfile

masking is actually an XOR operation of binary values. (0022 = 000 000 010 010)
1 and 0 = 1 ; 1 and 1 = 0 ; 0 and 0 = 0

000 110 110 110
000 000 010 010
-------------------
000 110 100 100
-------------------

The first 3 bits or the first octal number is used for special settings. They are used for setuid/setgid/sticky bit.

For example consider the below scenario:

Let's create a new user "tom".
It the group names (also known as primary groups) aren't changed, the default names are same as the user names. i.e, for eipe the group name is "eipe" and for tom it is "tom".

Let's change the group names to avoid confusion.
eipe@eipe-system:~$ groupmod -n eipe egroup
eipe@eipe-system:~$ groupmod -n tom tgroup

(we changed the default group names "eipe" and "tom" to "egroup" and "tgroup").

Let's create a new group "dev".
eipe@eipe-system:~$ groupadd dev

Add users to this group:
eipe@eipe-system:~$ usermod -a -G dev eipe tom


Let's check the individual user ID's.
eipe@eipe-system:~$ id eipe
uid=1000(eipe) gid=1000(egroup) groups=1000(egroup),4(adm),20(dialout),24(cdrom),46(plugdev),111(lpadmin),119(admin),122(sambashare),1002(dev)

eipe@eipe-system:~$ id tom
uid=1001(tom) gid=1001(tgroup) groups=1001(tgroup),1002(dev)


Note:
All user account related information are stored in
/etc/passwd – user account details
/etc/shadow – user password details
/etc/group – group account details

A sample entry in /etc/group

egroup:x:1000:
tgroup:x:1001:
dev:x:1002:eipe,tom

groups [username]

To list the groups the user (given by the username) has membership.

eipe@eipe-system:~/share$ groups
egroup adm dialout cdrom plugdev lpadmin admin sambashare dev

eipe@eipe-system:~/share$ groups tom
tom : tgroup dev


members groupname

To list the members of a particular group.
eipe@eipe-system:~/share$ members dev
eipe tom




Let's consider a scenario.

I(eipe) create's a folder “share” that needs to be shared among other users.
eipe@eipe-system:~$ mkdir share
eipe@eipe-system:~$ ls -l | grep share
drwxr-xr-x 2 eipe egroup 4096 2010-12-12 15:01 share


note that we need the folder to be group-executable so change the permissions if necessary for group to r-x.

Then I create a script file called “lister.sh” in share.
eipe@eipe-system:~/share$ cat>lister.sh
ls -la
eipe@eipe-system:~/share$ ./lister.sh
bash: ./lister.sh: Permission denied


This is because any file is not executable by default.
eipe@eipe-system:~/share$ ls -l
total 4
-rw-r--r-- 1 eipe egroup 7 2010-12-12 16:39 lister.sh

Since I'm planning to make it shareable I need to make it executable for both me (eipe) and my groups.
eipe@eipe-system:~/share$ chmod ug+x lister.sh
eipe@eipe-system:~/share$ ./lister.sh
total 12
drwxr-xr-x 2 eipe egroup 4096 2010-12-12 16:39 .
drwxr-xr-x 53 eipe egroup 4096 2010-12-12 16:39 ..
-rwxr-xr-- 1 eipe egroup 7 2010-12-12 16:39 lister.sh

Now tom logs in.
eipe@eipe-system:~$ su tom
Password:
tom@eipe-system:/home/eipe$ cd share
tom@eipe-system:/home/eipe/share$ ./lister.sh
bash: ./lister.sh: Permission denied

this is because the group associated with the file is “egroup” and not “dev”. Every file belongs to a user and a group. The group is usually the group of the user who created the file.

There are 2 solutions:

Solution 1:

Since tom is given read access to the file he could copy it to another location and then run that file.
tom@eipe-system:/home/eipe/share$ cp lister.sh /home/tom/clister.sh
tom@eipe-system:/home/eipe/share$ cd /home/tom
tom@eipe-system:~$ ls -l
total 8
-rwxr-xr-- 1 tom tgroup 7 2010-12-12 17:08 clister.sh
-rw-r--r-- 1 tom tgroup 179 2010-12-11 09:40 examples.desktop
tom@eipe-system:~$ ./clister.sh
AND IT WORKS...

Note that tom had to change the directory, since he was in eipe's home folder, where he cannot create files.

Solution 2:

Changing the group ownership of a file.
chgrp groupto filename
-r option is used to change for all files in a directory.
eipe@eipe-system:~/share$ chgrp dev lister.sh
chgrp: changing group of `lister.sh': Operation not permitted

this is because after creating the new group "dev" I didn't restart the system.

After relogging,
eipe@eipe-system:~/share$ chgrp dev lister.sh
eipe@eipe-system:~/share$ ls -l
total 4
-rwxr-xr-- 1 eipe dev 7 2010-12-12 16:39 lister.sh

Now tom logs in and he can execute the file.
eipe@eipe-system:~/share$ su tom
Password:
tom@eipe-system:/home/eipe/share$ ./lister.sh
total 12
drwxr-xr-x 2 eipe egroup 4096 2010-12-12 16:39 .
drwxr-xr-x 54 eipe egroup 4096 2010-12-12 18:09 ..
-rwxr-xr-- 1 eipe dev 7 2010-12-12 16:39 lister.sh


Continue Reading here