———————————————————————————————————————
主机操作系统:Centos 6.7
安装配置:tftp服务器
———————————————————————————————————————
1.tftp简介
TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂、开销不大的文件传输服务。由于只进行小文件传输的,因此不具有FTP的许多功能,比如,只能从文件服务器上获得或写入文件,不能列出目录,不进行认证等等。
对于技术人员,特别是嵌入式开发人员或者需要通过TFTP升级固件的IT人员,可能会经常用到TFTP。
2.安装tftp服务器
首先,我们使用rpm命令检查一下,在我们的Linux服务器上是否安装了tftp服务器软件包:
[leiyuxing@centos6 ~]$ rpm -qa | grep tftp
如果没有安装,我们可以直接使用yum来自动下载并安装tftp服务器
[leiyuxing@centos6 ~]$ sudo yum install -y tftp-server
已加载插件:fastestmirror, refresh-packagekit, security
设置安装进程
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: mirror.bit.edu.cn
* updates: mirror.bit.edu.cn
base | 3.7 kB 00:00
extras | 3.3 kB 00:00
updates | 3.4 kB 00:00
解决依赖关系
--> 执行事务检查
---> Package tftp-server.i686 0:0.49-8.el6 will be 安装
--> 处理依赖关系 xinetd,它被软件包tftp-server-0.49-8.el6.i686需要
--> 执行事务检查
---> Package xinetd.i686 2:2.3.14-40.el6 will be 安装
--> 完成依赖关系计算
依赖关系解决
================================================================================
软件包 架构 版本 仓库 大小
================================================================================
正在安装:
tftp-server i686 0.49-8.el6 base 38 k
为依赖而安装:
xinetd i686 2:2.3.14-40.el6 base 123 k
事务概要
================================================================================
Install 2 Package(s)
总下载量:161 k
Installed size: 312 k
下载软件包:
(1/2): tftp-server-0.49-8.el6.i686.rpm | 38 kB 00:00
(2/2): xinetd-2.3.14-40.el6.i686.rpm | 123 kB 00:00
--------------------------------------------------------------------------------
总计 195 kB/s | 161 kB 00:00
运行 rpm_check_debug
执行事务测试
事务测试成功
执行事务
: 2:xinetd-2.3.14-40.el6.i686 1/2
: tftp-server-0.49-8.el6.i686 2/2
Verifying : 2:xinetd-2.3.14-40.el6.i686 1/2
Verifying : tftp-server-0.49-8.el6.i686 2/2
已安装:
tftp-server.i686 0:0.49-8.el6
作为依赖被安装:
xinetd.i686 2:2.3.14-40.el6
完毕!
3.建立tftp服务主工作目录
使用命令mkdir建立tftp的主工作目录(这个目录用于存放宿主机与目标机之间使用tftp时传递的文件)
[leiyuxing@centos6 ~]$ mkdir tftp
[leiyuxing@centos6 ~]$ ls
dropbear-0.53.1 hello platform_led 模板 音乐
dropbear-0.53.1.tar.bz2 hello.1 platform_led_test 视频 桌面
fl2440 LED tftp 图片
gprs LED_test vmware-tools-distrib 文档
GPRS plat_button 公共的 下载
[leiyuxing@centos6 ~]$ cd tftp/
[leiyuxing@centos6 tftp]$ pwd
/home/leiyuxing/tftp
4. 配置并启动tftp服务
linux下的tftp服务是由xinetd(还有openbsd-inetd等其他服务)所设定的,默认情况下tftp是处于关闭状态。所以要修改tftp的配置文件,开启tftp服务。
tftp的配置文件在/etc/xinetd.d/tftp下:
[leiyuxing@centos6 ~]$ sudo vim /etc/xinetd.d/tftp
# default: off
# description: The tftp server serves files using the trivial file transfer \
# protocol. The tftp protocol is often used to boot diskless \
# workstations, download configuration files to network-aware printers, \
# and to start the installation process for some operating systems.
service tftp
{
disable = no #添加这一项
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /home/leiyuxing/tftp -c #修改这一项,这里-s指tftp服务器的根目录,我这里根目录就上上面建立的tftp文件夹,-c指能创建文件
disable = yes
per_source = 11
cps = 100 2
flags = IPv4
}
说明:修改项server_args=-s <path> -c,其中<path>处可以改为你的tftp-server的根目录,参数-s指定chroot,-c指定了可以创建文件。
5.开启xinetd服务
使用命令:sudo service xinetd restart 使上面的更改生效
[leiyuxing@centos6 ~]$ sudo service xinetd restart
停止 xinetd: [确定]
正在启动 xinetd: [确定]
使用netstat命令查看69端口,确认tftp服务是否开启
[leiyuxing@centos6 ~]$ sudo netstat -nlp | grep 69
udp 0 0 0.0.0.0:69 //tftp端口号69 0.0.0.0:* 14697/xinetd
unix 2 [ ACC ] STREAM LISTENING 12692 1764/VGAuthService /var/run/vmware/guestServicePipe
unix 2 [ ACC ] STREAM LISTENING 464217 12950/gdm-simple-gr /tmp/orbit-gdm/linc-3296-0-69ffc48d6cc7a
出现udp 0 0 0.0.0.0:69 0.0.0.0:* 14697/xinetd 则开启成功
6.SeLinux策略修改
SeLinux保持开启状态的话,系统有可能会组织tftp客户端的下载,可以将它暂时关闭:
[leiyuxing@centos6 ~]$ sudo setenforce 0 #这里0表示设置SeLinux为permissive模式,1代表设置SeLinux为enforcing模式可以使用getenforce 命令查看SeLinux状态
[leiyuxing@centos6 ~]$ getenforce
Permissive
如果想彻底禁用SeLinux,修改其配置文件将它禁用
[leiyuxing@centos6 ~]$ sudo vim /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disable #此处设置为disable即可
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
7.防火墙策略修改
系统开启了防火墙也有可能会阻止tftp客户端的下载,我们可以在防火墙规则中使能tftp,只需要使能tftp所使用的69端口即可。
[leiyuxing@centos6 ~]$ sudo vim /etc/sysconfig/selinux
[leiyuxing@centos6 ~]$ sudo /sbin/iptables -I INPUT -p tcp --dport 69 -j ACCEPT
[leiyuxing@centos6 ~]$ sudo /sbin/iptables -I INPUT -p udp --dport 69 -j ACCEPT
[leiyuxing@centos6 ~]$ sudo /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[leiyuxing@centos6 ~]$ sudo /sbin/iptables -I INPUT -p tcp --dport 21 -j ACCEPT
[leiyuxing@centos6 ~]$ sudo /sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT
[leiyuxing@centos6 ~]$ sudo /etc/rc.d/init.d/iptables save
iptables:将防火墙规则保存到 /etc/sysconfig/iptables: [确定]
[leiyuxing@centos6 ~]$ sudo service iptables restart
iptables:将链设置为政策 ACCEPT:nat mangle filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]
iptables:应用防火墙规则: [确定]
查看防火墙状态
[leiyuxing@centos6 ~]$ sudo service iptables status
如果希望在系统启动时防火墙不启动,我们可以用ntsysv关闭防火墙服务,同时还可 以设置tftp服务在系统启动时就开启
[leiyuxing@centos6 ~]$ sudo ntsysv
[ ] ip6tables
[ ] iptables
[*] tftp
[*] xinetd
#使用空格键进行选中或取消,使用Tab进行切换
8.本机测试tftp服务器功能
首先,在你之前设置的tftp根目录下创建一个文件,我自己的是在/home/leiyuxing/tftp/下用vim tt.c创建文件并写入“hello,leiyuxing! ”使用ifconfig查看本机ip地址
[leiyuxing@centos6 tftp]$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:C0:06:A8
inet addr:192.168.86.128
inet6 addr: fe80::20c:29ff:fec0:6a8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:277480 errors:0 dropped:0 overruns:0 frame:0
TX packets:143065 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:123948340 (118.2 MiB) TX bytes:49416246 (47.1 MiB)
Interrupt:19 Base address:0x2024
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:39 errors:0 dropped:0 overruns:0 frame:0
TX packets:39 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2346 (2.2 KiB) TX bytes:2346 (2.2 KiB)
virbr0 Link encap:Ethernet HWaddr 52:54:00:CF:7F:48
inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
通过命令tftp 192.168.86.128进入tftp命令模式,输入help命令,查看tftp支持的命令,通过命令get tt.c 就可以将/home/leiyuxing/tftp下的tt.c文件下载到当前目录下(我当前在/home/leiyuxing/目录下),用ls命令查看当前目录下是否存在tt.c,用cat tt.c查看tt.c的内容
[leiyuxing@centos6 ~]$ tftp 192.168.86.128
tftp> help
tftp-hpa 0.49
Commands may be abbreviated. Commands are:
connect connect to remote tftp
mode set file transfer mode
put send file
get receive file
quit exit tftp
verbose toggle verbose mode
trace toggle packet tracing
literal toggle literal mode, ignore ':' in file name
status show current status
binary set mode to octet
ascii set mode to netascii
rexmt set per-packet transmission timeout
timeout set total retransmission timeout
? print help information
help print help information
tftp> get tt.c
tftp> q
[leiyuxing@centos6 ~]$ ls
dropbear-0.53.1 hello platform_led 公共的 下载
dropbear-0.53.1.tar.bz2 hello.1 platform_led_test 模板 音乐
fl2440 LED tftp 视频 桌面
gprs LED_test tt.c 图片
GPRS plat_button vmware-tools-distrib 文档
在自己的home下看到tt.c说明使用TFTP与/home/tftp文件互传文件成功!
遇到的问题:
问题一:
[leiyuxing@centos6 ~]$ tftp
bash: tftp: command not found
原因:
安装的是tftp server包 ,但是没有安装tftp命令包
使用命令sudo yum install -y tftp安装
解决方法:
[leiyuxing@centos6 ~]$ sudo yum install -y tftp
[leiyuxing@centos6 ~]$ which tftp
/usr/bin/tftp
注意:是tftp和tftp server的版本要是一样的!