Managing User Account
We have three primary files in /etc/ directory that are related to user account.
/etc/passwd file stores essential information, which is required during login i.e. user account information, a list of the system's accounts, giving for each account some useful information like user ID, group ID, home directory, shell, etc. The /etc/passwd contains one entry per line for each user (or user account) of the system. All fields are separated by a colon (:) symbol. Total seven fields:
root:x:0:0:root:/root:/bin/bash jenkins:x:120:128:Jenkins,,,:/var/lib/jenkins:/bin/bash tomcat7:x:121:129::/usr/share/tomcat7:/bin/false k:x:1000:1000:k,,,:/home/k:/bin/bash
- Username
- Password
- User ID (UID): Each user must be assigned a user ID (UID). UID 0 (zero) is reserved for root and UIDs 1-99 are reserved for other predefined accounts. Further UID 100-999 are reserved by system for administrative and system accounts/groups.
- Group ID (GID): The primary group ID (stored in /etc/group file)
- User ID Info: The comment field. It allow usu to add extra information about the users such as user's full name, phone number etc. This field use by finger command.
- Home directory: The absolute path to the directory the user will be in when they log in. If this directory does not exists then users directory becomes /
- Command/shell: The absolute path of a command or shell (/bin/bash).
k@laptop:~$ tail /etc/shadow tail: cannot open '/etc/shadow' for reading: Permission deniedIt is readable by the superuser only.
k@laptop:~$ su -i root@laptop:/home/bogo# tail /etc/shadow root:!:16203:0:99999:7::: k:$6$d9C.Shbj$M2ptKv3YiS5efYO7yY0YKYu2DWbR4N.8auDIWFRyPFaqgPdfJiID/nPVk1cscVhDSA.parzgg79CwXzJOCL431:16203:0:99999:7::: postgres:*:16223:0:99999:7::: mongodb:*:16263:0:99999:7:::Note that root has "!" for password field. It means no login is allowed. It effectively locks the account and prevents logins to that account. Often this is furthered by setting the account's shell to something like /bin/false or /sbin/nologinin the /etc/passwd file. If the password field contains some string that is not a valid result of crypt, for instance ! or *, the user will not be able to use a unix password to log in (but the user may log in the system by other means).
/etc/group defines the groups to which users belong. Multiple users can be categorized into groups. The file system permissions are organized into three classes, user, group, and others. The use of groups allows additional abilities to be delegated in an organized fashion, such as access to disks, printers, and others.
root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:syslog,k tty:x:5: disk:x:6: lp:x:7: mail:x:8: news:x:9: uucp:x:10:
It has 4 fields:
- group_name
- Password
- Group ID (GID): Each user must be assigned a group ID. We can see this number in our /etc/passwd file.
- Group List: It is a list of user names of users who are members of the group.
Now we want to create a user.
root@laptop:/home/bogo# useradd -m -d /home/bogo -s /bin/bash bogo
where
- -m : Create the user's home directory if it does not exist.
- -d HOME_DIR : The new user will be created using HOME_DIR as the value for the user's login directory.
- -s SHELL The name of the user's login shell
We can see the changes in the files we discussed in the previous sections.
root@laptop:/home/bogo# tail /etc/passwd ... bogo:x:1005:1007::/home/bogo:/bin/bash root@laptop:/home/bogo# tail /etc/shadow ... bogo:!:16413:0:99999:7::: root@laptop:/home/bogo# cd /home/bogo root@laptop:/home/bogo# ls examples.desktop
The 'examples.desktop' has been automatically created. It is actually from /etc/skel:
root@laptop:/home/bogo# ls /etc/skel examples.desktop
Let's look into the /home/ directory:
root@laptop:/home/bogo# ls -l /home total 20 drwxr-xr-x 2 bogo bogo 4096 Dec 8 20:53 bogo drwxr-xr-x 106 k k 4096 Dec 8 18:02 k drwxr-xr-x 3 ubuntu ubuntu 4096 Dec 5 21:05 ubuntu
To check the group 'bogo' belongs:
root@laptop:/home/bogo# grep bogo /etc/group bogo:x:1007:
To set the password for 'bogo':
root@laptop:/home/bogo# passwd bogo Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully root@laptop:/home/bogo# tail /etc/shadow ... bogo:$6$WIsKtb8H$zrRWIZ4w/tYN5QMlMlKpwxpqCtYiDk9rqLGsu.wBK6LWLivx.IbXXyurkay3fZ5CW7IKSG.7IMvSF/z.IrfMv/:16413:0:99999:7::: root@laptop:/home/bogo#
Now, we see the account for 'bogo' is no longer locked.
But if we want to lock it:
root@laptop:/home/bogo# usermod -L bogo root@laptop:/home/bogo# tail /etc/shadow ... bogo:!$6$WIsKtb8H$zrRWIZ4w/tYN5QMlMlKpwxpqCtYiDk9rqLGsu.wBK6LWLivx.IbXXyurkay3fZ5CW7IKSG.7IMvSF/z.IrfMv/:16413:0:99999:7::: root@laptop:/home/bogo#
We can see the ! in the password field. To unlock we use -U switch:
root@laptop:/home/bogo# usermod -U bogo root@laptop:/home/bogo# tail /etc/shadow ... bogo:$6$WIsKtb8H$zrRWIZ4w/tYN5QMlMlKpwxpqCtYiDk9rqLGsu.wBK6LWLivx.IbXXyurkay3fZ5CW7IKSG.7IMvSF/z.IrfMv/:16413:0:99999:7::: root@laptop:/home/bogo#
The ! is gone again. Unlocked.
root@laptop:/home/bogo# usermod -U bogo root@laptop:/home/bogo# tail /etc/shadow ... bogo:$6$WIsKtb8H$zrRWIZ4w/tYN5QMlMlKpwxpqCtYiDk9rqLGsu.wBK6LWLivx.IbXXyurkay3fZ5CW7IKSG.7IMvSF/z.IrfMv/:16413:0:99999:7::: root@laptop:/home/bogo#
Now we want to delete the 'bogo' user we created.
root@laptop:/home/bogo# userdel bogo
After this command, there is no 'bogo' in both /etc/passwd and /etc/shadow files. However, it's still in /home directory:
root@laptop:/home/bogo# ls /home bogo k ubuntu root@laptop:/home/bogo# ls -l /home drwxr-xr-x 2 1005 1007 4096 Dec 8 20:53 bogo drwxr-xr-x 106 k k 4096 Dec 8 18:02 k drwxr-xr-x 3 ubuntu ubuntu 4096 Dec 5 21:05 ubuntu
So, we need to clean that up:
root@laptop:/home/bogo# rm -rf /home/bogo
Linux - system, cmds & shell
- Linux Tips - links, vmstats, rsync
- Linux Tips 2 - ctrl a, curl r, tail -f, umask
- Linux - bash I
- Linux - bash II
- Linux - Uncompressing 7z file
- Linux - sed I (substitution: sed 's///', sed -i)
- Linux - sed II (file spacing, numbering, text conversion and substitution)
- Linux - sed III (selective printing of certain lines, selective definition of certain lines)
- Linux - 7 File types : Regular, Directory, Block file, Character device file, Pipe file, Symbolic link file, and Socket file
- Linux shell programming - introduction
- Linux shell programming - variables and functions (readonly, unset, and functions)
- Linux shell programming - special shell variables
- Linux shell programming : arrays - three different ways of declaring arrays & looping with $*/$@
- Linux shell programming : operations on array
- Linux shell programming : variables & commands substitution
- Linux shell programming : metacharacters & quotes
- Linux shell programming : input/output redirection & here document
- Linux shell programming : loop control - for, while, break, and break n
- Linux shell programming : string
- Linux shell programming : for-loop
- Linux shell programming : if/elif/else/fi
- Linux shell programming : Test
- Managing User Account - useradd, usermod, and userdel
- Linux Secure Shell (SSH) I : key generation, private key and public key
- Linux Secure Shell (SSH) II : ssh-agent & scp
- Linux Secure Shell (SSH) III : SSH Tunnel as Proxy - Dynamic Port Forwarding (SOCKS Proxy)
- Linux Secure Shell (SSH) IV : Local port forwarding (outgoing ssh tunnel)
- Linux Secure Shell (SSH) V : Reverse SSH Tunnel (remote port forwarding / incoming ssh tunnel) /)
- Linux Processes and Signals
- Linux Drivers 1
- tcpdump
- Linux Debugging using gdb
- Embedded Systems Programming I - Introduction
- Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu/Fedora
- LXC (Linux Container) Install and Run
- Linux IPTables
- Hadoop - 1. Setting up on Ubuntu for Single-Node Cluster
- Hadoop - 2. Runing on Ubuntu for Single-Node Cluster
- ownCloud 7 install
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox I
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox II
- Windows 8 guest on Mac OSX host using VirtualBox I
- Ubuntu Package Management System (apt-get vs dpkg)
- RPM Packaging
- How to Make a Self-Signed SSL Certificate
- Linux Q & A
- DevOps / Sys Admin questions
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization