Home >Operation and Maintenance >Linux Operation and Maintenance >How to use sudo in Linux cloud server

How to use sudo in Linux cloud server

PHPz
PHPzforward
2023-05-12 10:31:221829browse

Step one: Install sudo

CentOS

yum install sudo -y

Ubuntu/Debian

apt-get install sudo -y

FreeBSD

cd /usr/ports/security/sudo/ && make install clean

or

pkg install sudo

th Step 2: Create a sudo user

The sudo user is an ordinary user in the Linux operating system. The following uses the username zhaomu as an example to create an ordinary user.

CentOS/Ubuntu/Debian/FreeBSD

adduser zhaomu

Step 3: Add the user to the wheel group

The wheel group is a user that restricts users from executing as administrators group, only users in this user group can execute sudo commands. In the Ubuntu/Debian operating system, the sudo group is usually used to replace the role of the wheel group.

CentOS

usermod -aG wheel zhaomu

Ubuntu/Debian

usermod -aG sudo zhaomu

FreeBSD

pw group mod wheel -m zhaomu

Step 4: Configure sudoers

The sudo configuration file is / etc/sudoers, we need to ensure that there is no problem with this configuration file so that the sudo command can be executed normally.

CentOS/Ubuntu/Debian/FreeBSD

vi /etc/sudoers

or

visudo

Find the following code:

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

Please make sure that the Linux cloud server you are using is also set up in this way of. Note: Some Linux systems do not use %sudo but %wheel. This is no problem.

Step 5: Restart the SSH service

If you have modified the /etc/sudoers file, you need to restart the SSH service to make it take effect.

CentOS 6

/etc/init.d/sshd restart

CentOS 7

systemctl restart sshd.service

Ubuntu/Debian

/etc/init.d/sshd restart

FreeBSD

/etc/rc.d/sshd start

Step 6: Test

After completing the previous operation, please exit the remote login, log in to the system as the sudo user again, and execute the following command to test whether sudo is configured correctly.

sudo uptime
sudo whoami

Sudo whoami should return root.

Execute any of the following commands to switch from the sudo user to the root user.

sudo su -
sudo -i
sudo -S

Step 7: Prohibit root user from logging in

After testing, if everything is normal, you can perform the last step, which is to prohibit root user from logging in. We need to edit the SSH configuration file.

sudo vi /etc/ssh/sshd_config

Use the :w/ command to search for the following code, delete the comment # in front of this line of code, and set the value to no.

PermitRootLogin     no

Next, follow the instructions in step five to restart the SSH service. Try to log in to the system as the root user. If you cannot log in, it means the setting is successful.

The above is the detailed content of How to use sudo in Linux cloud server. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete