阿里云Linux服务器设置
Debian系
软件安装
sudo apt install apt-transport-https ca-certificates -y
阿里云源
https://developer.aliyun.com/mirror/debian
apt-get install sudo net-tools vim tree wget curl tcpdump
用户设置
useradd -m -s /bin/bash admin&&passwd -d admin
添加一个admin用户,属于admin组,并且具有无密码的sudo权限。
useradd -m -G admin -s /bin/bash
- -m:创建用户的同时,为用户创建一个家目录(home directory)。
- -g:指定用户的初始登录组(primary group)。
- -G:指定用户的附加组(additional groups)。
- -s:指定用户的登录 Shell。
- -c:提供有关用户的注释信息。
- -d:指定用户的家目录路径。
- -e:设置用户帐户的过期日期。
- -p:设置用户的密码(通常使用 passwd 命令来设置密码更安全)。
删除用户密码,让其可以免密本地终端登入。(阿里云没有这样设置)
passwd -d admin
sudo权限设置
echo "admin ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
admin ALL=(ALL) NOPASSWD:ALL
这个规则允许用户 admin 以超级用户权限运行任何命令,而且无需输入密码。具体来说:
- admin:这是用户的用户名。
- ALL:这表示用户 admin 可以在任何主机上运行命令。
- NOPASSWD:ALL:这部分指定了用户不需要输入密码。
因此,用户 admin 可以以超级用户权限运行任何命令,而不需要密码验证。
这种配置在某些情况下可能会方便,但请务必小心使用。为用户分配无密码 sudo 权限是一个潜在的安全风险,应谨慎使用,确保只有可信任的用户才能获得此特权,同时只授予他们执行必要操作所需的权限。这有助于提高系统的安全性。
SSH登入设置
cat <<EOF >> /etc/ssh/sshd_config
UseDNS no
SyslogFacility AUTHPRIV
PermitRootLogin yes
EOF
cat <<EOF >> /etc/ssh/sshd_config
UseDNS no
AddressFamily inet
SyslogFacility AUTHPRIV
PermitRootLogin yes
PasswordAuthentication no
EOF
UseDNS no
AddressFamily inet
SyslogFacility AUTHPRIV
PermitRootLogin yes
PasswordAuthentication no
UseDNS no:此选项禁用了DNS反向解析。通常情况下,SSH服务器会尝试通过反向DNS查找来确定客户端的主机名。禁用此选项可以提高连接性能,因为不再需要等待DNS查询完成。但需要注意的是,禁用DNS反向解析可能会降低一些安全性,因为无法验证客户端的主机名。AddressFamily inet:该设置指定SSH服务器仅支持IPv4地址。这意味着服务器将仅接受IPv4地址的连接请求,而不会接受IPv6地址的请求。如果你的网络环境主要使用IPv4,则可以使用此设置。SyslogFacility AUTHPRIV:指定了用于记录SSH日志消息的系统设施。AUTHPRIV设施通常用于记录与身份验证和安全相关的消息。PermitRootLogin yes:允许Root用户通过SSH登录到服务器。这个选项的设置为 "yes",意味着Root用户可以远程登录到服务器。通常情况下,出于安全考虑,不建议启用Root用户的远程登录。更好的做法是创建一个普通用户,然后使用该用户进行远程登录,然后再通过sudo等方式升级权限。PasswordAuthentication no:禁用了密码身份验证,只允许公钥身份验证。这是一个很好的安全实践,因为密码身份验证容易受到暴力攻击。这个设置确保只有拥有有效SSH密钥的用户可以登录到服务器。
GRUB设置
推荐设置
GRUB_DEFAULT=0
GRUB_TIMEOUT=1
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT=""
GRUB_CMDLINE_LINUX="vga=792 console=tty0 console=ttyS0,115200n8 net.ifnames=0 biosdevname=0"
阿里云默认设置
vim /etc/default/grub
# 注意修改后使用 update-grub 更新
GRUB_DEFAULT=0
GRUB_TIMEOUT=1
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet"
GRUB_CMDLINE_LINUX="vga=792 console=tty0 console=ttyS0,115200n8 net.ifnames=0"
这是一个 GRUB 引导管理器的配置中的 GRUB_CMDLINE_LINUX 选项,它包含了一系列传递给 Linux 内核的启动参数。以下是这些参数的解释:
vga=792:这个参数设置图形模式,具体的分辨率和颜色深度根据不同的数字设置而有所不同。vga=792可能对应于特定的VESA图形模式。console=tty0:这个参数指定了Linux内核的控制台终端,通常是显示器。tty0表示显示器控制台。console=ttyS0,115200n8:这个参数指定了串口终端,通常是用于通过串口连接进行远程管理的终端。ttyS0表示串口设备,115200n8表示波特率为115200,数据位为8,不启用奇偶校验。net.ifnames=0:这个参数用于禁用网络接口的预设命名规则,通常用于恢复传统的网络接口命名方式。noibrs:这个参数禁用了Intel的Indirect Branch Restricted Speculation(IBRS)特性,这是与处理器漏洞(如Spectre)有关的选项之一。iommu=pt:这个参数启用了IOMMU并设置为“passthrough”模式,用于虚拟化环境。nvme_core.multipath=n:这个参数设置了NVMe多路径支持的策略,n可能是某种策略标志。crashkernel=0M-1G:0M,1G-4G:160M,4G-8G:192M,8G-128G:384M,128G-:512M:这个参数用于设置用于系统崩溃转储的内存区域大小和位置。这个示例配置了不同内存范围的崩溃转储内存大小。crash_kexec_post_notifiers:这个参数在崩溃时执行kexec post通知,用于特定的系统崩溃处理。nvme_core.io_timeout=4294967295和nvme_core.admin_timeout=4294967295:这些参数设置了NVMe核心的IO和管理员操作的超时值。
这些参数通常用于配置 Linux 内核的启动参数,以适应系统的需求和配置。不同的系统和用例可能需要不同的参数设置。
个人优化
信息显示
在Debian系的系统中,/etc/issue中的内容会在用户登入前显示,/etc/motd中的内容会在用户成功登入后显示。
脚本
cd ~&&touch hello.sh&&chmod +x hello.sh
#!/bin/bash
truncate -s 0 /etc/issue
truncate -s 0 /etc/motd
# 获取物理网卡 IPV4地址、子网掩码、网卡名称
ip=`ip addr | grep "scope global" | grep -v inet6 | grep -e eth -e ens |awk '{print("\033[34m|\033[0m\033[32m",$NF,"\t->",$2,"\033[0m")}'`
# 获取发行版名称和版本
sys_name=`cat /etc/os-release | grep PRETTY_NAME | awk -F '=' '{print($2)}' | sed 's/"//g'`
# 间隔符
t1="\033[34m+----------------------------\033[0m"
t2="\033[34m|\033[0m\033[32m"
t3="\033[0m"
# 自定义信息
info="
$t1
$t2 $sys_name $t3
$t1
$t2 User -> admin/NULL $t3
$t2 Root -> root/NULL $t3
$t1
$ip
$t1
"
# /etc/issue 默认存在的信息
issue_def="Debian GNU/Linux 11 \n \l"
# /etc/motd 默认存在的信息
motd_def="
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law."
echo "$issue_def" > /etc/issue
echo -e "$info" >> /etc/issue
echo "$motd_def" > /etc/motd
echo -e "$info" >> /etc/motd
添加自定义服务
通过添加自定义服务,并且设置为开机启动,来运行脚本。
vim /etc/systemd/system/hello.service
[Unit]
Description=Welcome information
After=networking.service
//centos After=NetworkManager-wait-online.service
//https://www.cnblogs.com/wutou/p/17024073.html
[Service]
Type=oneshot
User=root
ExecStart=/root/hello.sh
[Install]
WantedBy=multi-user.target
systemctl enable hello.service
Centos系
用户设置
useradd -m -s /bin/bash admin&&passwd -d admin
添加一个admin用户,属于admin组,并且具有无密码的sudo权限。
sudo权限设置
echo "admin ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
SSH登入设置
cat <<EOF >> /etc/ssh/sshd_config
UseDNS no
SyslogFacility AUTHPRIV
PermitRootLogin yes
EOF
cat <<EOF >> /etc/ssh/sshd_config
UseDNS no
AddressFamily inet
SyslogFacility AUTHPRIV
PermitRootLogin yes
PasswordAuthentication no
EOF
GRUB
GRUB_CMDLINE_LINUX="crashkernel=auto net.ifnames=0 console=tty0 console=ttyS0,115200n8"
grub2-mkconfig -o /boot/grub2/grub.cfg
服务器信息显示(个人优化)
在Centos的系统中,/etc/issue中的内容会在用户登入前显示,/etc/motd中的内容会在用户成功登入后显示。
脚本
cd ~&&touch hello.sh&&chmod +x hello.sh
#!/bin/bash
truncate -s 0 /etc/issue
truncate -s 0 /etc/motd
# 获取物理网卡 IPV4地址、子网掩码、网卡名称
ip=`ip addr | grep "scope global" | grep -v inet6 | grep -e eth -e ens |awk '{print("\033[34m|\033[0m\033[32m",$NF,"\t->",$2,"\033[0m")}'`
# 获取发行版名称和版本
sys_name=`cat /etc/os-release | grep PRETTY_NAME | awk -F '=' '{print($2)}' | sed 's/"//g'`
# 间隔符
t1="\033[34m+----------------------------\033[0m"
t2="\033[34m|\033[0m\033[32m"
t3="\033[0m"
# 自定义信息
info="
$t1
$t2 $sys_name $t3
$t1
$t2 User -> admin/NULL $t3
$t2 Root -> root/NULL $t3
$t1
$ip
$t1
"
# /etc/issue 默认存在的信息
issue_def="\S
Kernel \r on an \m"
# /etc/motd 默认存在的信息
#motd_def=""
echo "$issue_def" > /etc/issue
echo -e "$info" >> /etc/issue
#echo "$motd_def" > /etc/motd
echo -e "$info" >> /etc/motd
添加自定义服务
通过添加自定义服务,并且设置为开机启动,来运行脚本。
vim /etc/systemd/system/hello.service
[Unit]
Description=Welcome information
After=networking.service
//centos After=NetworkManager-wait-online.service
//https://www.cnblogs.com/wutou/p/17024073.html
[Service]
Type=oneshot
User=root
ExecStart=/root/hello.sh
[Install]
WantedBy=multi-user.target
systemctl enable hello.service

浙公网安备 33010602011771号