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] ********************************************** ok: [169.254.41.222] changed: [localhost

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,                 "total": 996,                 "used": 123             },             "swap": {                 "cached": 0,                 "free": 15

connect: Network is unreachable

connect: Network is unreachable If you are getting below error, then probably you have not added gateway address or you have incorrect gateway in config file. [root@cluster ~]# ping google.com connect: Network is unreachable Before: [root@cluster ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth3 DEVICE=eth3 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.174.130 NETMASK=255.255.255.0 After: [root@cluster ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth3 DEVICE=eth3 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.174.130 NETMASK=255.255.255.0 GATEWAY=192.168.174.2 [root@cluster ~]# ifdown eth3 [root@cluster ~]# ifup eth3 Determining if ip address 192.168.174.130 is already in use for device eth3... [root@cluster ~]# ping google.com PING google.com (216.58.218.174) 56(84) bytes of data. 64 bytes from dfw06s46-in-f14.1e100.net (216.58.218.174): icmp_seq=1 ttl=128 time=261 ms 6