Home  >  Article  >  System Tutorial  >  How to easily reset password on Linux system? A practical guide to teach you three methods

How to easily reset password on Linux system? A practical guide to teach you three methods

WBOY
WBOYforward
2024-02-11 21:00:04784browse

Linux system is a powerful and flexible operating system that can run on a variety of devices, from personal computers to servers, and even mobile phones and tablets. But what should you do if you forget your Linux system password, or you want to change your password? Don’t worry, this article will teach you three simple and effective methods so that you can easily reset the password of your Linux system, whether you are using Ubuntu, CentOS or other distributions.

How to easily reset password on Linux system? A practical guide to teach you three methods

One of these is to set a password for the database root account - you must keep this private and only use it when absolutely necessary. This article will come in handy if you forget your password or need to reset it (for example, when the database administrator is replaced or laid off!). We will explain how to reset or recover the root password for MySQL or MariaDB in Linux.

Suggested reading: Changing the root password for MySQL or MariaDB.

Although we will use MariaDB in this article, these instructions also apply to MySQL.

Recover the root password of MySQL or MariaDB

Before starting, stop the database service and check the service status. We should be able to see the environment variables set previously:

------------- SystemD ------------- 
# systemctl stop mariadb
------------- SysVinit -------------
# /etc/init.d/mysqld stop

Next, start the service with the --skip-grant-tables option:

------------- SystemD ------------- 
# systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
# systemctl start mariadb
# systemctl status mariadb
------------- SysVinit -------------
# mysqld_safe --skip-grant-tables &

How to easily reset password on Linux system? A practical guide to teach you three methods
Start MySQL/MariaDB using skip tables

This allows you to connect to the database without a root password (you may need to switch to another terminal):

# mysql -u root

Next, follow the steps listed below.

>MariaDB [(none)]> USE mysql;
MariaDB [(none)]> UPDATE user SET password=PASSWORD('YourNewPasswordHere') WHERE User='root' AND Host = 'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;

Finally, stop the service, cancel the environment variable settings and start the service again:

------------- SystemD ------------- 
# systemctl stop mariadb
# systemctl unset-environment MYSQLD_OPTS
# systemctl start mariadb
------------- SysVinit -------------
# /etc/init.d/mysql stop
# /etc/init.d/mysql start

This will allow the previous changes to take effect, allowing you to connect to the database using the new password.

Summarize

Through this article, you should have learned how to use three different methods to reset the password of your Linux system. These methods are using single-user mode, using a CD or USB flash drive, and using the chroot command. Each method has its advantages and disadvantages, and you can choose the appropriate method according to your situation and needs. After resetting your password, remember to keep your new password safe to avoid forgetting or losing it again.

The above is the detailed content of How to easily reset password on Linux system? A practical guide to teach you three methods. For more information, please follow other related articles on the PHP Chinese website!

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