> For the complete documentation index, see [llms.txt](https://yangsx95.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yangsx95.gitbook.io/notes/devops/vm/xin-jian-yi-ge-xin-de-linux-xu-ni-ji-xu-yao-pei-zhi-de-dong-xi.md).

# 新建一个新的Linux虚拟机需要配置的东西

## 配置mac地址

### Ubuntu 18.04 临时修改mac

```shell
# 关闭网卡
/sbin/ifconfig eth0 down
# 更改mac地址
/sbin/ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
# 启动网卡
/sbin/ifconfig eht0 up
```

或者使用ip命令：

```shell
ip link set eth0 down
ip link set eth0 address xx:xx:xx:xx:xx:xx
ip ling set eth0 up
```

### Ubuntu 18.04 永久修改mac

将修改mac地址的命令放入到 `/etc/rcS.d/rc.local` 文件中， `rc.local` 会在系统启动时执行这些命令。

或者直接修改 `/etc/network/interfaces` 文件，添加:

```shell
pre-up ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
```

### Ubuntu 20.04 修改mac

1. 安装macchanger工具

   ```shell
   sudo apt install macchanger
   ```

   这一步如果有mac地址冲突会提示自动修改，那么下面的步骤就不用走了
2. 查看所有的网络接口，找到你的网络适配器名称

   ```shell
   root@k8s-node1:~# ip addr
   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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
       link/ether 00:50:56:25:18:f2 brd ff:ff:ff:ff:ff:ff
       inet 192.168.121.11/24 brd 192.168.121.255 scope global ens160
          valid_lft forever preferred_lft forever
       inet6 fe80::250:56ff:fe25:18f2/64 scope link
          valid_lft forever preferred_lft forever
   ```
3. 查看当前网络适配器的mac地址

   ```shell
   macchanger –s ens160
   ```
4. 给网络设备随机一个mac地址

   ```shell
   sudo macchanger -r ens160
   ```
5. 不随机，指定一个mac地址：

   ```shell
   sudo macchanger –m c2:43:bc:1c:62:01 ens160
   ```

## 配置静态IP

### Centos

```shell
# vi /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE=Ethernet                # 网卡类型：为以太网
PROXY_METHOD=none            # 代理方式：关闭状态
BROWSER_ONLY=no                # 只是浏览器：否
BOOTPROTO=dhcp                # 网卡的引导协议：DHCP[中文名称: 动态主机配置协议]
DEFROUTE=yes                # 默认路由：是, 不明白的可以百度关键词 `默认路由`
IPV4_FAILURE_FATAL=no        # 是不开启IPV4致命错误检测：否
IPV6INIT=yes                # IPV6是否自动初始化: 是[不会有任何影响, 现在还没用到IPV6]
IPV6_AUTOCONF=yes            # IPV6是否自动配置：是[不会有任何影响, 现在还没用到IPV6]
IPV6_DEFROUTE=yes            # IPV6是否可以为默认路由：是[不会有任何影响, 现在还没用到IPV6]
IPV6_FAILURE_FATAL=no        # 是不开启IPV6致命错误检测：否
IPV6_ADDR_GEN_MODE=stable-privacy            # IPV6地址生成模型：stable-privacy [这只一种生成IPV6的策略]
NAME=ens33                    # 网卡物理设备名称
UUID=f47bde51-fa78-4f79-b68f-d5dd90cfc698    # 通用唯一识别码, 每一个网卡都会有, 不能重复, 否两台linux只有一台网卡可用
DEVICE=ens33                    # 网卡设备名称, 必须和 `NAME` 值一样
ONBOOT=no                        # 是否开机启动， 要想网卡开机就启动或通过 `systemctl restart network`控制网卡,必须设置为 `yes`
```

静态ip需要修改一下选项：

```shell
vi /etc/sysconfig/network-scripts/ifcfg-ens33

BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.111
NETMASK=255.255.255.0
GATEWAY=192.168.1.1

## 配置DNS服务器
DNS1=8.8.8.8
DNS2=114.114.114.114
```

`systemctl restart network` 重启网络即可

### Ubuntu

之前版本的Ubuntu信息配置在 `/etc/network/interfaces` 文件中，新版本将采用netplan命令，网卡信息则会被配置在 `/etc/netplan/01-network-manager-all.yaml` 中：

```yaml
# Let NetworkManager manage all devices on this system
network:
  version: 2
  # renderer: NetworkManager
  ethernets:
      ens33:
          addresses: [192.168.52.111/24]
          gateway4: 192.168.52.2
          nameservers:
                addresses:
                - 8.8.8.8
```

执行 `netplan apply` 应用更改。

## 关闭selinux

### 查看状态

```shell
sestatus -v
```

### 临时关闭

```shell
## 0: 设置SELinux 成为permissive模式
## 1：设置SELinux 成为enforcing模式
setenforce 0
```

### 永久关闭

`vim /etc/selinux/config`

```shell
#将
SELINUX=enforcing
#改为
SELINUX=disabled
```

注意，需要重新启动机器才会生效。

## 防火墙

### 使用iptables关闭防火墙(centos6)

1. 重启后生效

   开启：chkconfig iptables on

   关闭：chkconfig iptables off
2. 即时生效，重启后失效

   开启：service iptables start

   关闭：service iptables stop

需要说明的是对于Linux下的其它服务都可以用以上命令执行开启和关闭操作。

在开启了防火墙时，做如下设置，开启相关端口，修改/etc/sysconfig/iptables 文件，添加以下内容：

```shell
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
```

### 使用firewalld关闭防火墙(centos7)

```shell
# 查看防火墙状态
systemctl status firewalld.service
firewall-cmd --state

# 临时关闭防火墙
systemctl stop firewalld.service

# 永久关闭防火墙, 禁用开机重启
systemctl disable firewalld.service

# 开启防火墙
systemctl start firewalld.service

firewall-cmd --zone=public --add-port=80/tcp --permanent
# --permanent永久生效，没有此参数重启后失效

# 范围开启
firewall-cmd --zone=public --add-port=1000-2000/tcp --permanent

# 重新载入配置
firewall-cmd --reload

# 查看指定端口
firewall-cmd --zone=public --query-port=80/tcp

# 删除指定端口的开放
firewall-cmd --zone=public --remove-port=80/tcp --permanent

```

### 将firewall替换为传统的iptables

```shell
systemctl stop firewalld
systemctl mask firewalld

# 安装iptables
yum install iptables-services

# 设置开机自动启动
systemctl enable iptables

# 其他常用命令
systemctl stop iptables
systemctl start iptables
systemctl restart iptables
systemctl reload iptables

# 保存iptables的设置
service iptables save

```

### 使用ufw关闭防火墙(ubuntu20.04)

```shell
# 关闭防火墙（都是永久操作）
sudo ufw disable
# 开启防火墙
sudo ufw enable
# 检查防火墙状态
sudo ufw status
```

## 配置hostname

位于 `/etc/homstname` 配置文件下, 默认皆为 localhost.localdomai.

查看当前主机名:

```shell
hostname
```

centos7修改主机名:

```shell
hostnamectl set-hostname server1.yangsx95.com
```

或者直接修改 `/etc/hostname` 文件.

编辑 `/etc/hosts` 文件,为 `127.0..0.1` 添加hostname:

```shell
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 server1.yangsx95.com
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
```

## 配置root用户

如果是Ubuntu，默认的管理员账户不是root，那么需要切换到root用户再更改密码：

```shell
# 切换到root用户
sudo su

# 更改root用户密码
sudo password root
```

## 配置ssh

### 配置ssh root用户登录策略

**问题描述：**

当使用 SSH 登录云服务器 ECS （Elastic Compute Server） Linux 服务器时，如果是 root 用户，即便正确输入了密码，也会出现类似如下错误信息。

```shell
Permission denied, please try again.
SSH 服务器拒绝了密码，请再试一次。
```

但非root用户可以正常登录，而且root用户通过 管理终端 登录也正常。

修改配置文件 `/etc/ssh/sshd_config`：

![image-20220111181917441](/files/VnpY8Tzwd6bCuW2EBXni)

* 未配置该参数，或者将参数值配置为 yes （默认情况），都允许 root 用户登录。只有显示的设置为 no 时，才会阻断root 用户登录。
* 该参数只会影响用户的 SSH 登录，不影响用户通过 **管理终端** 等其它方式登录系统。

配置完毕后，重启ssh服务：

```shell
systemctl restart sshd
```

## 配置时钟同步

安装ntpd:

```shell
yum install -y ntp
```

配置时钟服务器地址:

```shell
# 阿里云服务地址, 包含 ntp1 到 netp7
ntpdate ntp1.aliyun.com

ntpdate cn.pool.ntp.org
```

执行时钟同步命令:

```shell
clock -w
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yangsx95.gitbook.io/notes/devops/vm/xin-jian-yi-ge-xin-de-linux-xu-ni-ji-xu-yao-pei-zhi-de-dong-xi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
