最近自己在单位内部搭建了个zabbix(过段时间在把搭建过程发出),管理所管辖的网络设备及服务器,顺便把网络设备也做个备份,方便日后维护。想到的就是让交换机每日自动备份到tftp服务器上。tftp打算跟zabbix放到一起。
在网上找了很多文章,但基本都是过时的,最后找到一个国外的文章,顺便翻译过来做个记录。

一、安装TFTP Server

TFTP 服务器包在 CentOS 8 的官方包存储库中可用。因此,首先,使用以下命令更新 CentOS 8 软件包存储库缓存:

dnf makecache

centos7没有tftp centos8 tftp_linux


现在,使用以下命令安装 TFTP Server:

dnf install tftp-server -y

centos7没有tftp centos8 tftp_linux_02

很快,已经安装好了!!下面就开始配置吧!

二、配置TFTP Server Service

TFTP 的默认 systemd 服务配置在 CentOS 8 上无法正常工作。因此,您必须为 TFTP 服务器创建自己的 systemd 服务版本。
首先,使用以下命令将默认的 /usr/lib/systemd/system/tftp.service 文件复制到 /etc/systemd/system/tftp-server.service

cp -v /usr/lib/systemd/system/tftp.service /etc/systemd/system/tftp-server.service

然后,使用以下命令将默认的 /usr/lib/systemd/system/tftp.socket 文件复制到 /etc/systemd/system/tftp-server.socket:

cp -v /usr/lib/systemd/system/tftp.socket /etc/systemd/system/tftp-server.socket

centos7没有tftp centos8 tftp_linux_03


现在,使用以下命令编辑 /etc/systemd/system/tftp-server.service 文件,个人习惯了用nano命令

nano /etc/systemd/system/tftp-server.service

tftp-server.service 文件的默认内容如下

[Unit]
Description=Tftp Server
Requires=tftp.socket
Documentation=man:in.tftpd

[Service]
ExecStart=/usr/sbin/in.tftpd -s /var/lib/tftpboot
StandardInput=socket

[Install]
Also=tftp.socket

1、将 Requires=tftp.socket 更改为 Requires=tftp-server.socket 2、将 ExecStart=/usr/sbin/in.tftpd -s /var/lib/tftpboot 更改为 ExecStart=/usr/sbin/in.tftpd -c -p -s /var/lib/tftpboot 3、将 Also=tftp.socket 更改为 Also=tftp-server.socket 4、在 [Install] 行之后添加一个新行 WantedBy=multi-user.target 说明:

ExecStart=/usr/sbin/in.tftpd -c -p -s /var/lib/tftpboot 用于运行 TFTP 服务器守护进程。
-c 选项用于允许在 TFTP 服务器中创建新文件。
-p 选项用于解决许多文件和目录权限问题。
-s 选项用于设置 TFTP 服务器的根目录。
TFTP 根目录为 /var/lib/tftpboot

centos7没有tftp centos8 tftp_centos7没有tftp_04


ctrl+o 然后回车保存修改,ctrl+x退出。

接着,使用以下命令编辑 /etc/systemd/system/tftp-server.socket 文件:

nano /etc/systemd/system/tftp-server.socket

默认的 tftp-server.socket 文件应如下面的所示。
ListenDatagram=69 行之后添加一个新行 BindIPv6Only=both

[Unit]
Description=Tftp Server Activation Socket

[Socket]
ListenDatagram=69

[Install]
WantedBy=sockets.target

centos7没有tftp centos8 tftp_CentOS_05


ctrl+o 然后回车保存修改,ctrl+x退出。

启动tftp-server服务:

systemctl start tftp-server.service

使用以下命令将 tftp-server 服务添加到 CentOS 8 机器的系统启动中:

systemctl enable tftp-server.service

好了,现在来看看tftp-server的运行状态:

systemctl status tftp-server.service

centos7没有tftp centos8 tftp_服务器_06

如果您启用了 SELinux,请使用以下命令允许对 TFTP 服务器的匿名写访问:

setsebool -P tftp_anon_write 1

注意:如果 CentOS 8 上可无法使用 setsebool 命令。请使用以下命令安装 policycoreutils-python 包:

yum install -y policycoreutils-python

现在,使用以下命令允许任何用户对 TFTP 根目录 /var/lib/tftpboot 的读取、写入和执行权限:

chmod 777 /var/lib/tftpboot

三、配置防火墙

TFTP 服务器在 UDP 端口 69 上运行。
如果您在 CentOS 8 机器上配置了防火墙,您必须使用以下命令允许访问 UDP 端口 69:

firewall-cmd --add-service=tftp --permanent

重载防火墙:

firewall-cmd --reload

四、TFTP Server的使用

要访问 TFTP 服务器,首先你要知道你 CentOS 8 机器的 IP 地址,可以使用以下命令找到您的 CentOS 8 机器的 IP 地址:

nmcli

centos7没有tftp centos8 tftp_centos7没有tftp_07


为了访问 TFTP 服务器,你要先安装 TFTP 客户端,在 CentOS 8/RHEL 8 上,您可以使用以下命令安装 TFTP 客户端:

dnf install -y tftp

安装好后就可以直接用了,先用cd命令进入你要上传或下载的目录,然后输入 tftp 192.168.10.13,上传用put命令,下载用get命令,用status查看状态,其他的这里我就不多说了!