# UNIX网络编程源码实战——搭建Linux环境
## 安装Linux操作系统
我们用的是从搬瓦工网站(http://banwagong.cn/ )上购买的主机。
由于是购买的Linux主机,我们需要先确定Linux操作系统的类型。
```
[root@host ~]# more /etc/issue
CentOS release 6.8 (Final)
Kernel \r on an \m
```
## 搭建环境
### 创建新用户 furnace
```
[root@host ~]# adduser furnace
[root@host ~]# passwd furnace
Changing password for user furnace.
New password:
```
### 给用户 furnace 添加 root 权限
1. 给文件 /etc/sudoers 添加写权限。
```
[root@host ~]# chmod u+w /etc/sudoers
```
2. 编辑文件
```
[root@host ~]# vi /etc/sudoers
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
furnace ALL=(ALL) ALL (新增)
```
3. 撤销文件 /etc/sudoers 的写权限
```
[root@host ~]# chmod u-w /etc/sudoers
```
### 安装必要的软件
1. 安装 vim
```
[root@host ~]# yum install vim
```
2. 安装 gcc
```
[root@host ~]# yum install gcc
[root@host ~]# gcc --version
gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-18)
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```
3. 安装 make
```
[root@host ~]# yum install make
[root@host ~]# make --version
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for i386-redhat-linux-gnu
```
4. 安装 tar
```
[root@host ~]# yum install tar
```
## 下载源代码
书中所有示例的源代码也可以从 http://www.turingbook.com/ 获得 (http://www.ituring.com.cn/book/164 )。
```
[furnace@host unp]$ tar -xzvf unpv13e.tar.gz
[furnace@host unp]$ ls
unpv13e unpv13e.tar.gz
```
## 编译源代码
查看文件 README。在执行完配置和库文件的编译之后,编译试例程序 daytimetcpcli,并运行 ./daytimetcpcli 127.0.0.1,测试是否编译成功。
```
[furnace@host unp]$ cd unpv13e
[furnace@host unpv13e]$ ./configure
[furnace@host unpv13e]$ cd lib
[furnace@host unpv13e]$ make
[furnace@host unpv13e]$ cd ../libfree/
[furnace@host unpv13e]$ make
[furnace@host unpv13e]$ cd ../intro/
[furnace@host unpv13e]$ make daytimetcpcli
[furnace@host unpv13e]$ ./daytimetcpcli 127.0.0.1
```
至此,完成了 "Unix Network Programming, Volume 1: The Sockets Networking API (Third Edition)" 中的源代码运行环境的搭建,以及编译源代码的完整流程。后面可以实战相应的实例了。
领取专属 10元无门槛券
私享最新 技术干货