如何为Apache生成和配置Htpasswd?

Today the world is insecure. We have to provide different types of security measures like authentication. In HTTP servers like Apache or Nginx, we can use HTTP Basic Authentication. HTTP Basic Authentication is an authentication protocol provided by the webserver. We can use htpasswd tool to create HTTP Basic Authentication Database and Users. Using application-level authentication is a better solution by the way.

今天,世界是不安全的。 我们必须提供不同类型的安全措施,例如身份验证。 在Apache或Nginx等HTTP服务器中,我们可以使用HTTP基本身份验证。 HTTP基本身份验证是Web服务器提供的身份验证协议。 我们可以使用htpasswd工具创建HTTP基本身份验证数据库和用户。 顺便说一句,使用应用程序级身份验证是更好的解决方案。

安装htpasswd工具 (Install htpasswd Tool)

htpasswd is provided by Apache Utils package for Ubuntu, Debian, Mint and Kali. We can install it with the following command.

htpasswd由Apache Utils软件包提供,适用于Ubuntu,Debian,Mint和Kali。 我们可以使用以下命令进行安装。

$ sudo apt install apache2-utils

为Fedora,CentOS,RHEL安装Htpasswd工具 (Install Htpasswd Tool For Fedora, CentOS, RHEL)

As RPM-based distributions like Fedora, CentOS and RHEL names Apache as httpd we can install htpasswd with the following command.

当基于RPM的发行版(如Fedora,CentOS和RHEL)将Apache命名为httpd我们可以使用以下命令安装htpasswd

$ sudo yum install httpd-tools

创建Htpasswd数据库和用户 (Create Htpasswd Database and User)

We will create a user password database with the -c option and providing related information like database file name, user, and password. In this example, we will create a database named .htpasswd username ismail and password.

我们将使用-c选项创建一个用户密码数据库,并提供相关信息,例如数据库文件名,用户和密码。 在此示例中,我们将创建一个名为.htpasswd用户名ismail和password的数据库。

$ htpasswd -c .htpasswd ismail
Create Htpasswd Database and User
创建Htpasswd数据库和用户

列出htpasswd用户(List htpasswd Users)

We can list existing users from providing a database file with the cat command. Because our file named .htpasswd is just a text file where the password is stored in an encrypted format. The database file is named as db and located under the current directory where the htpasswd command is executed.

我们可以通过使用cat命令提供数据库文件来列出现有用户。 因为我们名为.htpasswd的文件只是一个文本文件,其中密码以加密格式存储。 该数据库文件名为db ,位于htpasswd命令执行的当前目录下。

$ cat .htpasswd
List Users
List Users
列出用户

We can see the db file content that there are two columns which are delimited with the : . The first column contains the user name which is ismail, ahmet, ali in this case, and the second column stores passwords in an encrypted version.

我们可以看到db文件的内容有两列,它们之间用:分隔。 第一列包含用户名,在这种情况下为ismail,ahmet,ali,第二列存储加密版本的密码。

LEARN MORE  Linux SCP Command Usage With Examples
通过示例了解更多Linux SCP命令用法

更改现有用户密码(Change Existing User Password)

We can change the existing user password we just need to provide the user name again like creating from scratch. This will ask us the user password again. In this example, we will change the password of ismail.

我们可以更改现有的用户密码,只需从头开始创建即可再次提供用户名。 这将再次询问我们user密码。 在此示例中,我们将更改ismail的密码。

$ htpasswd db ismail
Change Existing User Password
Change Existing User Password
更改现有用户密码

As we can see from out that we have successfully update the password of  ismail.

从中可以看出,我们已经成功更新了ismail的密码。

从htpasswd文件中删除用户 (Remove User From htpasswd File)

If we want to prevent users from access we should remove users from our database. We will use -D option and specify the user name we want to remove. In this example, we will remove the user named ali.

如果要阻止用户访问,则应从数据库中删除用户。 我们将使用-D选项并指定要删除的用户名。 在此示例中,我们将删除名为ali的用户。

$ htpasswd -D .htpasswd ali
Remove User
Remove User
删除用户

为Apache配置身份验证(Configure Authentication For Apache)

Up to now, we have learned how to manage user databases. In this part, we will look at how to configure Apache for basic authentication. The following configuration will be put into Apache main conf or in a site configuration like virtual host.

到目前为止,我们已经学习了如何管理用户数据库。 在这一部分中,我们将研究如何配置Apache进行基本身份验证。 以下配置将放入Apache主配置文件或站点配置(如虚拟主机)中。

<VirtualHost *:80>
   ServerAdmin[email protected]
   DocumentRoot /var/www/html
   <Directory "/var/www/html">
   AuthType Basic
   AuthName "Restricted Content"
   AuthUserFile /etc/apache2/.htpasswd
   Require valid-user
   </Directory>
</VirtualHost>
  • In this configuration, we assume that our database file named db is located at /etc/apache2/db

    在此配置中,我们假设名为db的数据库文件位于/etc/apache2/db

  • This authentication will be applied for the directory /var/www/html

    此身份验证将应用于目录/var/www/html

In order to make configuration we should restart Apache2 server after saving configuration.

为了进行配置,我们应该在保存配置后重新启动Apache2服务器。

检查HTTP基本身份验证 (Check HTTP Basic Authentication)

If we try to browse the web page we will get a prompt like below which is asking for username and password those will be checked from our db file.

如果尝试浏览网页,则会出现如下提示,要求您usernamepassword这些信息将从db文件中进行检查。

Check HTTP Basic Authentication
Check HTTP Basic Authentication
检查HTTP基本身份验证

翻译自: https://www.poftut.com/generate-and-configure-htpasswd-password-in-linux-for-apache-and-nginx-server/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值