用于Ubuntu Server后期安装的简单BASH脚本

时间:2020-03-21 11:47:12  来源:igfitidea点击:

脚本Ubuntu-server-setup 可在新安装后自动进行Ubuntu服务器的设置和配置。
该脚本将执行以下操作:

  • 添加具有sudo访问权限的新用户帐户
  • 为新用户帐户添加公共ssh密钥
  • 禁用对服务器的密码身份验证
  • 拒绝root登录服务器
  • 设置简单的防火墙(UFW)
  • 根据计算机的已安装内存创建交换文件
  • 设置服务器的时区(默认值为“亚洲/新加坡”。我们可以提及时区)
  • 安装网络时间协议

在此教程中,我们将了解如何在Ubuntu 16.04 LTS服务器中运行此脚本。
这可能也适用于其他Ubuntu版本。

Ubuntu-server-setup-Ubuntu服务器后期安装的BASH脚本

在使用此脚本之前,我们需要在客户端系统上创建公共ssh密钥。
运行脚本时将需要此。

要创建公共ssh密钥,请运行:

$ssh-keygen -t rsa

输入两次密码。

Generating public/private rsa key pair.
Enter file in which to save the key (/home/sk/.ssh/id_rsa): 
Created directory '/home/sk/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/sk/.ssh/id_rsa.
Your public key has been saved in /home/sk/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:JFEf6UJEHVmuU/wO1zFUeXyE5oxhLCBfJOKB4yoX9mI Hyman@theitroad
The key's randomart image is:
+---[RSA 2048]----+
| .=+*+=*. ==|
| o. *.+=++ +.+|
| . .o.o..o+* oo|
| o . o. .o..o.o|
| . + S.o . o .|
|. E . . + |
| + . . |
| |
| |
+----[SHA256]-----+

要查看新创建的SSH公钥,请打开一个新的Terminal窗口并运行:

$cat ~/.ssh/id_rsa.pub

复制ssh公钥并确保其安全。
我们稍后必须输入此密钥。

现在,登录到Ubuntu服务器。
使用以下命令安装“ git”:

$sudo apt-get update
$sudo apt-get install git

接下来,使用以下命令获取Ubuntu-server-setup脚本:

$git clone https://github.com/jasonheecs/ubuntu-server-setup.git

上面的命令会将最新版本克隆到当前工作目录中名为“ ubuntu-server-setup”的文件夹中。

转到Ubuntu-server-setup目录并运行脚本,如下所示。

$cd ubuntu-server-setup/
$bash setup.sh

现在,我们必须回答一系列问题。
相应地回答。
首先,输入新的用户名:

Enter the username of the new user account:theitroad
Enter new UNIX password:
Retype new UNIX password:
Adding user `theitroad' ...
Adding new group `theitroad' (1001) ...
Adding new user `theitroad' (1001) with group `theitroad' ...
Creating home directory `/home/theitroad' ...
Copying files from `/etc/skel' ...
Changing the user information for theitroad
Enter the new value, or press ENTER for the default
 Full Name []: Senthilkumar
 Room Number []: 123
 Work Phone []: 
 Home Phone []: 
 Other []: 
Is the information correct? [Y/n] y

复制/粘贴我们在上一步中创建的Public ssh密钥。

Paste in the public SSH key for the new user:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDg2S1/s3ZB/O/KBVT6OP0JEQKPQ3QjTOaWhAjFN9CJrb2IVYGx0P8SA52d81tw0UJXByXkPpxvGwXYbWukV6bF/mdm8kj+e+BxEYlq2sXbFdwW+lW0GwTYMq2tIRQ1Iec6QFLGNSi0QjLcIhG/JTg5TrmK9KyECCHRFQfR7cDWRXo2tBtMkDxdJdH/D4BdmO//rvSFgY4QiqjwmkKqgpLrK5ZvvfgjKWtO0+1D+hfrm+cTNGByWHvOAj3j5HyXB6dzNaKswCQygzkvESzXI6eyMWYW3+QNz2PBHbof+HVHSd9av7aC8IO31aUMsyvrTcSB3wHk0NpmrDfqLRvOspNb Hyman@theitroad
Running setup script...

现在输入时区:

Enter the timezone for the server (Default is 'Asia/Singapore'):
Asia/Kolkata
Timezone is set to Asia/Kolkata
Installing Network Time Protocol... 
Setup Done! Log file is located at output.lo

请注意,除了我们刚刚创建的用户(即本例中的theitroad)之外,我们不能与其他任何用户一起使用SSH服务器。
因为此脚本将禁用所有用户(包括root用户)的基于密码的ssh身份验证。
而且,我们只能从已创建公共ssh密钥并与远程系统共享的客户端系统上SSH到Ubuntu服务器。
如果要从其他系统进行ssh,则应将公共ssh密钥手动上载到我们希望能够使用SSH登录的远程服务器。

从现在开始,每当我们尝试如下所示SSH到服务器时,系统都会要求我们输入在客户端系统中创建的密码。

$ssh Hyman@theitroad

输入密钥的密码:

Enter passphrase for key '/home/sk/.ssh/id_rsa':

Ubuntu-server-setup脚本刚刚完成了基本的安装后任务。
开始安装要在服务器中运行的其他内容。