tftp服务器搭建
[1] 基本原理
tftp服务器最好搭建在Linux系统上面,这里是在虚拟机(ubuntu)上搭建tftp服务器,搭建步骤如下:
1. 确认虚拟机和外网连通
2. 安装tftp服务软件并启动
3. 配置tftp服务软件权限和参数
4. 本机验证tftp服务
[2] 具体过程
1. 确认网络连通
在ubuntu上,安装软件需要网络的支持,所以安装软件前必须保证已经联网,且DNS配置正确。检测命令如下:
ubuntu@ubuntu:~$ ping -c4 www.baidu.com
PING www.a.shifen.com (119.75.218.77)56(84) bytes of data.
64 bytes from119.75.218.77: icmp_req=1 ttl=128 time=68.1 ms
表示网络和DNS已经配置正确。
2. 安装tftp服务软件
在ubuntu上,执行如下命令:
ubuntu@ubuntu:~$ sudo apt-get install tftp-hpa tftpd-hpa xinetd
tftp-hpa是客户端,tftpd-hpa是服务器
3. 确认tftp服务运行
在ubuntu系统下,执行如下命令:
ubuntu@ubuntu:~$ps -ef | grep tftp
root 1019 1 0 06:58 ? 00:00:00 /usr/sbin/in.tftpd --listen--user tftp --address 0.0.0.0:69 --secure /home/ubuntu/work/tftpboot
ubuntu 10476 2841 0 17:29 pts/0 00:00:00 grep --color=auto tftp
/usr/sbin/in.tftpd程序进程存在表示tftp服务运行成功。
4. 修改配置文件(新建tftp根目录,修改tftp服务根目录)
ubuntu@ubuntu:~$cd /
ubuntu@ubuntu:~$ sudo mkdir /tftpboot
ubuntu@ubuntu:~$sudo chmod 777 /tftpboot
如果想修改tftp服务的根目录,运行如下命令:
ubuntu@ubuntu:~$ sudo vim /etc/default/tftpd-hpa
会出现如下界面:
TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/tftpboot"
TFTP_ADDRESS="0.0.0.0:69"
TFTP_OPTIONS="-c -s -l" //-c表示create -s表示secure(安全)-l 表示listen
修改TFTP_DIRECTORY="/tftpboot"这一行中引号中的路径,然后杀掉tftpd服务进程,命令如下:
ubuntu@ubuntu:~$ sudo service tftpd-hpa restart
ubuntu会自动重启服务,服务启动后,根路径设置就生效了。
5. 验证tftp服务配置成功
写一个hello.c文件存放到/tftpboot,然后在用户主目录运行tftp命令,过程如下:
ubuntu@ubuntu:~$ ls /tftpboot/hello.c
/tftpboot/hello.c
ubuntu@ubuntu:~$ cd /home/ubuntu
ubuntu@ubuntu:~$ tftp127.0.0.1
tftp> get hello.c
tftp> quit
ubuntu@ubuntu:~$ ls hello.c
hello.c
NFS服务器搭建
nfs是网络文件系统,它可以将服务器上的一个文件夹挂载到本机作为一个文件系统。利用这个工具可以直接利用服务器上面的文件,而不用先下载到本地,给调试程序和文件系统带来极大的便利。
[1] 基本原理
nfs服务器最好搭建在Linux系统上面,这里是在虚拟机(ubuntu)上搭建nfs服务器,搭建步骤如下:
1. 确认虚拟机和外网连通
2. 安装nfs服务软件并启动
3. 配置nfs服务软件权限和参数
4. 本机(Linux机)验证nfs服务
5. Windows客户端挂载NFS
[2] 具体过程
使用nfs前需要在服务器端搭建nfs服务环境:
[1] 安装nfs服务程序及客户端程序
ubuntu@ubuntu:~$sudo apt-get install nfs-kernel-server nfs-common
新建一个exports.d目录,如果不存在则新建,若是存在则不用执行,命令如下:
ubuntu@ubuntu:~$sudo mkdir /etc/exports.d
[2] 导出nfs文件系统
1. 配置nfs文件系统
打开/etc/exports文件,添加如下行(注意:#表示注释,这里不添加):
ubuntu@ubuntu:~$ sudo vim /etc/exports
/source/rootfs * (rw,sync,no_root_squash, no_subtree_check)
“/source/rootfs”是存放nfs文件系统的路径;“*”表示在所有的网段都可以共享;“rw”表示nfs文件系统允许读写;“sync”表示修改都会同步到nfs服务端,否则只是会暂存在本地内存;
新建nfs目录,修改权限:
ubuntu@ubuntu:~$ sudo mkdir -p /source/rootfs
ubuntu@ubuntu:~$sudo chmod 777 -R /source
2. 导出
ubuntu@ubuntu:~$ sudo exportfs –rv
注意:这步一定要成功,否则下面mount会失败
exporting*:/home/caihuanming/nfs
[3] 启动NFS服务
root@ubuntu:~# sudo /etc/init.d/nfs-kernel-server status
[3] 重启服务
ubuntu@ubuntu:~$ sudo /etc/init.d/nfs-kernel-server restart
[4] 查看NFS当前状态
root@ubuntu:~# sudo /etc/init.d/nfs-kernel-server start
[4’]查看进程
root@ubuntu:~# ps -ef | grep nfs
[5] 测试及挂载共享资源
1. 新建客户端目录
ubuntu@ubuntu:~$ sudo mkdir /srv/nfsclient
ubuntu@ubuntu:~$ ls /srv/nfsclient -ld
drwxr-xr-x 2 root root 4096 2012-01-10 20:43/srv/nfsclient
2. 挂载到nfs服务器
ubuntu@ubuntu:~$
sudo mount -t nfs 127.0.0.1:/source/rootfs /srv/nfsclient
[sudo] password for ubuntu:
root@ubuntu:~# sudo mount -t nfs 192.168.1.104:/home/caihuanming/nfs /srv/nfsclient
root@ubuntu:~# sudo password for ubuntu:
3. 确认挂载成功
ubuntu@ubuntu:~$ df
Filesystem 1K-blocks Used Available Use%Mounted on
/dev/sda1 19609276 7010752 11602428 38% /
udev 506028 4 506024 1% /dev
tmpfs 205216 1396 203820 1% /run
none 5120 0 5120 0% /run/lock
none 513040 180 512860 1% /run/shm
/dev/sdb1 61926396 3774704 55006016 7% /home/ubuntu/work
127.0.0.1: /source/rootfs
61926400 3774592 55006080 7% /srv/nfsclient
ubuntu@ubuntu:~/work/rootfs$
确认粗体行一定要存在,这样表示nfs服务器搭建成功
[6]卸载共享资源
root@ubuntu:~# umount /srv/nfsclient
umount –f 强行卸载
[7]停止NFS服务
root@ubuntu:~# sudo /etc/init.d/nfs-kernel-server stop
[8]查看NFS服务器的共享资源
root@ubuntu:~# showmount -e 192.168.1.106 /*localhost*/
Export list for 192.168.1.106:
/home/caihuanming/nfs *
[9]卸载共享资源
umount /
4. Windows客户端挂载NFS
NFS是C/S架构。因此Windows客户端同样需要安装NFS客户端软件,我们以Win7为例
1)点击开始 -> 控制面板->点击程序
2)点击“打开或关闭windows功能”
3)展开NFS服务,勾选“NFS客户端”和“管理工具”;同时勾选上“基于unix的应用程序子系统”,然后点确定
4)等待安装
5)安装完毕,窗口会自动关闭。
这时我们按下win + r,输入cmd,进入命令提示符。
输入mount -h,查看下mount帮助信息(小技巧:未提示错误,也说明咱正确安装了NFS客户端)
6)Win7执行挂载NFS操作
挂载格式:mount \NFS的IP\NFS目录名 挂载盘符
取消挂载:umount 挂载盘符
mount \\192.168.1.251\tmp\test Z:
7)我们按下win+e,打开计算机资源管理器。可以看到,我们挂载的Z盘已存在
如果小伙们对DOS命令不太熟悉,我们也可以采用映射网络驱动器的方式
(和第六步实现效果一样)
点击完成,完成网络驱动器映射
以上两种方式,重启主机都需要手动从新映射。真是不厌其烦,因此我们可以写一个批处理脚本,并设置开启启动。步骤如下:
# 编写nfs.txt文件
@echo off
start "C:\Windows\System32\cmd.exe"
net use Z: \\192.168.1.251\tmp\test
taskkill /f /im cmd.exe
exit
# 编写完毕,修改nfs.txt后缀为:nfs.bat。(后缀为bat结尾的,为Windows可识别的批处理脚本)
#以下是命令解释:
#第一行命令作用:关闭脚本中命令执行的回显过程,那@echo on呢?聪明的您应该知道命令作用了
#第二行:启动cmd.exe程序
#第三行:将NFS服务器共享目录映射至本地Z盘
#第四行:命令执行完毕,关闭cmd命令窗口
#第五行:退出
(批处理脚本编写完毕,我们可双击nfs.bat
,手动查看是否映射成功。)
接下来完成最后一步,将nfs.bat
批处理文件加入Windows开启自启动目录
开始-->所有程序-->启动;右键打开-->将桌面上的
nfs.bat放入
samba环境搭建
samba服务器用于把Linux下的文件夹以windows共享的方式,共享给主机或局域网内其他的主机。
[1] 基本原理
samba服务器最好搭建在Linux系统上面,这里是在虚拟机(ubuntu)上搭建samba服务器,搭建步骤如下:
1. 确认虚拟机和外网连通
2. 安装samba服务软件并启动
3. 配置共享目录
4. 重启samba服务
5. 创建samba用户
6. 测试samba服务
[2] 具体过程
1. 确认网络连通
在ubuntu上,安装软件需要网络的支持,所以安装软件前必须保证已经联网,且DNS配置正确。检测命令如下:
ubuntu@ubuntu:~$ ping –c4 www.baidu.com
PING www.a.shifen.com(119.75.218.77) 56(84) bytes of data.
64 bytes from119.75.218.77: icmp_req=1 ttl=128 time=68.1 ms
表示网络和DNS已经配置正确
2. 安装samba服务软件
ubuntu@ubuntu:~$sudo apt-get install samba smbfs
3. 配置共享目录
打开我给你们发的smb.conf(在smb文件夹中),配置最后5行如下:
[ubuntu]
path=/home/caihuanming/samba
validusers=caihuanming
public=yes
writable=yes
在配置文件中找到usershare allowguests = yes
,在后面一行添加 usershare owner only = false
,设置后退出或重启ubuntu账户之后,局域网中的windows电脑即可访问该共享文件。
ubuntu是当前共享目录的用户,这里是将ubuntu的用户目录共享给主机。配置完成后拷贝到/etc/samba/目录下覆盖默认目录。
ubuntu@ubuntu:~$sudo cp smb.conf /etc/samba/
或直接修改
root@ubuntu:~# sudo vim /etc/samba/smb.conf
4. 重启samba服务
ubuntu@ubuntu:~$sudo service smbd restart
5. 创建samba用户
ubuntu@ubuntu:~$sudo smbpasswd -a Ubuntu
root@ubuntu:~#sudo smbpasswd -a caihuanming
创建samba用户,这里最后跟Linux用户名及密码相同,否则不便于记忆。
6. 测试samba服务
在主机下用windows系统访问虚拟机共享,如下图:
在地址一栏输入如下信息
出现如下图所示
或者可以使用我们的虚拟机的ip地址登录:
例如:\\192.168.1.125