Ubuntu Users: Difference between revisions

From CompleteNoobs
Jump to navigation Jump to search
Created page with "==Create User Account== add user using a ubuntu script <code>/usr/sbin/adduser</code><br \> <code>adduser <username></code><br \> And nice and straightforward.<br \> Manually for scripts:<br \> <code>useradd</code>-><code>/usr/sbin/useradd</code> <br \> Syntax:<code>useradd -s <path/shell> -d <directory> -m -G <SecondaryGroup> <user>; passwd <user> </code><br \> Example<code>useradd -s /bin/bash -d /home/green -m -G sudo green;passwd green</code> <div class="toccolours..."
 
(No difference)

Latest revision as of 21:53, 2 April 2023

Create User Account

add user using a ubuntu script /usr/sbin/adduser
adduser <username>
And nice and straightforward.

Manually for scripts:
useradd->/usr/sbin/useradd
Syntax:useradd -s <path/shell> -d <directory> -m -G <SecondaryGroup> <user>; passwd <user>
Exampleuseradd -s /bin/bash -d /home/green -m -G sudo green;passwd green

flag notes:

-s login shell to be used for created account
-d home directory for created account.
-m make home directory if does not exist.
-G add created user to groups.
; separator
passwd $USER will prompt for a password of created account.

Create User Account with restricted shell

sudo useradd -s /usr/bin/rbash -d /home/<username> -m <username>;passwd <username>


Change User Shell

Give a user a restricted shell:
chsh -s /usr/bin/rbash <user>

Change User Password

Open a terminal window and type the following command, replacing "username" with the name of the user whose password you want to change:

sudo passwd username

  • You will be prompted to enter and confirm the new password for the user. Follow the on-screen instructions to complete the process.
  • Once the password has been changed, you can test it by logging in to the Ubuntu server with the user's credentials.

Remove/Delete User

To delete a user and their home directory on Ubuntu 22.04, you can use the userdel command. Follow these steps:

  • Open a terminal window.
  • Make sure you have root privileges or use sudo. You need administrative rights to delete a user. If you're not logged in as the root user, you can use sudo to execute the command with elevated privileges.
  • Use the userdel command with the -r flag to delete the user and their home directory:

sudo userdel -r username

Replace username with the actual username of the user you want to delete.

The -r flag will remove the user's home directory, along with their mail spool, and any files they own in their home directory. Be cautious when using this flag, as it will permanently delete the user's data.

Note: Before deleting a user, ensure that they are not currently logged in and that no processes are running under their account. You can use the who command to see who is currently logged in and the ps -u username command to check for any running processes for that user. If necessary, you can terminate processes using the kill command, followed by the process ID

To delete USER and keep there home directory on system use:
userdel username

Add user to group/s

add user to sudo group after account created:
usermod -aG sudo <user>

Check if a User is a member of a group

To check if a user is a member of a specific group, you can use the groups or id command.

  • groups command:

groups username

Replace username with the actual username. This command will display a list of groups the user is a member of.

  • id command:

id username

Replace username with the actual username. This command will display the user ID, group ID, and the groups the user is a member of.

See what users are in a specific group

To see the list of users in a specific group, you can use the getent command:

getent group groupname

Replace groupname with the actual name of the group you want to check. This command will display the group name, group ID, and the list of users in that group.

Alternatively, you can check the /etc/group file, which contains information about all the groups on your system and their members:

grep groupname /etc/group

Replace groupname with the actual name of the group. This command will display the group name, group ID, and the list of users in that group.

Find Users on system

/etc/passwd see users
/etc/shadow contains hash of passwords.
cat /etc/shadow | grep <user>

Run Command as another user

sudo -H -u <user> bash -c "mkdir /home/<user>/.ssh"

su - <user> -c "mkdir /home/<user>/.ssh"

su - www-data -c "touch /var/log/logfile"

Monitoring user activity on your system

To see what users are currently doing on your system, you can use the following commands:
who

  • who: Shows a list of users currently logged in, their terminal, and login time.
  • w: Displays information about the users currently on the machine and their processes. This includes the user's login name, terminal, host, login time, idle time, and the currently running process.
  • last: Shows a list of the last logged-in users.

To monitor the activity of a specific user, you can use the ps command with the -u flag, followed by the username:

ps -u username

This command will show a list of processes owned by the specified user.