Creating and Managing User Accounts on Your CentOS VPS
This tutorial provides a step-by-step guide on creating and managing user accounts on your CentOS VPS, including user creation, permission management, and account security.
centos
Small
VPS
2
vCPU
4 GB
Memory
40 GB
NVMe Disk
2048 GB
Traffic
10.90€
/month
* Up to 12GB RAM, 120GB NVMe Disk Space and 1Gbit/s Network Speed
#### Prerequisites
Before you begin, ensure you have:
- A CentOS VPS (This guide is tested with CentOS 8)
- A user account with sudo privileges
- Basic familiarity with the command line
#### Step 1: Create a New User
To create a new user, use the `useradd` command followed by the username. For example, to create a user named `john`:
```bash
sudo useradd john
```
Set a password for the new user:
```bash
sudo passwd john
```
You will be prompted to enter and confirm the new password.
#### Step 2: Add User to Sudo Group
To grant the new user sudo privileges, add them to the `wheel` group:
```bash
sudo usermod -aG wheel john
```
Users in the `wheel` group have sudo access by default on CentOS.
#### Step 3: Create a Home Directory for the User
The `useradd` command typically creates a home directory automatically. Verify that the home directory exists:
```bash
ls /home/john
```
If the home directory does not exist, create it manually:
```bash
sudo mkdir /home/john
sudo chown john:john /home/john
```
#### Step 4: Set Up SSH Access
To allow the new user to access the server via SSH, copy the SSH keys (if applicable):
1. **Create the `.ssh` directory:**
```bash
sudo mkdir /home/john/.ssh
sudo chown john:john /home/john/.ssh
sudo chmod 700 /home/john/.ssh
```
2. **Add the public key to `authorized_keys`:**
```bash
sudo nano /home/john/.ssh/authorized_keys
```
Paste the public key into the file, save, and exit. Set the appropriate permissions:
```bash
sudo chown john:john /home/john/.ssh/authorized_keys
sudo chmod 600 /home/john/.ssh/authorized_keys
```
#### Step 5: Configure User Shell Settings
To change the default shell for a user, use the `chsh` command followed by the shell path. For example, to set the default shell to Bash:
```bash
sudo chsh -s /bin/bash john
```
Verify the change:
```bash
grep john /etc/passwd
```
#### Step 6: Managing User Permissions
To manage user permissions, you can edit the sudoers file using `visudo`:
```bash
sudo visudo
```
Add specific permissions for the user under the user privilege specification section:
```plaintext
john ALL=(ALL) NOPASSWD: /usr/bin/yum
```
This line allows the user `john` to run `yum` commands without a password prompt.
#### Step 7: Lock and Unlock User Accounts
To lock a user account, preventing them from logging in:
```bash
sudo passwd -l john
```
To unlock the account:
```bash
sudo passwd -u john
```
#### Step 8: Delete a User Account
To delete a user account along with their home directory, use the `userdel` command with the `-r` flag:
```bash
sudo userdel -r john
```
#### Step 9: Monitor User Activity
To monitor user activity and login attempts, you can use the `last` command to see the history of user logins:
```bash
last john
```
To view the current logged-in users, use:
```bash
w
```
#### Step 10: Secure User Accounts
For enhanced security, enforce strong passwords and password policies. Edit the `/etc/login.defs` file to set password aging policies:
```bash
sudo nano /etc/login.defs
```
Configure settings such as `PASS_MAX_DAYS`, `PASS_MIN_DAYS`, and `PASS_WARN_AGE` to enforce password changes and expiration:
```plaintext
PASS_MAX_DAYS 90
PASS_MIN_DAYS 10
PASS_WARN_AGE 7
```
#### Conclusion
You have successfully created and managed user accounts on your CentOS VPS, including user creation, permission management, and security measures. Regularly review and update user permissions and security settings to ensure the integrity and security of your server.
**Additional Resources:**
- **CentOS Documentation:** [https://www.centos.org/docs/](https://www.centos.org/docs/)
- **Linux User Management:** [https://www.linux.org/forums/linux-user-management.148/](https://www.linux.org/forums/linux-user-management.148/)
- **Sudoers Manual:** [https://www.sudo.ws/man/1.8.15/sudoers.man.html](https://www.sudo.ws/man/1.8.15/sudoers.man.html)