Home > Article > Operation and Maintenance > How to use sudo in Linux cloud server
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
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
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
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.
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
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
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!