Ubuntu16.04中搭建TFTP 和 NFS 服务器

2023-03-14,,

Ubuntu 16.04中搭建TFTP服务

1. 安装
  $ apt-get install tftp-hpa tftpd-hpa
 
2. 建立目录
  $ mkdir /tftpboot # 这是建立tftp传输目录。
  $ sudo chmod 0777 /tftpboot
  $ sudo touch test.txt # test.txt文件最好输入内容以便区分
   

3. 配置/etc/xinetd.conf
  配置相关服务文件。进入根目录下的 etc 文件夹(cd /etc/),首先看目录中有没有一个xinetd.conf 文件,如果没有则新建一个,有的话查看内容,看是否与下面的一致,若不一致则
修改,内容如下:

  # Simple configuration file for xinetd
  #
  # Some defaults, and include /etc/xinetd.d/

  defaults
  {

  # Please note that you need a log_type line to be able to use log_on_success
  # and log_on_failure. The default is the following :
  # log_type = SYSLOG daemon info

  }

  includedir /etc/xinetd.d

  

4. 配置
  # vi /etc/default/tftpd-hpa
  TFTP_USERNAME="tftp"
  TFTP_DIRECTORY="/tftpboot" # 这里是你的tftpd-hpa的服务目录,这个想建立在哪里都行
  TFTP_ADDRESS="0.0.0.0:69"
  TFTP_OPTIONS="-l -c -s" # 这里是选项,-c是可以上传文件的参数,-s是指定tftpd-hpa服务目录,上面已经指定
  

5. 配置/etc/xinetd.d/tftp
  然后进入 xinetd.d 文件夹(cd xinetd.d),查看是否有一个 tftp 文件,如果没有就新建一个,如果有的话就查看内容是否与下面的一致,不一致则修改,内容如下:
  service tftp
  {
  socket_type = dgram
  wait = yes
  disable = no
  user = root
  protocol = udp
  server = /usr/sbin/in.tftpd
  server_args = -s /tftpboot
  #log_on_success += PID HOST DURATION
  #log_on_failure += HOST
  per_source = 11
  cps =100 2
  flags =IPv4

  }

  

 
6. 重启服务

  重新启动服务。sudo service tftpd-hpa restart,这也是我经常疏忽的一步,当配置好 tftp 的配置文件后,需要重新启动一下 xinetd,在终端中输入 sudo /etc/init.d/xinetd reload,重新加载一下进程,再输入 sudo /etc/init.d/xinetd restart,重启服务。记住,每次修改完配置文件后,都需要重新启动一下服务。
执行次序:

  $ sudo service tftpd-hpa restart
  $ sudo /etc/init.d/xinetd reload
  $ sudo /etc/init.d/xinetd restart

 
7. 测试
  # cd /home
  # tftp localhost  #localhost 表示本机
  tftp>get test.txt  //test.txt 是之前在 /tftpboot 目录下新建的文件
  tftp>put test1.txt //test1.txt 是在 /home 目录下新建的文件
  tftp>q
  退出后,在/home目录下会有一个test.txt文件,在/tftpboot 目录下有test1.txt,表示tftp服务器安装成功!
  

ubuntu 16.04 中搭建NFS服务

  nfs服务是实现Linux和Linux之间的文件共享,nfs服务的搭建比较简单。现在介绍如何在ubuntu16.04系统中搭建nfs服务,ubuntu的搭建比红帽的还要简单。

1、安装nfs服务

$ sudo apt-get install nfs-kernel-server
$ sudo apt-get install nfs-common

2、修改配置文件

  1、sudo vim /etc/exports

修改内容如下:
/root/rootfs *(rw,sync,no_root_squash,no_subtree_check)

  

各段表达的意思如下,根据实际进行修改
复制代码
/home :共享的目录
* :指定哪些用户可以访问
* 所有可以ping同该主机的用户
192.168.1.* 指定网段,在该网段中的用户可以挂载
192.168.1.12 只有该用户能挂载
(ro,sync,no_root_squash): 权限
ro : 只读
rw : 读写
sync : 同步
no_root_squash: 不降低root用户的权限
其他选项man 5 exports 查看
复制代码

  2、更改文件夹的访问权限:chmod 777 -R /root/rootfs/rootfs

  3、在终端执行:showmount -e

  4、更新:exportfs -r

  5、重启本地网卡:showmount localhost -e

  6、挂在目录到本地:mount -t nfs -o nolock 192.168.1.141:/root/rootfs/rootfs /opt

3、重启nfs服务

  sudo /etc/init.d/nfs-kernel-server restart
  到此,nfs的服务就搭建好了。

下面介绍客户端如何访问服务器

  1、检查客户端和服务端的网络是否连通(ping命令)

    ping + 主机IP

  2、查看服务端的共享目录

    showmount -e + 主机IP

    showmount -e 192.168.1.93
    Export list for 192.168.1.93:
    /home *
  3、将该目录挂载到本地

    mount -t nfs -o nolock 192.168.1.141:/root/rootfs/rootfs /opt
  4、访问

    访问本地的mnt目录,就可访问服务端共享的目录了。

Ubuntu16.04中搭建TFTP 和 NFS 服务器的相关教程结束。

《Ubuntu16.04中搭建TFTP 和 NFS 服务器.doc》

下载本文的Word格式文档,以方便收藏与打印。