如何在Fedora上建立一个TFTP服务器

如何在Fedora上建立一个TFTP服务器

TFTP 即简单文本传输协议Trivial File Transfer Protocol,允许用户通过 UDP 协议在系统之间传输文件。默认情况下,协议使用的是 UDP 的 69 号端口。TFTP 协议广泛用于无盘设备的远程启动。因此,在你的本地网络建立一个 TFTP 服务器,这样你就可以对 安装好的 Fedora 和其他无盘设备做一些操作,这将非常有趣。

TFTP 仅仅能够从远端系统读取数据或者向远端系统写入数据,而没有列出远端服务器上文件的能力。它也没提供用户身份验证。由于安全隐患和缺乏高级功能,TFTP 通常仅用于局域网内部(LAN)。

安装 TFTP 服务器

首先你要做的事就是安装 TFTP 客户端和 TFTP 服务器:

  1. <span class="pln">dnf install tftp</span><span class="pun">-</span><span class="pln">server tftp </span><span class="pun">-</span><span class="pln">y</span>

上述的这条命令会在 /usr/lib/systemd/system 目录下为 systemd 创建 tftp.servicetftp.socket 文件。

  1. <span class="str">/usr/</span><span class="pln">lib</span><span class="pun">/</span><span class="kwd">systemd</span><span class="pun">/</span><span class="pln">system</span><span class="pun">/</span><span class="pln">tftp</span><span class="pun">.</span><span class="pln">service</span>
  2. <span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="kwd">systemd</span><span class="pun">/</span><span class="pln">system</span><span class="pun">/</span><span class="pln">tftp</span><span class="pun">.</span><span class="pln">socket</span>

接下来,将这两个文件复制到 /etc/systemd/system 目录下,并重新命名。

  1. <span class="kwd">cp</span><span class="pln"> </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="kwd">systemd</span><span class="pun">/</span><span class="pln">system</span><span class="pun">/</span><span class="pln">tftp</span><span class="pun">.</span><span class="pln">service </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">systemd</span><span class="pun">/</span><span class="pln">system</span><span class="pun">/</span><span class="pln">tftp</span><span class="pun">-</span><span class="pln">server</span><span class="pun">.</span><span class="pln">service</span>
  2. <span class="kwd">cp</span><span class="pln"> </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="kwd">systemd</span><span class="pun">/</span><span class="pln">system</span><span class="pun">/</span><span class="pln">tftp</span><span class="pun">.</span><span class="pln">socket </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="kwd">systemd</span><span class="pun">/</span><span class="pln">system</span><span class="pun">/</span><span class="pln">tftp</span><span class="pun">-</span><span class="pln">server</span><span class="pun">.</span><span class="pln">socket</span>

修改文件

当你把这些文件复制和重命名后,你就可以去添加一些额外的参数,下面是 tftp-server.service 刚开始的样子:

  1. <span class="pun">[</span><span class="typ">Unit</span><span class="pun">]</span>
  2. <span class="typ">Description</span><span class="pun">=</span><span class="typ">Tftp</span><span class="pln"> </span><span class="typ">Server</span>
  3. <span class="typ">Requires</span><span class="pun">=</span><span class="pln">tftp</span><span class="pun">.</span><span class="pln">socket</span>
  4. <span class="typ">Documentation</span><span class="pun">=</span><span class="kwd">man</span><span class="pun">:</span><span class="kwd">in</span><span class="pun">.</span><span class="pln">tftpd</span>
  5.  
  6. <span class="pun">[</span><span class="typ">Service</span><span class="pun">]</span>
  7. <span class="typ">ExecStart</span><span class="pun">=</span><span class="str">/usr/</span><span class="pln">sbin</span><span class="pun">/</span><span class="kwd">in</span><span class="pun">.</span><span class="pln">tftpd </span><span class="pun">-</span><span class="pln">s </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">tftpboot</span>
  8. <span class="typ">StandardInput</span><span class="pun">=</span><span class="pln">socket</span>
  9.  
  10. <span class="pun">[</span><span class="typ">Install</span><span class="pun">]</span>
  11. <span class="typ">Also</span><span class="pun">=</span><span class="pln">tftp</span><span class="pun">.</span><span class="pln">socket</span>

[Unit] 部分添加如下内容:

  1. <span class="typ">Requires</span><span class="pun">=</span><span class="pln">tftp</span><span class="pun">-</span><span class="pln">server</span><span class="pun">.</span><span class="pln">socket</span>

修改 [ExecStart] 行:

  1. <span class="typ">ExecStart</span><span class="pun">=</span><span class="str">/usr/</span><span class="pln">sbin</span><span class="pun">/</span><span class="kwd">in</span><span class="pun">.</span><span class="pln">tftpd </span><span class="pun">-</span><span class="pln">c </span><span class="pun">-</span><span class="pln">p </span><span class="pun">-</span><span class="pln">s </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">tftpboot</span>

下面是这些选项的意思:

  • -c 选项允许创建新的文件
  • -p 选项用于指明在正常系统提供的权限检查之上没有其他额外的权限检查
  • -s 建议使用该选项以确保安全性以及与某些引导 ROM 的兼容性,这些引导 ROM 在其请求中不容易包含目录名。

默认的上传和下载位置位于 /var/lib/tftpboot

下一步,修改 [Install] 部分的内容

  1. <span class="pun">[</span><span class="typ">Install</span><span class="pun">]</span>
  2. <span class="typ">WantedBy</span><span class="pun">=</span><span class="pln">multi</span><span class="pun">-</span><span class="pln">user</span><span class="pun">.</span><span class="pln">target</span>
  3. <span class="typ">Also</span><span class="pun">=</span><span class="pln">tftp</span><span class="pun">-</span><span class="pln">server</span><span class="pun">.</span><span class="pln">socket</span>

不要忘记保存你的修改。

下面是 /etc/systemd/system/tftp-server.service 文件的完整内容:

  1. <span class="pun">[</span><span class="typ">Unit</span><span class="pun">]</span>
  2. <span class="typ">Description</span><span class="pun">=</span><span class="typ">Tftp</span><span class="pln"> </span><span class="typ">Server</span>
  3. <span class="typ">Requires</span><span class="pun">=</span><span class="pln">tftp</span><span class="pun">-</span><span class="pln">server</span><span class="pun">.</span><span class="pln">socket</span>
  4. <span class="typ">Documentation</span><span class="pun">=</span><span class="kwd">man</span><span class="pun">:</span><span class="kwd">in</span><span class="pun">.</span><span class="pln">tftpd</span>
  5.  
  6. <span class="pun">[</span><span class="typ">Service</span><span class="pun">]</span>
  7. <span class="typ">ExecStart</span><span class="pun">=</span><span class="str">/usr/</span><span class="pln">sbin</span><span class="pun">/</span><span class="kwd">in</span><span class="pun">.</span><span class="pln">tftpd </span><span class="pun">-</span><span class="pln">c </span><span class="pun">-</span><span class="pln">p </span><span class="pun">-</span><span class="pln">s </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">tftpboot</span>
  8. <span class="typ">StandardInput</span><span class="pun">=</span><span class="pln">socket</span>
  9.  
  10. <span class="pun">[</span><span class="typ">Install</span><span class="pun">]</span>
  11. <span class="typ">WantedBy</span><span class="pun">=</span><span class="pln">multi</span><span class="pun">-</span><span class="pln">user</span><span class="pun">.</span><span class="pln">target</span>
  12. <span class="typ">Also</span><span class="pun">=</span><span class="pln">tftp</span><span class="pun">-</span><span class="pln">server</span><span class="pun">.</span><span class="pln">socket</span>

启动 TFTP 服务器

重新启动 systemd 守护进程:

  1. <span class="kwd">systemctl</span><span class="pln"> daemon</span><span class="pun">-</span><span class="pln">reload</span>

启动服务器:

  1. <span class="kwd">systemctl</span><span class="pln"> enable </span><span class="pun">--</span><span class="pln">now tftp</span><span class="pun">-</span><span class="pln">server</span>

要更改 TFTP 服务器允许上传和下载的权限,请使用此命令。注意 TFTP 是一种固有的不安全协议,因此不建议你在与其他人共享的网络上这样做。

  1. <span class="kwd">chmod</span><span class="pln"> </span><span class="lit">777</span><span class="pln"> </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">tftpboot</span>

配置防火墙让 TFTP 能够使用:

  1. <span class="pln">firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">add</span><span class="pun">-</span><span class="pln">service</span><span class="pun">=</span><span class="pln">tftp </span><span class="pun">--</span><span class="pln">perm</span>
  2. <span class="pln">firewall</span><span class="pun">-</span><span class="pln">cmd </span><span class="pun">--</span><span class="pln">reload</span>

客户端配置

安装 TFTP 客户端

  1. <span class="kwd">yum</span><span class="pln"> install tftp </span><span class="pun">-</span><span class="pln">y</span>

运行 tftp 命令连接服务器。下面是一个启用详细信息选项的例子:

相关推荐