Linux basic network configuration and user management (2)

        Before getting familiar with Linux operation commands, we must first set up the Linux operating system environment. The distribution version of Linux we choose here is Ubuntu. After installing the Ubuntu operating system, we need to perform a series of initial operations on the system , such as setting the administrator password, network configuration, baseline hardening and other configurations.

1. Configure administrator account

After installing the Ubuntu system, we need to perform initial configuration, such as setting the administrator password, opening the network interface, and so on.

First, you need to configure the password of the administrator root user, enter sudo passwd root to set the root user password setting. Because the default root password of Ubuntu is random, that is, there is a new root password every time it is turned on, and it is not required to set the root password when installing the system, so Ubuntu cannot use root to log in to the system directly after the installation is complete, you need to use The user set during installation logs in to the system and sets the root password by itself.

1.1 Log in to the system and set the root password

arshou@arshou-ser:~$ sudo passwd root

[sudo] password for arshou:

New password:

Retype new password:

passwd: password updated successfully

arshou@arshou-ser:~$

2 Network configuration

2.1 View host IP

All available ethernet interfaces can be quickly identified using the ip command as shown below.

2.2 Temporary IP address allocation

For temporary network configurations, you can use the ip command, which is also found on most other GNU/Linux operating systems. The ip command allows you to configure settings that take effect immediately, but they are not persistent and are lost after a reboot.

If you need to temporarily configure an IP address, you can use the IP command in the following ways. Modify the IP address and subnet mask according to the actual network conditions.

:~$ sudo ip addr add 192.168.20.127/24 dev ens33   

The connection can then be set using the ip.

:~$ sudo ip link set dev ens33 up
:~$ sudo ip link set dev ens33 down

Use the IP command to verify the IP address configuration of ens33, as follows:

2.3 Configure the default gateway

The method of using the ip command to configure the default gateway is as follows: Please modify the default gateway address to meet your network requirements.

~$ sudo ip route add default via 192.168.20.2

The configuration of the default gateway can also be verified using the ip command as follows:

2.4 Static IP address allocation

To configure the system to use static address assignment, create a netplan configuration in the /etc/netplan/00-installer-config.yaml file. The examples below assume you are configuring the first Ethernet interface identified as eth0. Modify the Address, Routing, and Nameserver values ​​to suit your network needs.

Enter the network card configuration yaml file to configure (note: use the root user to modify, or add sudo in front of the command)

:~$ sudo you /etc/netplan/00-installer-config.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:

      dhcp4: no

      dhcp6: no

      addresses:
        - 192.168.20.125/24
      routes:
        - to: default
          via: 192.168.20.2

      nameservers:
          search: []
          addresses: [202.103.224.68]

That configuration can then be applied using the netplan command.

sudo netplan apply

note

The netplan in Ubuntu Bionic 18.04 LTS does not use the "to:default" syntax to specify the default route, the old gateway4:10.10.10.1 should be used instead.

2.5 DNS configuration

Traditionally, the /etc/resolv.conf file is a static configuration file that rarely needs to be changed, or is changed automatically through a dhcp client hook. Systemd-resolved handles nameserver configuration and should be interacted with through the systemd-resolve command. Netplan configures system -resolved to generate a list of domain name servers and domain names and put them in /etc/resolv.conf.

To configure a resolver, add the IP addresses of the appropriate nameservers for your network to the netplan configuration file. The search option can also be used for multiple domain names, so DNS queries will be appended in the order entered. For example, your network may have multiple sub-domains to search, example.com, and two sub-domains, sales.example.com and dev.example.com. If you wish to search multiple domains, your configuration may look like this :

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s25:
      addresses:
        - 192.168.20.125/24
      routes:
        - to: default
          via: 192.168.20.2
      nameservers:
          search: [example.com, sales.example.com, dev.example.com]
          addresses: [202.103.224.68, 8.8.8.8, 4.4.4.4]

3 SSH-Server Configuration Guide

3.1 Introduction to SSH

      

    SSH 的全称是安全的 Shell(Secure Shell)的简称,是一个应用程序提供安全通信的协议,通过SSH协议可以安全地访问服务器,因为SSH 具有成熟的公钥加密体系,在数据进行传输时进行加密,保证数据在传输时不被恶意篡改、破坏和泄露,能有效防止网络嗅探和IP欺骗等攻击,同时提供不同的身份认证方式和多个配置选项。SSH是由芬兰的一家公司开发的,但是因为受版权和加密算法的限制,现在很多人都转而使用OpenSSH,OpenSSH是SSH的替代软件,而且是免费的。

    OpenSSH是一个功能强大的工具集合,用于远程控制和在联网计算机之间传输数据。了解OpenSSH服务器应用程序的一些配置,以及如何在Ubuntu系统上更改它们。

    OpenSSH服务器组件sshd进程持续侦听来自任何客户机工具的客户机连接,当出现连接请求时,sshd根据连接的客户端工具的类型建立正确的连接。例如,如果远程计算机正在使用ssh客户端应用程序,OpenSSH服务器在身份验证后建立远程控制会话。如果远程用户使用scp连接到OpenSSH服务器,OpenSSH服务器守护进程在认证后启动服务器和客户端之间的文件安全拷贝。

3.2 OpenSSH 的基本概念

SSH 协议基于(C/S)架构。服务器(Server)允许客户端(Client)通过通信通道进行连接,并且该信道是经过加密的,信息交换通过 SSH 公私钥进行管理。

  • 在作为服务器的主机上部署 SSH 服务器组件,它由 openssh-server 包提供。
  • 在远程访问服务器的客户端机器上部署 SSH 客户端组件,它由 openssh-client 包提供,大多数 Linux 和 BSD 发行版都已经预装好了。

3.3 安装

OpenSSH客户端和服务器应用程序的安装非常简单。要在Ubuntu系统上安装OpenSSH客户端应用程序,在终端提示符下使用以下命令安装openssh-client。

:~$ sudo apt install openssh-client

要安装OpenSSH服务器应用程序和相关支持文件,在终端提示符下使用以下命令安装openssh-server。

:~$ sudo apt install openssh-server

3.4 配置

通过编辑/etc/ssh/sshd_config文件来配置OpenSSH服务器应用程序,在sshd配置文件中有许多指令控制通信设置和身份验证模式。以下是可以通过编辑/etc/ssh/sshd_config文件更改的配置指令的示例。

提示

在编辑配置文件之前,应该复制原始文件做为备份保护它不被写入,这样就可以将原始设置作为参考并在必要时重用。

复制/etc/ssh/sshd_config文件,并使用以下命令保护它不被写入,在终端提示符下执行以下命令:

:~$ sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.original
:~$ sudo chmod a-w /etc/ssh/sshd_config.original

3.5  /etc/ssh/sshd_config文件参数

/etc/ssh/sshd_config文件是OpenSSH系统的配置文件,允许你通过设置不同的选项来改变程序的运行方式。这个文件的每一行

包含“关键词-值”的匹配,其中“关键词”是忽略大小写的。下面列出来的是最重要的关键词.

编辑sshd_config文件(vi /etc/ssh/sshd_config),下面逐行说明一些重要选项参数.

Port

设置sshd监听的端口号。

ListenAddress 0.0.0.0

“ListenAddress”设置sshd服务器绑定的IP地址。

HostKey /etc/ssh/ssh_host_key

“HostKey”设置包含计算机私人密匙的文件。

LoginGraceTime 2m

“LoginGraceTime”设置如果用户不能成功登录,在切断连接之前服务器需要等待的时间(以秒为单位)。

PermitRootLogin prohibit-password

表示允许 root 账户登录,但是不能以密码的方式登录,所以只能以公私钥的方式登录,为了简便,我们不采用这种方式,而是采用 PermitRootLogin yes,这样,直接使用密码就可以登录了。

IgnoreRhosts yes

“IgnoreRhosts”设置验证的时候是否使用“rhosts”和“shosts”文件。

IgnoreUserKnownHosts yes

“IgnoreUserKnownHosts”设置ssh daemon是否在进行RhostsRSAAuthentication安全验证的时候忽略用户的“$HOME/.ssh/known_hosts”

StrictModes yes

“StrictModes”设置ssh在接收登录请求之前是否检查用户家目录和rhosts文件的权限和所有权。这通常是必要的,因为新手经常会把自己的目录和文件设成任何人都有写权限。

X11Forwarding no

“X11Forwarding”设置是否允许X11转发。

PrintMotd yes

“PrintMotd”设置sshd是否在用户登录的时候显示“/etc/motd”中的信息。

SyslogFacility AUTH

“SyslogFacility”设置在记录来自sshd的消息的时候,是否给出“facility code”。

LogLevel INFO

“LogLevel”设置记录sshd日志消息的层次。INFO是一个好的选择。查看sshd的man帮助页,已获取更多的信息。

RhostsAuthentication no

“RhostsAuthentication”设置只用rhosts或“/etc/hosts.equiv”进行安全验证是否已经足够了。

RhostsRSAAuthentication no

“RhostsRSA”设置是否允许用rhosts或“/etc/hosts.equiv”加上RSA进行安全验证。

RSAAuthentication yes

“RSAAuthentication”设置是否允许只有RSA安全验证。

PasswordAuthentication yes

“PasswordAuthentication”设置是否允许口令验证。

PermitEmptyPasswords no

“PermitEmptyPasswords”设置是否允许用口令为空的帐号登录。

3.6 启动远程连接

首先需要修改配置文件将PasswordAuthentication选项注释取消掉,然后:wq!保存并推出。

:~$ sudo vim /etc/ssh/sshd_config

配置文件修改后需要重启 sshd服务:

sudo systemctl start ssh

3.7 远程连接

配置完之后,我们就可以使用远程连接工具进行连接了,这里使用的与远程连接工具是SecureCRT 。

 首先,我们使用ifconfig获取到我们的 ip 地址

 然后,我们使用根据 ip 地址进行连接。

最后,我们成功进入我们的 Linux 系统。

3.7.1管理员用户运程登录配置

默认的配置为允许root登录,但是禁止root用密码登录,如果root需要使用密码登录,需要解除限制,修改配置即可,将默认配置注释掉,添加一行新的配置,修改/etc/ssh/sshd_config配置文件,如下:

PermitRootLogin prohibit-password → PermitRootLogin yes

修改配置文件后需要重启服务。

$ sudo systemctl restart sshd

3.8 修改的配置指令的例子:

(1)、要使OpenSSH服务器显示/etc/issue.net文件的内容作为登录前的提示内容(就是登录远程主机时在进入主机之前显示提示的内容),只需在/etc/ssh/sshd_config文件中添加或修改这一行:

Banner /etc/issue.net

(2)、并修改/etc/issue.net文件的内容,输入自定义的提示内容后保存文件即可。

(3)、修改/etc/ssh/sshd_config文件后,保存该文件,并在终端提示符下使用以下命令重启sshd服务器应用程序以使更改生效:

systemctl restart sshd.service

测试远程登录

3.8 SSH Keys,配置主机信任

SSH允许在两台主机之间进行身份验证,而不需要密码。SSH密钥认证使用私钥和公钥。

(1)、要生成密钥,请在终端提示符中输入:

~$ ssh-keygen -t rsa

这将使用RSA算法生成密钥。在撰写本文时,生成的密钥将有3072位。可以使用-b选项修改比特数。例如,要生成4096位的密钥,您可以这样做:

~$ ssh-keygen -t rsa -b 4096

在生成密钥过程中,系统将提示您输入密码。当提示创建密钥时,只需按Enter键即可。

(2)、默认情况下,公钥保存在~/.ssh/id_rsa.pub文件中。而其中 ~/.ssh/id_rsa为私钥。现在复制 id_rsa.pub文件到远程主机,并将其追加到~/.ssh/authorized_keys文件中,输入如下命令:

~$ ssh-copy-id -i .ssh/id_rsa username@remotehost

使用ssh-copy-id命令进行复制时,可以只指定复制本地的公钥文件,到远程主机,这个命令复制的公钥会自动追加到.ssh/authorized_keys文件中。

(3)、最后,仔细检查远程主机的authorized_keys文件上的权限,只有经过身份验证的用户应该具有读写权限。如果权限不正确,请通过以下方式更改它们:

~$ chmod 600 .ssh/authorized_keys

配置好之后现在应该能够SSH到远程主机,而不需要提示输入密码。

3.9 配置主机信任示例

(1)、准备两台主机环境

系统版本

 IP

用途

Ubuntu 22.04.1 LTS

192.168.20.125

服务器

Ubuntu 22.04.1 LTS

192.168.20.128

服务器

(2)、查看当前主机是否存在SSH密钥

root@arshou-ser:~# ip a | grep ens33

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000

    inet 192.168.20.125/24 brd 192.168.20.255 scope global ens33

root@arshou-ser:~#

root@arshou-ser:~# ll -a .ssh/

total 0

drwx------ 2 root root  29 Jun 17 07:31 ./

drwx------ 6 root root 173 Jun 24 03:20 ../

-rw------- 1 root root   0 Jun 17 07:31 authorized_keys

root@arshou-ser:~#

(3)、生成密钥,请在终端提示符中输入:

root@arshou-ser:~# ssh-keygen -t rsa -b 4096

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa

Your public key has been saved in /root/.ssh/id_rsa.pub

The key fingerprint is:

SHA256:J2K35uYA6bb8qQHY8uZEgKdk2ipEyCjfZgAhaswK3NY root@arshou-ser

The key's randomart image is:

+---[RSA 4096]----+

|+.               |

|@.. .            |

|OXoo E           |

|XO.o .           |

|*.* * o S .      |

|.= = o o +       |

|o + + . o        |

|.+ o o =.        |

|  . +oooo        |

+----[SHA256]-----+

root@arshou-ser:~#

root@arshou-ser:~# ll -a .ssh/

total 8

drwx------ 2 root root   61 Jun 24 03:49 ./

drwx------ 6 root root  173 Jun 24 03:20 ../

-rw------- 1 root root    0 Jun 17 07:31 authorized_keys

-rw------- 1 root root 3381 Jun 24 03:49 id_rsa   ------------------------>私钥

-rw-r--r-- 1 root root  741 Jun 24 03:49 id_rsa.pub   ------------------------>公钥

root@arshou-ser:~#

(4)、将公钥复制到其他主机,这里复制给192.168.20.128这个主机

root@arshou-ser:~# ssh-copy-id -i  .ssh/id_rsa.pub [email protected]

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"

The authenticity of host '192.168.20.128 (192.168.20.128)' can't be established.

ED25519 key fingerprint is SHA256:qVi8ZSlnwf3AfyM+Fd+FQfyffubQyxqCNhjHRigYuWE.

This key is not known by any other names

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys

[email protected]'s password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"

and check to make sure that only the key(s) you wanted were added.

root@arshou-ser:~#

(5)、在当前主机测试远程登陆至192.168.20.128,如下:

root@arshou-ser:~# ip a | grep ens33

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000

    inet 192.168.20.125/24 brd 192.168.20.255 scope global ens33

root@arshou-ser:~#

root@arshou-ser:~# ssh [email protected]                               #发现登录无需密码即可远程登录到远程主机

Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-75-generic x86_64)

 * Documentation:  Official Ubuntu Documentation

 * Management:     Log in

 * Support:        Ubuntu Pro | Ubuntu

  System information as of Sat Jun 24 04:00:10 AM UTC 2023

  System load:  0.0029296875        Processes:              249

  Usage of /:   42.2% of 483.27GB   Users logged in:        2

  Memory usage: 24%                 IPv4 address for ens33: 192.168.20.128

  Swap usage:   0%

 * Introducing Expanded Security Maintenance for Applications.

   Receive updates to over 25,000 software packages with your

   Ubuntu Pro subscription. Free for personal use.

     Ubuntu Pro | Ubuntu

Expanded Security Maintenance for Applications is not enabled.

0 updates can be applied immediately.

1 additional security update can be applied with ESM Apps.

Learn more about enabling ESM Apps service at Ubuntu Expanded Security Maintenance | Security | Ubuntu

Last login: Sat Jun 24 04:00:11 2023 from 192.168.20.125

root@ubuntu:~#

root@ubuntu:~# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000

    link/ether 00:0c:29:bf:61:c6 brd ff:ff:ff:ff:ff:ff

    altname enp2s1

    inet 192.168.20.128/24 brd 192.168.20.255 scope global ens33

       valid_lft forever preferred_lft forever

    inet6 fe80::20c:29ff:febf:61c6/64 scope link

       valid_lft forever preferred_lft forever

root@ubuntu:~#

4 用户管理

Ubuntu开发人员做出了一个谨慎的决定,在所有Ubuntu安装中默认禁用管理root帐户。这并不意味着根帐户已被删除或无法访问。它只是被给予了一个不匹配任何可能值的密码散列,因此可能不会自己直接登录。

4.1 添加和删除用户

adduser和addgroup根据命令行选项和/etc/adduser.conf中的配置信息向系统中添加用户和用户组。它们相比其底层的工具例如:useradd,groupadd和usermod更加友好。adduser在使用该命令创建用户是会在/home下自动创建与用户名同名的用户目录,系统shell版本,会在创建时会提示输入密码和可识别的特征,如全名、电话号码等。

当创建一个新用户时,adduser实用程序将创建一个名为/home/username的全新主目录默认配置文件是根据/etc/skel目录中的内容建模的,其中包括所有配置文件基础。

4.2 adduser命令介绍

 adduser [options] [--home DIR] [--shell SHELL] [--no-create-home] [--uid ID] [--firstuid ID] [--lastuid ID] [--ingroup GROUP |

 --gid ID] [--disabled-password] [--disabled-login] [--gecos GECOS] [--add_extra_groups] [--encrypt-home] user

选项说明:

--system  添加系统用户或组,该命令会创建1-999的UID,用户组不会增加,用户目录(/home/<username>)会增加,增加用户组

需要配合使用--group参数


--conf <file>  使用 <file>文件配置,而不是 /etc/adduser.conf配置。
--disabled-login  禁止用户帐户登录。
--disabled-password 禁止使用密码登录。

--uid <ID> 创建用户时指定用户的ID

--gid <ID> 将组ID设置为指定的<ID>。如果指定了用户,则还会将用户添加到组中。
--group 运行addgroup命令并添加用户组。如果指定了--system,则添加系统用户和组。
--home <directory>  将用户的主目录设置为<directory>目录。
--shell <shell>  使用<shell>路径作为用户的自定义shell。
--ingroup <group>  将用户添加到<group>而不是配置文件中的默认组。
--no-create-home  禁止用户创建主目录。

4.3 adduser用法示例

4.3.1 增加普通用户

  命令格式 sudo adduser <username>

该命令会自动增加用户主目录,增加UID以及GID,设置密码。创建的UID会大于1000

arshou@arshou-ser:~$ sudo adduser test

Adding user `test' ...

Adding new group `test' (1001) ...

Adding new user `test' (1001) with group `test' ...

Creating home directory `/home/test' ...

Copying files from `/etc/skel' ...

New password:

Retype new password:

passwd: password updated successfully

Changing the user information for test

Enter the new value, or press ENTER for the default

        Full Name []:

        Room Number []:

        Work Phone []:

        Home Phone []:

        Other []:

Is the information correct? [Y/n] y

arshou@arshou-ser:~$

查看用户是否增加可以通过查看/etc/passwd来确认。

4.3.2 增加系统用户

命令格式 sudo adduser --system <username>

该命令会创建1-999的UID,用户相同名称的用户组不会增加,新增的用户会放在nogroup组中,用户目录(/home/<username>)会增加。

arshou@arshou-ser:~$ sudo adduser --system sgrp

Adding system user `sgrp' (UID 113) ...

Adding new user `sgrp' (UID 113) with group `nogroup' ...

Creating home directory `/home/sgrp' ...

arshou@arshou-ser:~$

4.3.3 增加系统用户的同时增加用户组

命令格式 sudo adduser --group --system <name>

UID和GID在1至999,表示它是系统用户和组。

arshou@arshou-ser:~$ sudo adduser --group --system mtest

Adding system user `mtest' (UID 113) ...

Adding new group `mtest' (GID 118) ...

Adding new user `mtest' (UID 113) with group `mtest' ...

Creating home directory `/home/mtest' ...

arshou@arshou-ser:~$

4.3.4 增加自定义用户目录

命令格式 sudo adduser --home <directory> <username>

自定义用户家目录使用--home参数时,指定的家目录位置会自动创建,不用提前手工创建目录,示例如下:

arshou@arshou-ser:~$ sudo adduser --home /data/muser muser

Adding user `muser' ...

Adding new group `muser' (1002) ...

Adding new user `muser' (1002) with group `muser' ...

Creating home directory `/data/muser' ...

Copying files from `/etc/skel' ...

New password:

Retype new password:

passwd: password updated successfully

Changing the user information for muser

Enter the new value, or press ENTER for the default

        Full Name []:

        Room Number []:

        Work Phone []:

        Home Phone []:

        Other []:

Is the information correct? [Y/n] y

arshou@arshou-ser:~$

4.3.5 增加自定义用户ID

查看:

4.3.6 删除用户帐户及其主组的语法如下:

sudo deluser username

这样删除帐户不会删除它们各自用户的主文件夹。

4.3.7 永久删除用户

要从 Linux 系统中完全删除用户,请以 root 或 sudo 用户身份登录.

如果要删除用户及其主目录和邮件,请运行:

arshou@arshou-ser:~$ sudo deluser --remove-home test

Looking for files to backup/remove ...

Removing files ...

Removing user `test' ...

Warning: group `test' has no more members.

Done.

arshou@arshou-ser:~$

查看用户主目录已经被删除了。

4.3.8 用户配置文件介绍

Linux 系统安装之后会默认生成很多的用户,这些用户中的绝大多数是系统或服务正常运行所必需的用户,这些用户通常称为系统用户或伪用户。系统无法使用这些系统用户来登录系统,但也不能删除,因为一旦删除,依赖这些用户运行的服务或程序就不能正常执行,会导致系统问题。/etc/passwd 文件,是系统用户配置文件,存储了系统中所有用户的基本信息,并且所有用户都可以对此文件执行读操作。

/etc/passwd 文件中的内容非常规律,每行记录对应一个用户,每行用户信息都以 ":" 作为分隔符,划分为 7 个字段,如下:

第一个字段:acceount:用户名,就是一串代表用户身份的字符串。

第二个字段:password:密码,"x" 表示此用户设有密码,但不是真正的密码,真正的密码保存在 /etc/shadow 文件中。

          第二个字段在早期的 UNIX 中,保存是真正的加密密码串,但由于所有程序都能读取此文件,非常容易造成用户数据被窃取。虽然密码是加密的,但是采用暴力破解的方式也是能够进行破解的。因此,现在 Linux 系统把真正的加密密码串放置在 /etc/shadow 文件中,此文件只有 root 用户可以浏览和操作,这样就最大限度地保证了密码的安全。这里虽然 "x" 并不表示真正的密码,只是密码占位符,但也不能删除这个"x"。

第三个字段:UID:也就是用户 ID。每个用户都有唯一的一个 UID,Linux 系统通过 UID 来识别不同的用户。

         UID的范围在0~65535 之间的数,不同范围的数字表示不同的用户身份。

           0代表超级用户root,UID 为 0 就代表这个账号是管理员账号。在 Linux 中,把普通用户升级成管理员的方法只需把普通用户的 UID 修改为 0 就可以。

           1~499    系统用户(伪用户),此范围的 UID 保留给系统使用。其中,1~99 用于系统自行创建的账号;100~499 分配给有系统账号需求的用户。

         除了 0 之外,其他的 UID 并无不同,只是默认 500 以下的数字给系统作为保留账户,只是一个公认的习惯,500~65535 普通用户。

第四个字段:GID:基本组ID,表示用户初始组的组 ID 号。这里补充一下初始组(基本组)和附加组的概念。

        基本组,指用户登陆时就拥有这个用户组的相关权限。每个用户的基本组只能有一个,通常就是和此用户的用户名相同的组名作为该用户的基本组。比如说,新添加用户user01的同时,就会建立 user01 组作为user01用户的基本组。

       附加组,指一个用户可以加入多个其他的用户组,并拥有这些组的权限。除基本组外,用户再加入其他的用户组时就为用户的附加组。附加组可以有多个,并且用户会拥有附加组的权限。虽然基本组和附加组的身份是可以修改的,但是在工作中不建议修改基本组,只修改附加组。需要注意的是,在 /etc/passwd 文件的第四个字段中看到的 ID 是这个用户的基本组GID。

第五个字段comment:注释

第六个字段:DIR:用户家目录,也就是用户登录后有操作权限的访问目录,通常称为用户的主目录。

第七个字段:SHELL:用户的默认shell,通常情况下Linux 系统默认使用的命令解释器是 bash(/bin/bash)

4.3.9 用户密码文件 /etc/shadow

第一个字段:acceount:登录名

第二个字段:encrypted password:加密的密码,这个部分可能为两个!!,两个!!表示这个账户是被锁定的。

                   加密的密码部分由两部分组成:前面部分为solt(盐)即杂质部分,后面部分为杂质和密码进行加密后组成。

第三个字段:(19525),从1970年一月一日开始到上一次更改密码所经过的天数;即为上次修改密码的时间

第四个字段:(0),密码最短使用期限,0表示不做限制

第五个字段:(99999),密码最长使用期限

第六个字段:(7),离密码过期还要多少天提示该更密码,即更改密码告警时间

第七个字段:密码过期之后账号禁之前的期限

第八个字段:从1970年一月一日开始到期后账号就停用,即号的失效日期

第九个字段:保留字段,无意义。

5 组管理命令

/ect/group 文件是用户组配置文件,即用户组的所有信息都存放在此文件中,接下来介绍这个文件的组成。

   

组文件有四个部分组成,以隔开

第一部分:组名,也就是是用户组的名称,由字母或数字构成。组名和用户名一样也不能重复。

第二部分:x,密码占位符,这里的 "x" 仅仅是密码标识,真正加密后的组密码默认保存在 /etc/gshadow 文件中,并且几乎都不设置组密码

第三部:组ID,Linux 系统就是通过 GID 来区分用户组的

第四部分:以这个组为附加组的用户列表,如果有多个用户使用逗号分隔,没有则为空

5.1 addgroup介绍

addgroup是一条Linux命令,用于添加一个新组。在Linux系统中,一个用户可以属于多个组,addgroup就是为用户添加一个新组。

addgroup命令的语法是:

      addgroup [options] [--gid ID] group_name

       addgroup --system [options] [--gid ID] group_name

其中,group_name表示需要添加的新组的名称。

options包括:

$ -g, --gid GID   指定新组的GID
$ -h, --help        显示帮助信息
$ -K, --key KEY=VALUE 设置debconf指定的关键字
$ --system   创建一个系统帐户(GID < 1000)
$ --force-badname 忽略组名不规范的警告
$ --group    组建立时GID必须给出
$ --verbose  让命令输出更多信息

当执行addgroup命令时,会首先检查group_name是否已经存在。如果group_name已经存在,则会返回错误。如果group_name不存在,则新建一个组。

5.2 使用addgroup添加新组

在Linux系统中,我们可以使用addgroup添加一个新组。

格式如下    sudo addgroup group_name

其中,group_name表示我们要添加的组的名称。例如,我们要添加一个新组webtool,则可以执行以下命令:

arshou@arshou-ser:~$ sudo addgroup webtool

Adding group `webtool' (GID 1001) ...

Done.

arshou@arshou-ser:~$

5.3、使用addgroup选项

除了直接使用addgroup添加新组,我们还可以使用addgroup命令的选项来自定义新组的GID、描述等信息。

例如,我们可以使用--gid选项来指定新组的GID:

arshou@arshou-ser:~$ sudo addgroup --gid 1006 webdev

Adding group `webdev' (GID 1006) ...

Done.

arshou@arshou-ser:~$

执行上述命令后,根据系统会返回的信息,表示已经成功添加了一个GID为1006的新组webdev。

5.4 删除组使用delgroup命令

arshou@arshou-ser:~$ sudo delgroup webdev

Removing group `webdev' ...

Done.

arshou@arshou-ser:~$

根据系统会返回的信息,表示已经成功删除webdev组。

6 sudo配置

前面创建了一个名为“test”的新用户,可以用来登录系统了,但权限比较低,因该用户尚未获得 sudo 访问权限。所以他不能执行更多的管理任务。

可以验证用户是否具有 sudo 访问权限,如下所示:

$ sudo -l -U test

输出结果:

User test is not allowed to run sudo on arshou-ser.

根据输出结果显示当前用户没有sudo权限。

6.1 向 Ubuntu Linux 中的用户授予 Sudo 权限

将之前添加的test用户授予 Sudo 权限

arshou@arshou-ser:~$ sudo adduser test sudo

Adding user `test' to group `sudo' ...

Adding user test to group sudo

Done.

arshou@arshou-ser:~$

执行上述命令后,根据系统会返回的信息,表示已经test用户已经添加了sudo权限。

也可以使用以下命令将用户添加到 sudo 组。

$ sudo usermod -aG sudo test

   

要验证用户是否已添加到 sudo 组中,请运行:

arshou@arshou-ser:~$ sudo -l -U test

Matching Defaults entries for test on arshou-ser:

    env_reset, mail_badpass,

    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin,

    use_pty

User test may run the following commands on arshou-ser:

    (ALL : ALL) ALL

arshou@arshou-ser:~$

6.2 删除用户的 Sudo 访问权限

可以删除用户的 sudo 权限,而无需完全删除用户

警告:在 Ubuntu 系统中执行此操作时必须小心。不要从“sudo”组中删除真正的管理员。系统中应该至少有一个 sudo 用户。

首先,确保您已从用户“test”会话已注销并以另一个 sudo 用户身份登录系统。

要撤销用户的 sudo 权限(例如 test),命令将是:

arshou@arshou-ser:~$ sudo deluser test sudo  

Removing user `test' from group `sudo' ...

Done.

arshou@arshou-ser:~$

 

7 临时锁定或解锁用户密码

 临时锁定或解锁用户密码,分别使用以下语法:

sudo passwd -l username               #临时锁定密码
sudo passwd -u username

用户密码锁定后,无法使用锁定的用户登录系统。

注意:

简单地禁用/锁定用户密码不会阻止用户远程登录到您的服务器,如果他们之前设置了SSH公钥身份验证。他们仍然可以通过shell访问服务器,而不需要任何密码。记得检查用户的主目录,查找允许这种类型的经过身份验证的SSH访问的文件,例如/home/username/.ssh/authorized_keys。

解决可以通过删除或重命名用户主文件夹中的. SSH /目录,以防止进一步的SSH身份验证功能。

用户目录权限管理

默认情况下,在Ubuntu中创建的用户主目录具有全局读/执行权限。这意味着所有用户都可以浏览和访问其他用户主目录的内容。这可能不适合您的环境。这意味着所有用户都可以浏览和访问其他用户主目录的内容。

要验证当前用户的主目录权限,请使用以下语法:

$ ls -ld /home/username

以下输出显示目录/home/username具有全局可读权限:

Drwxr-xr-x 2 username username 4096 2007-10-02 20:03 username

以用下面的语法修改全局可读权限:

执行命令:chmod 0750 /home/username

更有效的方法是在创建用户主文件夹时修改adduser全局默认权限。只需编辑/etc/adduser.conf文件并将DIR_MODE变量修改为适当的值,这样所有新的主目录都将获得正确的权限,修改如下

DIR_MODE=0750

这个在Ubuntu 22.04.1 LTS中,DIR_MODE变量已经为0750

9 密码策略

强大的密码策略是安全状态中最重要的方面之一。许多安全漏洞涉及简单的暴力破解和针对弱密码的字典攻击。如果您打算提供涉及本地密码系统的任何形式的远程访问,请确保充分满足最低密码复杂性要求、最长密码生存期要求和对身份验证系统的频繁审计。

说明:密码复杂度设置对root用户无效,只对普通用户有效。当你使用root用户给普通用户设置密码时,即使不符合密码规则,也可以设置成功。

9.1 安装cracklib模块

安装PAM的cracklib模块,cracklib能提供额外的密码检查能力,先装个软件。

$ sudo apt install libpam-cracklib

9.2 限制使用过去使用过的密码

默认情况下,系统没有开启密码重复次数检测策略,找到如下配置,并在后面添加remember=5,表示禁止使用最近用过的5个密码,己使用过的密码会被保存在/etc/security/opasswd

password        [success=1 default=ignore]      pam_unix.so obscure sha512 remember=5

9.3 最小密码长度和复杂度

默认情况下,Ubuntu要求最小密码长度为6个字符,以及一些基本的熵检查。这些值在/etc/pam.d/common-password文件中控制,将最小长度调整为8个字符和调整密码复杂度,修改如下。

 

password        requisite                       pam_cracklib.so retry=3 minlen=8 difok=3 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1

password        [success=1 default=ignore]      pam_unix.so obscure sha512 remember=5

参数详解:

retry=3

 表示可以输错3次   (默认参数 3),然后退出并返回错误

minlen = 8

 设置最小长度,等于8表示 8 个字符)

difok = 3 

设置新密码中不能出现在旧密码中的字符数,默认参数3

ucredit = -1 

要求新密码中至少有一个大写字符

lcredit =-1   

要求新密码中至少有一个小写字符

dcredit = -1 

新密码至少需要一位数字

ocredit = -1 

新密码中至少需要一个其他字符

minclass = 2 

设置新密码所需的最少字符类别数 (大写/小写/数字/其他)

maxrepeat = 2  

设置新密码中允许的最大连续相同字符数

maxclassrepeat = 4  

设置新密码中允许的同类最大连续字符数

maxsequence = 2  

设置新密码中单调字符序列的最大长度

gecoscheck = 1 

 检查新密码中是否包含用户密码条目的GECOS字段中超过3个字符的单词

9.4 密码过期

在创建用户帐户时,应该制定一项策略,设置密码的最小和最大使用期限,迫使用户在密码过期时更改密码。

要轻松查看用户帐户的当前状态,请使用以下语法:

sudo chage -l username

chage命令选项:

      -m  #密码可更改的最小天数,为0时代表任何时候都可以更改密码

      -M  #密码保持有效的最大天数

      -W  #用户密码到期前,提前收到警告信息的天数

      -E  #帐号到期的日期。过了这天,此帐号将不可用。

      -d  #上一次更改的日期

      -I  #停滞时期。如果一个密码已过期这些天,那么此帐号将不可用。

      -l  #例出当前的设置。由非特权用户来确定他们的密码或帐号何时过期。

下面的输出显示了关于用户帐户的信息,即没有应用任何策略:

  • 手动修改密码期限设(-E)为06/30/2023,密码最小有效期(-m)为5天,密码最大有效期(-m)为90天,密码到期后帐号会被锁住(-I)为30天,密码过期前警告时间(-W)为14天。示例如下:

  • sudo chage -E 6/30/2023 -m 5 -M 90 -I 30 -W 14 mtest

验证更改,下面的输出显示了为该帐户建立的新策略:

9.5 通过修改配置文件设置密码过期期限

用户密码过期期限策略在/etc/login.defs文件中进行调整,需要调整的参数如下:

PASS_MAX_DAYS 99999

密码的最大有效期, 99999:永久有期

PASS_MIN_DAYS 0

是否可修改密码,0可修改,非0多少天后可修改

PASS_MIN_LEN 5

密码最小长度,使用pam_cracklib module,该参数不再有效

PASS_WARN_AGE 7

密码失效前多少天在用户登录时通知用户修改密码

修改方法如下:

$ sudo sed  -i 's#^PASS_MAX_DAYS.*#PASS_MAX_DAYS\t90#'  /etc/login.defs

$ sudo sed  -i 's#^PASS_MIN_DAYS.*#PASS_MIN_DAYS\t10#'  /etc/login.defs

$ sudo sed  -i 's#^PASS_MIN_LEN.*#PASS_MIN_LEN\t8#'  /etc/login.defs

$ sudo sed  -i 's#^PASS_WARN_AGE.*#PASS_WARN_AGE\t5#'  /etc/login.defs

Guess you like

Origin blog.csdn.net/yjun89/article/details/131360979