blogger templates blogger widgets
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.

No comments:

Post a Comment