Skip to main content

Managing linux users

We will discuss how to create new user, modify existing user details and password.



Types  of users          UID 

root                               0
system                           1-999
normal                          1000-6000


System user is again divided into two types- Kernel and Application users

Kernel :Like ping, shutdown have UID between 1-200
Application: Like Mysql have UID between 201-999

 Adding user:

Command:
useradd <user>

Eg:
useradd star1

Note that a line will be added at the bottom of /etc/passwd file as below

star1:x:1076:1078::/home/star1:/bin/bash


If password not set for user then the user will be locked.

Setting a password for the created user:

command:
passwd <user>

Eg:

#passwd star1
Changing password for user star1.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.

 You can switch user from one user to another using "su -" command

Eg:
su - star1

Each row in /etc/passwd has some meaning

Eg:
star1:x:1076:1078::/home/star1:/bin/bash

Each is separated by :
1.  Username
2. password pointer
3. User ID
4. Group ID
5. Comment (In above example it is blank)
6. User home directory path
7. Shell assigned to the user

All the above information are taken from /etc/login.defs

The user password is stored in /etc/shadow file in SHA5 encrypted format.

Eg:
star1:$6$EalMg2fD$BPhfTEC8yo0xEvrmFEYBVZ/ZUW/j7dmdRA4392GqFy7n5GENMqRg4bbBr1yMBdKTX1cf3.z2mjH1GGVXKeuH6.:16683:0:99999:7:::


Highlighted above is the password of the user star1 in encrypted format.

Line has 9 fields.

1. User
2. Encrypted Password
3. The date of the last password change since Jan 1, 1970.
4.  The number of days the user will have to wait to change password again
5. The number of days after which the user will have to change password
6. The number of days before a password is going to expire
7.  Password inactivity period
8. The date of expiration of the account since Jan 1, 1970.
9.  This field is reserved for future use.


By default adding user using useradd will create UID, home directory automatically.

If we need to give our own UID, home directory then below command helps.

Ex:
useradd -u 5555 -s /bin/csh -c "star2xxx@gmail.com" -d /home/myhome star2


 -c COMMENT        
  -d home directory of the new account
  -e expiration date of the new account
  -f password inactivity period of the new account
  -g GID
  -G list of supplementary groups of the new account
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -p, --password PASSWORD       encrypted password of the new account
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account

To delete user:

userdel <user>

Eg:
userdel star1


Above command will not delete the home directory of the user star1

To remove entire home directory along with the user use -r in userdel command

eg:
userdel -r star2












Comments

Popular posts from this blog

Ansible script to stop iptables

 Ansible script to stop iptables and disable during boot Step 1. [root@cluster playbooks]# pwd /root/playbooks [root@cluster playbooks]# cat hosts [webservers] 169.254.41.221 169.254.41.222 Step2. [root@cluster playbooks]# cat iptables.yml --- - name: stop ipatbles and disable   hosts: webservers   tasks:   - name: stop iptables     service: name=iptables state=stopped   - name: disbale on iptable on boot     service: name=iptables enabled=no Step3: [root@cluster playbooks]# ansible-playbook iptables.yml PLAY [stop ipatbles and disable] *********************************************** TASK [setup] ******************************************************************* ok: [169.254.41.222] ok: [169.254.41.221] ok: [localhost] TASK [stop iptables] *********************************************************** changed: [localhost] ok: [169.254.41.221] ok: [169.254.41.222] TASK [disbale on iptable on boot] *********************************...

Get information about remote hosts using Ansible

Get information about remote hosts using Ansible setup command Below command gives all the information of client hosts which includes memory, server architecture, IP adresses etc. [root@ansible mywork]# ansible all -i hosts -m setup If you need just memory information of remote node then for the above command need to add filter as shown below [root@ansible mywork]# ansible all -i hosts -m setup -a "filter=ansible_*_mb" node01 | SUCCESS => {     "ansible_facts": {         "ansible_memfree_mb": 873,         "ansible_memory_mb": {             "nocache": {                 "free": 919,                 "used": 77             },             "real": {                 "free": 873,           ...

Using ansible ping module check connectivity between two nodes

Using ansible ping module check connectivity between two nodes Once server and client machine is configured with SSH passwordless authentication and  ansible installed in server, we will verify if ansible can connect from server to client. If you have not configured refer page Login to server node and create a directory to save all the work done in one location Eg:mywork [root@ansible ~]# mkdir mywork [root@ansible ~]# cd mywork/ Create new file called "hosts" under "mywork" and add your client host name in the file as shown below. [root@ansible mywork]# cat hosts [mynodes] node01 Now is the time to check if ansible from server machine able to communicate to client node01 To check this we will use ansible module called ping. Run command as shown below. [root@ansible mywork]# ansible all -i hosts -u root -m ping node01 | SUCCESS => {     "changed": false,     "ping": "pong" }