Home Linux ServerLinux Fundementals How to create a sudo user and disabling SSH access with the root user

How to create a sudo user and disabling SSH access with the root user

by Thạch Phạm
Published: Last Updated on
A+A-
Reset

Creating a sudo user and disabling SSH access with the root user with 5 simple steps.

Introduction

On Linux, the sudo command is used to allow the user to run the program with the highest privileges, being able to execute all other users’ rights, and with this privilege, we will often see the root user.

In this article, AZDIGI will show you how to create a new user and grant sudo privileges to that user on a Linux server (specifically CentOS 7). And after creating this new user, you can directly use it and perform command line administration without having to log in to the root user as usual.

Implementation Guide

Step 1: SSH into your Linux server

To create a new user and grant sudo privileges on Linux, we first need to SSH or access your VPS/server as root. If you don’t know how to SSH into your VPS/Server, you can refer to the following tutorial:

After successfully SSH, we continue with step 2 to continue the configuration.

Step 2: Create a new user and password

  • You can use the command below to add a new User to the system.

For example, I will add a new user named: new_user, then the command structure is as follows: (Remember to replacenew_userwith the new user you need to create).

AZDIGI Tutorial
adduser new_user
    
  • After adding the user, use the command below to set the password for that user with the following command structure:
AZDIGI Tutorial
passwd new_user
    

Next, you set a new password, you will receive the message that all authentication tokens updated successfully is successful.

CleanShot 2022 05 12 at 13.56.31

Step 3: Grant permissions to the user

  • To grant permissions to a new user, use usermod to add a user to the wheel group with the command:
AZDIGI Tutorial
usermod -aG wheel new_user
    

By default, the server will have a group named wheel, members added to this wheel group will have sudo privileges.

  • Check if the user can use the sudo command

To check, you can type the command sudo t.

AZDIGI Tutorial
su - new_user
sudo df -h
    

If the system displays the results as shown below, it is successful.

CleanShot 2022 05 12 at 14.00.49

Step 4: Disable SSH access with the root user

After creating a new user and granting sudo permissions, if you just want to SSH through the newly created user and want to disable SSH access with the root user, you can do the following:

You open and edit the SSH configuration file according to the path:

AZDIGI Tutorial
vi /etc/ssh/sshd_config
    

Here, you find the PermitRootLogin line and change it to no.

How to create a sudo user and disabling SSH access with the root user

Now you proceed to save the file and restart the sshd service with the command below:

AZDIGI Tutorial
systemctl restart ssh
    

Step 5: Check the result

Now we will go to check the result after configuration. Below is the result when I SSH back into the server with both the new and root users.

CleanShot 2022 05 12 at 14.05.41

So in this article, AZDIGI showed you how to create and add sudo privileges to a new user to replace the root user, which helps you manage multiple users and make your Linux server more secure. Hope this article is helpful to you, wish you success!

You can refer to other instructions at the link below:

If you need support, you can contact support in the ways below:

Đánh giá

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign