Merge pull request #5359 from geekpi/master

translated
This commit is contained in:
geekpi 2017-03-27 09:48:17 +08:00 committed by GitHub
commit 42222bca45
2 changed files with 179 additions and 181 deletions

View File

@ -1,181 +0,0 @@
translating---geekpi
How to Install a DHCP Server in Ubuntu and Debian
============================================================
Dynamic Host Configuration Protocol (DHCP) is a network protocol that is used to enable host computers to be automatically assigned IP addresses and related network configurations from a server.
The IP address assigned by a DHCP server to DHCP client is on a “lease”, the lease time normally varies depending on how long a client computer is likely to require the connection or DHCP configuration.
#### How Does DHCP Work?
The following is a quick description of how DHCP actually works:
* Once a client (that is configured to use DHCP) and connected to a network boots up, it sends a DHCPDISCOVER packet to the DHCP server.
* When the DHCP server receives the DHCPDISCOVER request packet, it replies with a DHCPOFFERpacket.
* Then the client gets the DHCPOFFER packet, and it sends a DHCPREQUEST packet to the server showing it is ready to receive the network configuration information provided in the DHCPOFFERpacket.
* Finally, after the DHCP server receives the DHCPREQUEST packet from the client, it sends the DHCPACK packet showing that the client is now permitted to use the IP address assigned to it.
In this article, we will show you how to setup a DHCP server in Ubuntu/Debian Linux, and we will run all the commands with the [sudo command][1] to gain root user privileges.
#### Testing Environment Setup
We are going to use following testing environment for this setup.
```
DHCP Server - Ubuntu 16.04
DHCP Clients - CentOS 7 and Fedora 25
```
### Step 1: Installing DHCP Server in Ubuntu
1. Run the command below to install the DCHP server package, which was formerly known as dhcp3-server.
```
$ sudo apt install isc-dhcp-server
```
2. When the installation completes, edit the file /etc/default/isc-dhcp-server to define the interfaces DHCPD should use to serve DHCP requests, with the INTERFACES option.
For example, if you want the DHCPD daemon to listen on `eth0`, set it like so:
```
INTERFACES="eth0"
```
And also remember to [configure a static IP address][2] for the interface above.
### Step 2: Configuring DHCP Server in Ubuntu
3. The main DHCP configuration file is `/etc/dhcp/dhcpd.conf`, you must add all your network information to be sent to clients here.
And, there are two types of statements defined in the DHCP configuration file, these are:
* parameters  specify how to perform a task, whether to carry out a task, or what network configuration options to send to the DHCP client.
* declarations  define the network topology, state the clients, offer addresses for the clients, or apply a group of parameters to a group of declarations.
4. Now, open and modify the main configuration file, define your DHCP server options:
```
$ sudo vi /etc/dhcp/dhcpd.conf
```
Set the following global parameters at the top of the file, they will apply to all the declarations below (do specify values that apply to your scenario):
```
option domain-name "tecmint.lan";
option domain-name-servers ns1.tecmint.lan, ns2.tecmint.lan;
default-lease-time 3600;
max-lease-time 7200;
authoritative;
```
5. Now, define a subnetwork; here, well setup DHCP for 192.168.10.0/24 LAN network (use parameters that apply to your scenario).
```
subnet 192.168.10.0 netmask 255.255.255.0 {
option routers 192.168.10.1;
option subnet-mask 255.255.255.0;
option domain-search "tecmint.lan";
option domain-name-servers 192.168.10.1;
range 192.168.10.10 192.168.10.100;
range 192.168.10.110 192.168.10.200;
}
```
### Step 3: Configure Static IP on DHCP Client Machine
6. To assign a fixed (static) IP address to a particular client computer, add the section below where you need to explicitly specify its MAC addresses and the IP to be statically assigned:
```
host centos-node {
hardware ethernet 00:f0:m4:6y:89:0g;
fixed-address 192.168.10.105;
}
host fedora-node {
hardware ethernet 00:4g:8h:13:8h:3a;
fixed-address 192.168.10.106;
}
```
Save the file and close it.
7. Next, start the DHCP service for the time being, and enable it to start automatically from the next system boot, like so:
```
------------ SystemD ------------
$ sudo systemctl start isc-dhcp-server.service
$ sudo systemctl enable isc-dhcp-server.service
------------ SysVinit ------------
$ sudo service isc-dhcp-server.service start
$ sudo service isc-dhcp-server.service enable
```
8. Next, do not forget to permit DHCP service (DHCPD daemon listens on port 67/UDP) on firewall as below:
```
$ sudo ufw allow 67/udp
$ sudo ufw reload
$ sudo ufw show
```
### Step 4: Configuring DHCP Client Machines
9. At this point, you can configure your clients computers on the network to automatically receive IP addresses from the DHCP server.
Login to the client computers and edit the Ethernet interface configuration file as follows (take note of the interface name/number):
```
$ sudo vi /etc/network/interfaces
```
And define the options below:
```
auto eth0
iface eth0 inet dhcp
```
Save the file and exit. And restart network services like so (or reboot the system):
```
------------ SystemD ------------
$ sudo systemctl restart networking
------------ SysVinit ------------
$ sudo service networking restart
```
Alternatively, use the GUI on a desktop machine to perform the settings, set the Method to Automatic (DHCP) as shown in the screenshot below (Fedora 25 desktop).
[
![Set DHCP Network in Fedora](http://www.tecmint.com/wp-content/uploads/2017/03/Set-DHCP-Network-in-Fedora.png)
][3]
Set DHCP Network in Fedora
At this point, if all settings are correctly configured, your client machine should be receiving IP addresses automatically from the DHCP server.
Thats it! In this tutorial, we showed you how to setup a DHCP server in Ubuntu/Debian. Share your thoughts with us via the feedback section below. If you are using Fedora based distribution, go through how to setup a DHCP server in CentOS/RHEL.
--------------------------------------------------------------------------------
作者简介:
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/install-dhcp-server-in-ubuntu-debian/
作者:[Aaron Kili][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/aaronkili/
[1]:http://www.tecmint.com/sudoers-configurations-for-setting-sudo-in-linux/
[2]:http://www.tecmint.com/set-add-static-ip-address-in-linux/
[3]:http://www.tecmint.com/wp-content/uploads/2017/03/Set-DHCP-Network-in-Fedora.png
[4]:http://www.tecmint.com/author/aaronkili/
[5]:http://www.tecmint.com/10-useful-free-linux-ebooks-for-newbies-and-administrators/
[6]:http://www.tecmint.com/free-linux-shell-scripting-books/

View File

@ -0,0 +1,179 @@
如何在 Ubuntu 以及 Debian 中安装 DHCP 服务器
============================================================
动态主机配置协议DHCP是一种用于使主机能够从服务器自动分配IP地址和相关的网络配置的网络协议。
DHCP 服务器分配给 DHCP 客户端的IP地址处于“租用”状态租用时间通常取决于客户端计算机可能需要连接的时间或 DHCP 配置的时间。
#### DHCP 如何工作?
以下是 DHCP 实际工作原理的简要说明:
* 一旦客户端(配置使用 DHCP并连接到网络后它会向 DHCP 服务器发送 DHCPDISCOVER 数据包。
* 当 DHCP 服务器收到 DHCPDISCOVER 请求报文后会使用 DHCPOFFER 包进行回复。
* 然后客户端获取到 DHCPOFFER 数据包,并向服务器发送一个 DHCPREQUEST 包,表示它已准备好接收 DHCPOFFER 包中提供的网络配置信息。
* 最后DHCP 服务器从客户端收到 DHCPREQUEST 报文后,发送 DHCPACK 报文表示客户端现在允许使用分配给它的IP地址。
在本文中,我们将介绍如何在 Ubuntu/Debian Linux 中设置 DHCP 服务器,我们将使用[ sudo 命令][1]来运行所有命令,以获得 root 用户权限。
### 测试环境设置
这步我们会使用如下的测试环境。
```
DHCP Server - Ubuntu 16.04
DHCP Clients - CentOS 7 and Fedora 25
```
### 步骤1在 Ubuntu 中安装 DHCP 服务器
1. 运行下面的命令来安装 DHCP 服务器包,也就是 dhcp3-server。
```
$ sudo apt install isc-dhcp-server
```
2. 安装完成后,编辑 /etc/default/isc-dhcp-server 使用 INTERFACES 选项定义 DHCPD 响应 DHCP 请求所使用的接口。
比如,如果你想让 DHCPD 守护进程监听 `eth0`,按如下设置:
```
INTERFACES="eth0"
```
同样记得为上面的接口[配置静态地址][2]。
### 步骤 2在 Ubuntu 中配置 DHCP 服务器
3. DHCP 配置的主文件是 `/etc/dhcp/dhcpd.conf` 你必须填写会发送到客户端的所有网络信息。
并且 DHCP 配置中定义了两种不同的声明,它们是:
* parameters - 指定如何执行任务、是否执行任务,还有指定要发送给 DHCP 客户端的网络配置选项。
* declarations - 定义网络拓扑、指定客户端、为客户端提供地址,或将一组参数应用于一组声明。
4. 现在打开并修改主文件,定义 DHCP 服务器选项:
```
$ sudo vi /etc/dhcp/dhcpd.conf
```
在文件顶部设置以下全局参数,它们将应用于下面的所有声明(请指定适用于你情况的值):
```
option domain-name "tecmint.lan";
option domain-name-servers ns1.tecmint.lan, ns2.tecmint.lan;
default-lease-time 3600;
max-lease-time 7200;
authoritative;
```
5. 现在定义一个子网,这里我们为 192.168.10.0/24 局域网设置 DHCP (使用适用你情况的参数):
```
subnet 192.168.10.0 netmask 255.255.255.0 {
option routers 192.168.10.1;
option subnet-mask 255.255.255.0;
option domain-search "tecmint.lan";
option domain-name-servers 192.168.10.1;
range 192.168.10.10 192.168.10.100;
range 192.168.10.110 192.168.10.200;
}
```
### 步骤 3在 DHCP 客户端上配置静态地址
6. 要给特定的客户机分配一个固顶的静态的IP你需要显式将这台机器的 MAC 地址以及静态分配的地址添加到下面这部分。
```
host centos-node {
hardware ethernet 00:f0:m4:6y:89:0g;
fixed-address 192.168.10.105;
}
host fedora-node {
hardware ethernet 00:4g:8h:13:8h:3a;
fixed-address 192.168.10.106;
}
```
保存并关闭文件。
7.接下来,启动 DHCP 服务,并让它下次开机自启动,如下所示:
```
------------ SystemD ------------
$ sudo systemctl start isc-dhcp-server.service
$ sudo systemctl enable isc-dhcp-server.service
------------ SysVinit ------------
$ sudo service isc-dhcp-server.service start
$ sudo service isc-dhcp-server.service enable
```
8. 接下来不要忘记允许 DHCP 服务DHCP 守护进程监听 67 UDP 端口)的防火墙权限:
```
$ sudo ufw allow 67/udp
$ sudo ufw reload
$ sudo ufw show
```
### 步骤 4配置 DHCP 客户端
9. 此时,你可以将客户端计算机配置为自动从 DHCP 服务器接收 IP 地址。
登录到客户端并编辑以太网接口的配置文件(注意接口名称/号码):
```
$ sudo vi /etc/network/interfaces
```
定义如下选项:
```
auto eth0
iface eth0 inet dhcp
```
保存文件并退出。重启网络服务(或重启系统):
```
------------ SystemD ------------
$ sudo systemctl restart networking
------------ SysVinit ------------
$ sudo service networking restart
```
另外你也可以使用 GUI 来在进行设置,如截图所示(在 Fedora 25 桌面中设置将方式设为自动DHCP
[
![Set DHCP Network in Fedora](http://www.tecmint.com/wp-content/uploads/2017/03/Set-DHCP-Network-in-Fedora.png)
][3]
在 Fedora 中设置 DHCP 网络
此时,如果所有设置完成了,你的客户端应该可以自动从 DHCP 服务器接收IP 地址了。
就是这样了!在本篇教程中,我们向你展示了如何在 Ubuntu/Debian 设置 DHCP 服务器。在反馈栏中分享你的想法。如果你正在使用基于 Fedora 的发行版,请阅读如何在 CentOS/RHEL 中设置 DHCP 服务器。
--------------------------------------------------------------------------------
作者简介:
Aaron Kili 是 Linux 和 F.O.S.S 爱好者,将来的 Linux SysAdmin 和 web 开发人员,目前是 TecMint 的内容创建者,他喜欢用电脑工作,并坚信分享知识。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/install-dhcp-server-in-ubuntu-debian/
作者:[Aaron Kili][a]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/aaronkili/
[1]:http://www.tecmint.com/sudoers-configurations-for-setting-sudo-in-linux/
[2]:http://www.tecmint.com/set-add-static-ip-address-in-linux/
[3]:http://www.tecmint.com/wp-content/uploads/2017/03/Set-DHCP-Network-in-Fedora.png
[4]:http://www.tecmint.com/author/aaronkili/
[5]:http://www.tecmint.com/10-useful-free-linux-ebooks-for-newbies-and-administrators/
[6]:http://www.tecmint.com/free-linux-shell-scripting-books/