Difference between revisions of "Useradd"

From wikieduonline
Jump to navigation Jump to search
(Created page with " == Add user using Ansible == <pre> - user: name: YOUR_USER_NAME shell: /bin/bash groups: sudo append: yes password_lock: yes </pre> == Ad...")
 
 
(80 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{lowercase}}
 +
<code>useradd</code><ref>https://linux.die.net/man/8/useradd</ref> - create a new user or update default new user information ([https://linux.die.net/man/8/useradd man])
 +
 +
[[useradd -m]] YOUR_USERNAME [[-s]] [[/bin/]][[bash]]
 +
        -m, --create-home
 +
 +
<code>useradd -m YOUR_USERNAME --[[uid]] #### --[[gid]] #### -s [[/bin/bash]] --create-home</code>
 +
 +
<code>useradd -m YOUR_USERNAME -[[g]] your-group -s [[/bin/bash]] --create-home</code>
 +
 +
[[-c]]
  
 
== Add user using [[Ansible]] ==
 
== Add user using [[Ansible]] ==
<pre>
+
 
 
   - user:
 
   - user:
 
       name: YOUR_USER_NAME
 
       name: YOUR_USER_NAME
 
       shell: /bin/bash
 
       shell: /bin/bash
       groups: sudo
+
       [[groups]]: sudo
 
       append: yes
 
       append: yes
       password_lock: yes
+
       [[password_lock]]: yes
</pre>
 
  
 
== Add user using bash ==
 
== Add user using bash ==
 
Example creating a user in [[Ubuntu]] with bash shell, ~/.ssh directory and part of group [[sudo]] using <code>useradd</code><ref>http://man7.org/linux/man-pages/man8/useradd.8.html</ref> command:
 
Example creating a user in [[Ubuntu]] with bash shell, ~/.ssh directory and part of group [[sudo]] using <code>useradd</code><ref>http://man7.org/linux/man-pages/man8/useradd.8.html</ref> command:
<pre>
 
#!/bin/bash
 
USERNAME="Your_user_name"
 
  
# Create user and add to sudo group
+
#!/bin/bash
  useradd  --create-home -s /bin/bash $USERNAME
+
USERNAME="Your_user_name"
  sudo usermod -aG sudo $USERNAME
+
 +
# Create user and add to sudo group
 +
  useradd  --create-home -s /bin/bash $USERNAME
 +
  sudo [[usermod]] -aG sudo $USERNAME
 +
 +
#Create ssh directory and lock password login
 +
  mkdir /home/$USERNAME /home/$USERNAME/.ssh
 +
  chown $USERNAME.$USERNAME /home/$USERNAME /home/$USERNAME/.ssh
 +
  [[passwd -l]] $USERNAME  
  
#Create ssh directory and lock password login
 
  mkdir /home/$USERNAME /home/$USERNAME/.ssh
 
  chown $USERNAME.$USERNAME /home/$USERNAME /home/$USERNAME/.ssh
 
  passwd -l $USERNAME
 
  
</pre>
 
 
:<code>[[passwd]] -l $USERNAME</code> // for disabling password login
 
:<code>[[passwd]] -l $USERNAME</code> // for disabling password login
 
:<code>passwd -u $USERNAME</code> // will unlock account if needed
 
:<code>passwd -u $USERNAME</code> // will unlock account if needed
<pre>
 
passwd -u $USERNAME
 
passwd: password expiry information changed.
 
</pre>
 
  
After creating user you can copy ssh key using [[ssh|ssh-copy-id]] and modifying [[sudo]] for giving new user privileges.
 
  
 +
After creating user you can copy ssh key using <code>[[ssh|ssh-copy-id]]</code> and modifying <code>[[sudo]]</code> for giving new user privileges.
 +
 +
== useradd command ==
 
<code>useradd</code><ref>http://man7.org/linux/man-pages/man8/useradd.8.html</ref> command.
 
<code>useradd</code><ref>http://man7.org/linux/man-pages/man8/useradd.8.html</ref> command.
:-m --create-home
+
-m --create-home
:-s --shell
+
-M, --no-create-home
 +
-N, --no-user-group
 +
-s --shell
  
 +
<code>useradd -m YOUR_USERNAME -s /bin/bash</code>
  
You can also consider activating [[Sudo/passwordless sudo|passwordless sudo]] for your accounts.
 
  
== See also ==
+
You can also consider activating <code>[[passwordless sudo]]</code> for your accounts.
* [[macOS]]: [[MacOS/Create a user from command line|sysadminctl]]
+
 
* [[sudo]]
+
== Related commands ==
* [[OpenSSH]]
+
* <code>[[jailkit]]</code>, <code>[[ChrootDirectory]]</code>
* [[Ansible]]: [[DevOps/Ansible/User ssh access]]
+
* <code>[[passwd]]</code>
* [[userdel]]
+
* <code>[[userdel]]</code>, <code>[[usermod]]</code>
* [[usermod]]  
+
* <code>[[adduser]]</code> ([[BusyBox]])
* <code>[[groups]]</code> or <code>[[id]]</code> commands to list groups of a user. sudo usermod -a -G root USERNAME
+
* <code>[[groups]]</code>, <code>[[chgrp]]</code>
* [[chmod]]
+
* [[Cisco IOS]]: <code>[[username]]</code>
 +
* [[FreeIPA]]: <code>[[ipa user-add]]</code>
 +
* [[macOS]]:
 +
** <code>[[sysadminctl -addUser]] USERNAME</code>
 +
** <code>[[dscl . -create]] /Users/USERNAME_HERE</code>
 +
 
 +
* <code>[[~/.ssh/]]</code>
 +
* [[Cisco IOS]]: [[Associate a user with default higher privileges]]: <code>[[username]]</code>
 +
* <code>[[CREATE ROLE]]</code>
 +
* <code>[[groupadd]]</code>
 +
* [[aws iam create-user]]
 +
* [[AWS]]: <code>[[aws iam create-role]]</code>
 +
* <code>[[aws_iam_user]]</code>
 +
* <code>[[Kubernetes services accounts]]: [[kind: ServiceAccount]]</code>
 +
* <code>[[kubectl create clusterrolebinding]] ops-user-cluster-admin-binding --clusterrole=[[cluster-admin]] --user=ops-user</code>
  
 +
== Activities ==
 +
* [[Ansible]]: [[User ssh access]]
 +
* [[Sftp configuration]]
  
 +
== See also ==
 +
* [[macOS]]: <code>[[sysadminctl]]</code>
 +
* {{Linux Commands privileges}}
 +
* <code>[[groups]]</code> or <code>[[id]]</code> commands to list groups of a user. sudo <code>usermod -a -G root USERNAME</code>
 +
* {{useradd}}
  
[[Category:Linux]]
+
[[Category:Linux commands]]

Latest revision as of 09:23, 24 January 2024

useradd[1] - create a new user or update default new user information (man)

useradd -m YOUR_USERNAME -s /bin/bash
        -m, --create-home

useradd -m YOUR_USERNAME --uid #### --gid #### -s /bin/bash --create-home

useradd -m YOUR_USERNAME -g your-group -s /bin/bash --create-home

-c

Add user using Ansible[edit]

 - user:
     name: YOUR_USER_NAME
     shell: /bin/bash
     groups: sudo
     append: yes
     password_lock: yes

Add user using bash[edit]

Example creating a user in Ubuntu with bash shell, ~/.ssh directory and part of group sudo using useradd[2] command:

#!/bin/bash
USERNAME="Your_user_name"

# Create user and add to sudo group
  useradd  --create-home -s /bin/bash $USERNAME
  sudo usermod -aG sudo $USERNAME 

#Create ssh directory and lock password login
  mkdir /home/$USERNAME /home/$USERNAME/.ssh
  chown $USERNAME.$USERNAME /home/$USERNAME /home/$USERNAME/.ssh 
  passwd -l $USERNAME 


passwd -l $USERNAME // for disabling password login
passwd -u $USERNAME // will unlock account if needed


After creating user you can copy ssh key using ssh-copy-id and modifying sudo for giving new user privileges.

useradd command[edit]

useradd[3] command.

-m --create-home
-M, --no-create-home
-N, --no-user-group
-s --shell

useradd -m YOUR_USERNAME -s /bin/bash


You can also consider activating passwordless sudo for your accounts.

Related commands[edit]

Activities[edit]

See also[edit]

  • https://linux.die.net/man/8/useradd
  • http://man7.org/linux/man-pages/man8/useradd.8.html
  • http://man7.org/linux/man-pages/man8/useradd.8.html
  • Advertising: