How to use sudo in Linux cloud server

PHPz
Release: 2023-05-12 10:31:22
forward
1740 people have browsed it

Step one: Install sudo

CentOS

yum install sudo -y
Copy after login

Ubuntu/Debian

apt-get install sudo -y
Copy after login

FreeBSD

cd /usr/ports/security/sudo/ && make install clean
Copy after login

or

pkg install sudo
Copy after login

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
Copy after login

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
Copy after login

Ubuntu/Debian

usermod -aG sudo zhaomu
Copy after login

FreeBSD

pw group mod wheel -m zhaomu
Copy after login

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
Copy after login

or

visudo
Copy after login

Find the following code:

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

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
Copy after login
Copy after login

CentOS 7

systemctl restart sshd.service
Copy after login

Ubuntu/Debian

/etc/init.d/sshd restart
Copy after login
Copy after login

FreeBSD

/etc/rc.d/sshd start
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!