One of the fundamental tasks in Linux server administration is managing user accounts and access control. In this tutorial, we will guide you through the process of creating a new user and configuring an SSH login for that user on a Linux server. SSH (Secure Shell) is a crucial tool for remotely accessing and managing servers securely. By the end of this guide, you will be able to create a new user account and enable SSH access for that user.
Prerequisites
Before we get started, ensure that you have:
- Access to a Linux server: You should have SSH access to the server as a user with administrative privileges or root access.
Step 1: Log in to the Server
To begin, SSH into your Linux server. Open your terminal or an SSH client and enter the following command, replacing your_server_ip
with your server's IP address:
ssh your_username@your_server_ip
You will be prompted to enter your password.
Step 2: Create a New User
Once you are logged in, you can create a new user using the useradd
command. Replace new_username
with the desired username:
sudo useradd new_username
Step 3: Set a Password for the New User
Next, set a password for the new user using the passwd
command:
sudo passwd new_username
You will be prompted to enter and confirm the new password.
Step 4: Grant Sudo Privileges (Optional)
If you want to grant administrative privileges to the new user (allowing them to execute commands with superuser rights), you can add them to the sudo group using the usermod
command:
sudo usermod -aG sudo new_username
Step 5: Create SSH Keys (Optional)
For added security and convenience, you can generate SSH keys for the new user. This step is optional but highly recommended. To generate SSH keys, run the following command:
ssh-keygen
Follow the prompts to generate the SSH keys. This will create a public key (id_rsa.pub
) and a private key (id_rsa
) in the ~/.ssh
directory of the new user's home folder.
Step 6: Configure SSH for the New User
Now, we need to configure SSH to allow the new user to log in. Open the SSH configuration file using a text editor such as nano
or vi
:
sudo nano /etc/ssh/sshd_config
Find the line that says PermitRootLogin
and change its value to no
to disable root login:
PermitRootLogin no
Next, add the following lines at the end of the file to allow the new user to log in via SSH:
AllowUsers new_username
Save the file and exit the text editor.
Step 7: Restart the SSH Service
To apply the changes, restart the SSH service:
sudo systemctl restart sshd
Step 8: Test SSH Login
Exit the server by typing:
exit
Now, try logging in with the new user:
ssh new_username@your_server_ip
If you configured SSH keys, you should be able to log in without entering a password. If not, you will need to enter the password you set for the new user.
Conclusion
In this tutorial, you've learned how to create a new user and configure SSH access on a Linux server. This process is essential for securing your server and managing user access effectively. Remember to always follow best practices for user and access management to ensure the security of your Linux server.