实验 3 服务器的配置与管理

使用包管理安装你需要的大部分内容


SSH 服务的源码安装

要安装 SSH, 使用如下命令:

# apt-get install openssh-server openssh-client

什么,你说实验指导书要求源码编译安装?请允许我做一个标准微笑。

首先安装构建工具组:

# apt-get install build-essential autoconf git

这会安装一大堆用于开发的软件包, autoconfgit,后面会用到。

由于 OpenSSH 依赖于 OpenSSL, 而 OpenSSL 又依赖于 zlib. 秉承着 HaRdCoRe FrIcKiNg DuMdUm 的精神,我们选择从源码编译。

获取 zlib 的源码:

$ wget https://www.zlib.net/zlib-1.2.11.tar.gz

解压、进入目录,然后运行:

$ ./configure
$ make test
# make install

如果上述命令都没有报错,那么可以继续进行。

获取 OpenSSL 的源码:

$ git clone https://github.com/openssl/openssl.git

进入编译:

$ cd openssl
$ ./config shared --prefix=/opt/openssl --openssldir=/opt/openssl
$ make depend
$ make
$ make test
# make install

如果上述过程都没有出错,那么可以继续进行。

获取 OpenSSH 源码:

$ git clone https://github.com/openssh/openssh-portable

添加 sshd 用户(不要手动编辑 /etc/passwd):

# groupadd -r sshd
# useradd -M -r -s /bin/false -d /var/empty -g sshd sshd

启动编译:

$ cd openssh-portable
$ autoreconf
$ export LD_LIBRARY_PATH="/opt/openssl/lib:${LD_LIBRARY_PATH}"
$ ./configure --prefix="/opt/openssh" --with-ldflags="-L/opt/openssl/lib" CFLAGS="-I/opt/openssl/include"
$ make
$ make tests
# make install

如果以上命令均未报错,那么安装成功。

启动、自启动和停止 SSH 服务

首先我们将 OpenSSH 添加为一个服务。

# cd /etc/systemd/system
# vim sshd.service

键入以下内容:

[Unit]
Description = OpenSSH Server Daemon
After = network.target

[Service]
ExecStart = /opt/openssh/sbin/sshd

[Install]
WantedBy = multi-user.target

保存退出。

通知 systemctl 重载服务列表:

# systemctl daemon-reload

启动 SSH 服务:

# systemctl start sshd

没有消息就是好消息。

要查看 SSH 服务的运行状态,使用:

$ systemctl status sshd

应该显示为绿色的 active.

要使 SSH 服务随系统启动,使用:

# systemctl enable sshd

要停止 SSH 服务,使用:

# systemctl stop sshd

安装、启动和停止 Apache

这次总算是不需要自己编译了。

安装 Apache:

# apt-get install apache2

启动 Apache:

# systemctl start apache2

停止 Apache:

# systemctl stop apache2

至于为啥不配置,因为确实不需要单独配置 ServerName.

为 Apache 配置 TLS 支持

进行自签名证书的生成:

接下来,用户的输入会以方括号表示。

$ openssl genrsa -des3 -out wtf.key 4096
Generating RSA private key, 4096 bit long modulus (2 primes)
....................................++++
.................++++
e is 65537 (0x010001)
Enter pass phrase for wtf.key: [<输入密码>]
Verifying - Enter pass phrase for wtf.key: [<确认密码>]
$ openssl req -new -key wtf.key -out wtf.csr
Enter pass phrase for wtf.key: [<输入之前的密码>]
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]: [CN]
State or Province Name (full name) [Some-State]: [.]
Locality Name (eg, city) []: [.]
Organization Name (eg, company) [Internet Widgits Pty Ltd]: [.]
Organizational Unit Name (eg, section) []: [.]
Common Name (e.g. server FQDN or YOUR name) []: [.]
Email Address []: [.]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: [.]
An optional company name []: [.]
$ openssl x509 -req -days 365 -in wtf.csr -signkey wtf.key -out wtf.crt 
Signature ok
subject=C = CN
Getting Private key
Enter pass phrase for wtf.key: [<输入之前的密码>]

至此生成了 wtf.keywtf.crt. 复制到对应目录:

$ cp wtf.crt /etc/ssl/certs
$ cp wtf.key /etc/ssl/private

将 Apache 的默认 SSL 配置文件应用于整个站点:

# ln -s /etc/apache2/sites-available/default-ssl.conf  /etc/apache2/sites-enabled/default-ssl.conf

编辑默认 SSL 配置文件:

# vim /etc/apache2/sites-enabled/default-ssl.conf

定位到 DocumentRoot 行(使用 / 指令),然后查阅对应内容是否一致,不一致则修改,不存在则追加(善用 / 指令),省略号代表内容无需注意。

...
Listen 443
<VirtualHost _default_:443> # 注意在此行之后添加
DocumentRoot "/var/www/http"
ServerName localhost:443 # 注意
ServerAdmin ...
ErrorLog "..."
TransferLog "..."

SSLEngine On
SSLSertificateFile "/etc/ssl/certs/wtf.crt"
SSLCertificateKeyFile "/etc/ssl/private/wtf.key"
SSLOptions +StrictRequire
</VirtualHost> # 注意在此行之前添加
...

保存并关闭 vim.

要重启 Apache: # systemctl restart apache2. 没有消息就是好消息。

要进行测试,访问 https://localhost/

编译 Linux 内核(幸亏没有安装)

获取 Linux 内核源码:到 https://www.kernel.org/ 手动下载源码。什么你说你看不懂英文?首页上的亮黄色按钮,点一下玩一年,编译不花一分钱*。

下载之后解压,进入目录,运行:

$ make menuconfig

打开文字菜单配置。

然后就是进行内核配置。要让内核能正常使用,要启用内核中包含对相应硬盘驱动器、文件系统和外设驱动的支持,以下是一个范例配置:

Device Drivers -->
  Generic Driver Options -->
    [*] Maintain a devtmpfs filesystem to mount at /dev
    [ ]   Automount devtmpfs at /dev, after kernel mounted rootfs
  SCSI device support -->
    <*> SCSI disk support
File systems -->
  <*> Second extended fs support
  <*> The Extended 3 (ext3) filesystem
  <*> The Extended 4 (ext4) filesystem
  DOS/FAT/NT Filesystems -->
    <*> MSDOS fs support
    <*> VFAT (Windows-95) fs support
    <*> NTFS
Pseudo Filesystems -->
  [*] /proc file system support
  [*] Tmpfs virtual memory file system support (former shm fs)
Processor type and features  --->
  [*] Symmetric multi-processing support
HID support  --->
    -*- HID bus support
    <*>   Generic HID driver
    [*]   Battery level reporting for HID devices
      USB HID support  --->
        <*> USB HID transport layer
  [*] USB support  --->
    <*>     xHCI HCD (USB 3.0) support
    <*>     EHCI HCD (USB 2.0) support
    <*>     OHCI HCD (USB 1.1) support
-*- Enable the block layer --->
   Partition Types --->
      [*] Advanced partition selection
      [*] EFI GUID Partition support
Processor type and features  --->
    [*] EFI runtime service support 
    [*]   EFI stub support
    [*]     EFI mixed-mode support
Firmware Drivers  --->
    EFI (Extensible Firmware Interface) Support  --->
        <*> EFI Variable Support via sysfs

完成配置并保存配置后,运行如下命令进行内核编译:

$ make && make modules_install

内核编译通常需要 20 分钟不等,坐和放宽。

如果我们还要把这个内核装好,那么之上的配置还是有限的,我们接下来还需要配置内核模块和引导等内容。此处不再赘述。

* 电费和计算机设备购置费不算在内

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容