Create a New User Grant Root Privileges and Disabl SSH Root Login on CentOS
Question
Recently, we encountered the problem that Alibaba Cloud ECS server was attacked by DDoS, indicating that our server security needs to be improved. From the perspective of ssh login, you can set sub-users for the linux system and prohibit root login, which can improve certain security.
In this article you will learn
- How to create a new user and set a password for linux system and CentOS system
- How to grant permission control to new CentOS users
- How to prohibit root login to improve server ssh remote connection security
- How to restart sshd service
- How to view file permissions and modify file permissions
User Management
Create a user and set a password
First create a user, give a name, such as
lwebapp
adduser lwebapp
Set a password for the user
lwebapp
, it will trigger interaction, just enter the passwordpasswd lwebapp # Trigger interaction, enter password lwebappwd
Grant root privileges Modify
sudoers
to give root privileges to the newly created user, so that every time you only need to log in with the new userlwebapp
, all server operations can be completed.The
sudoers
file is in the/etc
directory, first modify the permission of this file to allow modificationchmod 777 /etc/sudoers
Then open with
vim
Hit
i
to enter edit modevim /etc/sudoers
Then after the
root ALL=(ALL) ALL
line break, enterlwebapp ALL=(ALL) ALL # Give all permissions to lwebapp, same as root above
Hit
Esc
, enter colon:
to enter vim command mode, then enterwq
, hitEnter
to save and exitAfter saving the file, set the file permissions back
chmod 444 /etc/sudoers
Disable root login Because of the new user, the root user's login authority is prohibited, so that hackers cannot operate the server by cracking the root login. At least our new user name has changed, which adds a layer of difficulty to hacker attacks.
Locate and edit the
sshd_config
filevim /etc/ssh/sshd_config
Find
PermitRootLogin yes
, changeyes
tono
, which means that the root account is not allowed to log inPermitRootLogin no
Restart sshd Finally restart sshd to take effect
systemctl restart sshd.service
Extended Learning
File Permissions
Check file permissions
stat -c '%A %a %n' *
Make all files in a folder have 777 permissions
chmod 777 -R ./webapps
vim basic operations
Open a file
vim file.txt
Enter edit mode
Hiti
, the bottom of the terminal interface displays-- INSERT --
is the edit modeEnter command mode
Enter:
, the bottom of the terminal interface displays:
and the cursorExit edit mode or command mode
HitEsc
Save and exit
In command mode, enterwq
, hitEnter
to save and exitForce Quit
In command mode, enterq!
and hitEnter
to complete the forced exit
sshd service
View sshd service status
systemctl status sshd.service
A series of service statuses will be displayed, such as
running
, which means that the startup is successful.Start the sshd service
systemctl start sshd.service
Restart the sshd service
systemctl restart sshd.service
Set to start at boot
systemctl enable sshd.service
Comments