ubuntu服务器设置

Upon successful installation of your Debian / Ubuntu server, a few configuration steps are essential in enhancing the security and functionality of the server. In this guide, we will take you through the basic steps in the initial server setup of Debian 9 / Ubuntu 18.04 server. These basic steps will fortify your server and allow the execution of subsequent operations in a seamless manner.

成功安装Debian / Ubuntu服务器后,一些配置步骤对于增强服务器的安全性和功能至关重要。 在本指南中,我们将引导您完成Debian 9 / Ubuntu 18.04服务器初始服务器设置中的基本步骤。 这些基本步骤将加强您的服务器,并允许以无缝方式执行后续操作。

以root用户身份登录 (Login as root user)

The initial step in setting up your server is to log in as the root user. But first, you need to have your server’s IP address and the Password or a private key for authentication. To log in, open your Linux terminal and execute the command below. Be sure to substitute the server’s IP address with your IP address.

设置服务器的第一步是以root用户身份登录。 但是首先,您需要具有服务器的IP地址和密码或用于身份验证的私钥。 要登录,请打开Linux终端并执行以下命令。 确保用您的IP地址替换服务器的IP地址。

ssh user@ip-address

In this guide, we are using Ubuntu 18.04 server with a Public IP address 38.76.11.180.

在本指南中,我们使用公共IP地址为38.76.11.180 Ubuntu 18.04服务器。

To log in using ssh via the terminal, the command is

要通过终端使用ssh登录,命令为

ssh root@38.76.11.180

If you are login in for the first time, you will be required to verify the server’s authenticity.

如果您是首次登录,则将需要验证服务器的真实性。

Type ‘Yes’ and hit Enter. You will then be prompted for the server’s Password. Provide the correct Password and hit Enter.

输入“是”,然后按Enter。 然后将提示您输入服务器的密码。 提供正确的密码,然后按Enter。

This will drop you into the server’s shell prompt as shown below

这将使您进入服务器的shell提示符,如下所示

Great! Now that we have successfully logged in to our server, let’s move over to the second step

大! 现在我们已经成功登录到服务器,让我们继续第二步

创建一个新用户 (Create a New user)

For best security practices, use of the root account for administrative tasks is highly discouraged. Why is that the case? Running your server as root leaves you prone to making unintentional and costly mistakes which may be detrimental to your server. You can easily do something harmful to your system which may cause irreversible damage to your system.

为了获得最佳安全性实践,强烈建议不要将root帐户用于管理任务。 为什么会这样? 以root用户身份运行服务器会使您容易犯意外的代价高昂的错误,这可能对服务器有害。 您可以轻松地做一些对您的系统有害的事情,这可能会对您的系统造成不可挽回的损害。

For this reason, we are going to create a new non-root user and later grant it administrative privileges. This way, every time you try to execute a root-level task, you will be prompted for the password. This will give you some time to pause and think about the consequences for executing the command and stop in your tracks if you notice a costly mistake in the command execution.

因此,我们将创建一个新的非root用户,并在以后为其授予管理权限。 这样,每次尝试执行根级任务时,系统都会提示您输入密码。 如果您发现命令执行中的代价高昂的错误,这将为您提供一些时间来暂停和思考执行命令的后果,并停在轨道上。

To create a new user run

创建新用户运行

adduser user_name

We are going to add a new user ‘james’

我们将添加一个新用户“ james”

adduser james

You will be prompted for the user’s password and a few additional questions. provide the necessary information

系统将提示您输入用户密码和一些其他问题。 提供必要的信息

Perfect! to verify the creation of the user view the /etc/passwd and confirm the existence of the user.

完善! 要验证用户的创建,请查看/etc/passwd并确认用户的存在。

cat /etc/passwd

Sample Output

样本输出

The last line above displays information of the newly created user.

上方的最后一行显示新创建的用户的信息。

向新用户授予管理权限 ( Grant Administrative privileges to the New user)

So far the new user has regular account privileges. We need to setup root privileges to our newly created user so that they can perform administrative tasks by appending sudo before any operation.

到目前为止,新用户具有常规帐户特权。 我们需要为新创建的用户设置root特权,以便他们可以通过在执行任何操作之前附加sudo来执行管理任务。

To accomplish this, we need to add the user to the sudo group to avoid logging out and logging in as root every time when performing administrative tasks.

为此,我们需要将用户添加到sudo组,以避免每次执行管理任务时注销并以root用户身份登录。

The syntax for achieving this is

实现此目的的语法是

usermod -aG sudo user_name

In this case, the command for granting sudo privileges to user ‘james’ will be

在这种情况下,向用户“ james”授予sudo特权的命令为

usermod -aG sudo james

You can now log out and log in with the new user using the command as indicated earlier

现在,您可以使用前面所述的命令注销并使用新用户登录

ssh james@38.76.11.180

After successful login by verifying the authenticity of the server and providing the correct password, you can now you can execute any superuser tasks by preceding the command with sudo

通过验证服务器的真实性并提供正确的密码成功登录后,您现在可以通过在命令前加上sudo来执行任何超级用户任务。

For instance, to update the system using the account execute the command below

例如,要使用帐户更新系统,请执行以下命令

sudo apt update

You will be prompted for a password and after providing it, the operation will commence.

系统将提示您输入密码,并在提供密码后开始操作。

配置基本防火墙 (Configure a basic Firewall)

Debian and Ubuntu servers use the UFW firewall to allow or deny traffic into or out of the server.

Debian和Ubuntu服务器使用UFW防火墙来允许或拒绝进出服务器的流量。

To view existing connection in the firewall execute

要查看防火墙中的现有连接,请执行

sudo ufw app list

As expected, OpenSSH will be displayed because we are currently using ssh to connect to the server

正如预期的那样,将显示OpenSSH,因为我们当前正在使用ssh连接到服务器

Output

输出量

To allow a connection via the firewall, execute the following command.

要允许通过防火墙的连接,请执行以下命令。

ufw allow service_name

For example, to allow SSH connections for a new server run:

例如,要允许新服务器运行SSH连接:

sudo ufw allow OpenSSH

Output

输出量

To open a specific port on the firewall, use the syntax

要打开防火墙上的特定端口,请使用以下语法

sudo ufw allow port/protocol

For example

例如

sudo ufw allow 80/tcp

Output

输出量

To check the status of the firewall execute

要检查防火墙的状态,请执行

sudo ufw status

Output

输出量

To enable the firewall, run the following command.

要启用防火墙,请运行以下命令。

sudo ufw enable

When prompted type yes and press ‘ENTER’ to enable the firewall and effect the changes

出现提示时,键入“ yes ,然后按“ ENTER”以启用防火墙并影响更改

To view the status of the firewall again and existing connections/open ports, execute the command

要再次查看防火墙的状态以及现有的连接/打开的端口,请执行以下命令

为新用户启用无密码身份验证 (Enable Passwordless authentication for the new user)

At the moment, we are connecting to our server using the SSH protocol with password authentication. For best security practices, setting up of SSH keys without password authentication is highly recommended. SSH keys come in a pair: a public key and a private key.
The Private key resides on the client machine while the public key resides on the server we are connecting to. Once the SSH key authentication is set up, Password authentication should be disabled. This ensures that only the user with the private key can connect to the server on which the public key resides.

目前,我们正在使用带有密码验证的SSH协议连接到我们的服务器。 为了获得最佳的安全性,强烈建议您设置没有密码验证的SSH密钥。 SSH密钥成对出现:公共密钥和私有密钥。
私钥驻留在客户端计算机上,而公钥驻留在我们连接的服务器上。 设置SSH密钥身份验证后,应禁用密码身份验证。 这样可以确保只有具有私钥的用户才能连接到公钥所在的服务器。

步骤1.生成公钥 (Step 1. Generate the public key)

To generate the key pair, run the command below on the client machine

要生成密钥对,请在客户端计算机上运行以下命令

ssh-keygen

You will get the output as shown

您将得到如下所示的输出

Hit ENTER to save the key pair is .ssh/ subdirectory in your home directory. Alternatively, you can specify your own path.

按ENTER键,将密钥对保存在主目录中的.ssh/子目录中。 或者,您可以指定自己的路径。

Next, you will be prompted for a secure passphrase which is highly recommended to add an extra protective layer. If you wish to do without a passphrase, hit ENTER.

接下来,系统将提示您输入安全密码,强烈建议您添加一个额外的保护层。 如果您希望不使用密码短语,请按Enter。

Finally, you will get the following output

最后,您将获得以下输出

At this point, you have both the private and public key. They have been saved in the .ssh directory in your home directory. In this case, the path is /root.ssh

此时,您将拥有私钥和公钥。 它们已保存在主目录的.ssh目录中。 在这种情况下,路径为/root.ssh

cat /root/.ssh

Output

输出量

id_rsa is the private key

id_rsa是私钥

id_rsa_pub is the public key

id_rsa_pub是公钥

步骤2.复制公钥 (Step 2. Copy the Public Key)

To copy the public key to the server, we are going to use the ssh-copy-id command.

要将公钥复制到服务器,我们将使用ssh-copy-id命令。

The Syntax will be

语法将是

ssh-copy-id user@remote-server

In this case, we shall execute

在这种情况下,我们将执行

ssh-copy-id james@38.76.11.180

You will get output similar to the one below

您将获得类似于以下内容的输出

The id_rsa.pub key which is the public key has been copied to the server.

作为公共密钥的id_rsa.pub密钥已复制到服务器。

Now if you try logging in using the syntax ssh user@server-ip , you will not be prompted for a password!

现在,如果您尝试使用ssh user@server-ip语法ssh user@server-ip ,则不会提示您输入密码!

禁用密码认证 ( Disable Password authentication)

Someone can still log in to our server if they got hold of the password. To eliminate this risk, we need to disable SSH password authentication.

如果他们掌握了密码,仍然可以登录我们的服务器。 为了消除这种风险,我们需要禁用SSH密码身份验证。

To achieve this, we are going to open SSH’s configuration file

为此,我们将打开SSH的配置文件

sudo vim /etc/ssh/sshd_config

Search for the section indicated as PasswordAuthentication .

搜索指示为PasswordAuthentication的部分。

Comment out the line and set the value to no

注释掉该行并将值设置为no

Save and Exit.

保存并退出。

To implement the changes restart the SSH daemon

要实施更改,请重新启动SSH守护程序

sudo systemctl restart ssh

Now you can launch a new terminal and try to log in using the password and see whether Password authentication has been disabled.

现在,您可以启动新终端并尝试使用密码登录,并查看是否已禁用密码身份验证。

最后的话 (Final word)

At this point, you should have access to the server using SSH-key based authentication configured on your remote server and not using the SSH password.

此时,您应该可以使用在远程服务器上配置的基于SSH密钥的身份验证访问服务器,而不必使用SSH密码。

Your server is now fully set up and has a solid security foundation. Ensure to keep your public key safe. Away from prying eyes

ubuntu服务器设置_Ubuntu服务器设置–安全最佳实践相关推荐

  1. ubuntu下php服务器搭建_Ubuntu服务器下搭建php运行环境的方法

    本文实例讲述了Ubuntu服务器下搭建php运行环境的方法.分享给大家供大家参考,具体如下: 安装 Apache2: sudo apt-get install apache2 安装PHP模块: sud ...

  2. ubuntu mysql快捷键_ubuntu快捷键设置大全

    转自:http://xiongyongyuan1234.blog.163.com/blog/static/5566116820103845725657/ GNOME应用技巧: 通用快捷键 Alt + ...

  3. 4种分支机构服务器管理最佳实践—Vecloud微云

    管理远程办公室/分支机构的站点通常需要大量的计算资源,但通常缺少现场IT人员.因此,与传统数据中心相比,部署和管理分支机构服务器需要IT经理对硬件和管理工具的选择,域控制器的放置以及监视和自动化策略的 ...

  4. RocketMQ中高性能最佳实践-网易云课笔记

    RocketMQ中高性能最佳实践 最佳实践之Producer 降低服务器管理的复杂性和管理成本 最佳实践之Consumer 最佳实践之NameServer JVM与Linux内核配置 JVM配置 Li ...

  5. 【360开源】Pika最佳实践

    奇技指南: Pika是360 热门的c++开源项目,基于rocksdb开发的大容量类Redis存储,力求在完全兼容Redis协议.继承Redis便捷运维设计的前提下通过持久化存储方式解决Redis在大 ...

  6. 360开源的类Redis存储系统Pika的最佳实践

    360DBA及基础架构团队根据360内部的Pika使用经验及社区用户的问题反馈,整理了以下这些Pika的最佳实践分享给大家. Pika最佳实践之一 在群里提问主动带上版本号能大幅度加快问题解决速度.( ...

  7. 程序员为什么应该旗帜鲜明地反对“最佳实践”?

    让第一个版本的系统混乱一点,或许是件好事. 作者 | 黄峰达,CSDN 博客专家 Phodal 责编 | 唐小引 头图 | 作者绘制并授权 CSDN 使用 出品 | CSDN(ID:CSDNnews) ...

  8. samba服务器新建文件权限,Ubuntu中安装samba服务器创建文件共享的方法及普通用户设置共享的问题解析...

    Ubuntu中安装samba服务器创建文件共享的方法及普通用户设置共享的问题解析 发布时间:2011-01-04 19:10:00   作者:佚名   我要评论 本文是基于ubuntu桌面版进行操作说 ...

  9. ubuntu下设置DNS服务器

    当网卡通过DHCP获取IP地址时,DHCP会根据DHCP服务器配置,自动给系统分配DNS服务器地址,此时执行 ping www.baidu.com 动作,会将百度的域名在本地转换成ip地址,然后走路由 ...

最新文章

  1. java学习总结:3
  2. mysql中两种备份方法的优缺点_Mysql两种存储引擎的优缺点
  3. 使用Oracle中fileopen命令加载文件时遇到的怪问题(转)
  4. MySQL ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
  5. css圆角box(网上流行用b标签)
  6. android 下滑,Android实现下滑和上滑事件
  7. android活动开始,android – 点击谷歌地图标记infoWindow开始活动
  8. 王思聪欠款1.5亿成被执行人 孙宇晨:我帮你还钱!
  9. docker hub push_Docker系列-(2) 镜像制作与发布
  10. 没能 PK 掉 WiFi 的 Li-Fi,可能是 5G 请来的救兵
  11. [zz] 海洋环境的光能传递
  12. 51视频编辑压缩官网
  13. SQL SEVER登录失败,无法连接服务器或已成功与服务器建立连接,但是在登录过程中发生错误。(Win10版本,SQL 2019)
  14. 致敬第一个1024(第一次Java代码编程)
  15. 用html5做课件,涨姿势┃一分钟PPT轻松转化H5,让“课件”动起来
  16. Labview Actorfromwork ESA(发布订阅模式,即观察者模式)Demo,整体操作过程已录制视频
  17. 新买的电脑如何用U盘装系统
  18. 利用Pandas改变Excel的填充背景颜色,包含奇偶行等设置
  19. redis再战之AKF的Z轴拆分和代理集群搭建《八》
  20. 中国手机游戏业的若干矛盾

热门文章

  1. Oracle 11g完全卸载(Windows)
  2. 使用模板实现asp代码和页面分离_asp技巧
  3. 关于MATLAB实现的数字信号处理(四)
  4. [转载] PyTorch: 序列到序列模型(Seq2Seq)实现机器翻译实战
  5. [转载] numpy入门4:线性代数
  6. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_08 Map集合_3_Map接口中的常用方法...
  7. MySql 入门.md
  8. 树莓派3B 开启串口
  9. 大小端判断及相互转化
  10. hashtable的C++实现