Ubuntu Server20的常用设置


♦ Ubuntu server的安装

  • 镜像文件:ubuntu-20.04.1-live-server-amd64.iso

需要注意的要点:

  • Configure proxy配置页面的Proxy address无需配置。
  • Configure Ubuntu Archive mirror需要填写:
http://mirrors.aliyun.com/ubuntu
  • 最好在安装界面中就完成磁盘分区和网卡静态地址配置。
  • 建议在Installer update available界面中进行更新。

重启后可查看版本信息并修改root密码

cat /etc/os-release
lsb_release -a
# 修改root密码
sudo passwd root

♦ 取消ssh的登陆欢迎界面

touch $HOME/.hushlogin
或者
touch ~/.hushlogin

♦ 修改hostname

# 查看主机名
hostnamectl
# 无需重启,注销并登陆即可。
hostnamectl set-hostname host.example.com
# 修改本地域名解析
vim /etc/hosts

如果在云实例上运行Ubuntu,并且cloud-init已安装软件包,则还需要编辑该/etc/cloud/cloud.cfg文件。通常,该软件包通常默认安装在云提供商提供的映像中,并且用于处理云实例的初始化。

vim /etc/cloud/cloud.cfg

# 搜索preserve_hostname,并将值从false更改为true:
# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true

♦ 配置静态IP

cd /etc/netplan/
vim /etc/netplan/00-installer-config.yaml 
# This is the network config written by 'subiquity'
network:
  ethernets:
    ens33:
      addresses:
      - 192.168.74.150/24
      gateway4: 192.168.74.2
      nameservers:
        addresses:
        - 114.114.114.114
  version: 2
====================================或者=====================================
network:
  version: 2
  ethernets:
    ens33:
      addresses:
        - 192.168.74.150/24
      gateway4: 192.168.74.2
      nameservers:
        addresses: [221.3.131.11, 221.3.131.12]

常用的DNS:

114.114.114.114 电信
114.114.115.115 电信
119.29.29.29 POD
182.254.116.116 POD
182.254.118.118 POD
180.76.76.76 百度
223.5.5.5 阿里
223.6.6.6 阿里
1.2.4.8 CNNIC
210.2.4.8 CNNIC
8.8.8.8 Google
8.8.4.4 Google
  • netplan apply 命令可以让配置直接生效。详细可参考man netplan 或者这里
  • 使用 netplan generate 可以校验配置。netplan --debug apply命令可以用于启用配置时的调错。
  • 使用systemd-resolve --status | grep ‘DNS Server’ 可以查看当前的DNS。或者使用等价的命令resolvectl status,或干脆resolvectl。
  • 当然networkctl status也可以综合地看IP地址和DNS。
  • 静态路由等其他配置方法可以参考这里
  • ip路由命令可参考:man ip-route
  • 其他可参考这里

♦ 修改更新源

ll /etc/apt/sources.list
-rw-r--r-- 1 root root 2717 Jan 11 15:23 /etc/apt/sources.list

vim /etc/apt/sources.list
==============================================================
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://mirrors.aliyun.com/ubuntu focal main restricted
# deb-src http://mirrors.aliyun.com/ubuntu focal main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://mirrors.aliyun.com/ubuntu focal-updates main restricted
# deb-src http://mirrors.aliyun.com/ubuntu focal-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://mirrors.aliyun.com/ubuntu focal universe
# deb-src http://mirrors.aliyun.com/ubuntu focal universe
deb http://mirrors.aliyun.com/ubuntu focal-updates universe
# deb-src http://mirrors.aliyun.com/ubuntu focal-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://mirrors.aliyun.com/ubuntu focal multiverse
# deb-src http://mirrors.aliyun.com/ubuntu focal multiverse
deb http://mirrors.aliyun.com/ubuntu focal-updates multiverse
# deb-src http://mirrors.aliyun.com/ubuntu focal-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://mirrors.aliyun.com/ubuntu focal-backports main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu focal-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu focal partner
# deb-src http://archive.canonical.com/ubuntu focal partner

deb http://mirrors.aliyun.com/ubuntu focal-security main restricted
# deb-src http://mirrors.aliyun.com/ubuntu focal-security main restricted
deb http://mirrors.aliyun.com/ubuntu focal-security universe
# deb-src http://mirrors.aliyun.com/ubuntu focal-security universe
deb http://mirrors.aliyun.com/ubuntu focal-security multiverse
# deb-src http://mirrors.aliyun.com/ubuntu focal-security multiverse

sudo apt-get update
sudo apt-get upgrade

♦ 修改时区并同步时间

  • 修改时区
# 输入数字按回车
tzselect
# 查看时区
timedatectl
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

或者

# 打印当前系统时区
timedatectl
# 系统时区通过链接文件/etc/localtime配置,该链接指向/usr/share/zoneinfo目录下的一个二进制时区标识文件。
ls -l /etc/localtime
# 显示当前系统时区
cat /etc/timezone
# 列出所有可用的时区
timedatectl list-timezones

# 修改时区(一条命令搞定,立即生效!)
timedatectl set-timezone Asia/Shanghai

# 查看修改后的时间和时区
$ date -R
Thu, 11 Apr 2024 15:43:03 +0800
                            ^---东8区
  • 同步时间
apt install -y ntpdate
ntpdate -u ntp.ntsc.ac.cn
timedatectl

# 开启cron同步
crontab -e
添加如下内容
*/60 * * * * /usr/sbin/ntpdate ntp.ntsc.ac.cn >/dev/null 2>&1
# 显示添加的内容
crontab -l

Quartz在线Cron表达式生成

♦ 修改时间戳的显示格式

  • 修改日志文件的时间戳显示格式
vim /etc/rsyslog.conf
# Use traditional timestamp format.
# To enable high precision timestamps, comment out the following line.
# 下一行是原来的配置,将它注释
# $ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
# 添加下面两行
$template CustomFormat,"%$NOW% %TIMESTAMP:8:15% %HOSTNAME% %syslogtag% %msg%\n"
$ActionFileDefaultTemplate CustomFormat

# 重启 rsyslog 服务
systemctl restart rsyslog.service
  • 修改ls命令的输出日期格式
vim /etc/profile
export TIME_STYLE='+%Y/%m/%d %H:%M:%S'

source /etc/profile
ls -lrt

♦ 修改时间为24小时制

# 首先修改为东八区的时区
tzselect
4→9→1→1
rm /etc/localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 然后配置文件
vim /etc/default/locale
LANG=en_US.UTF-8
LC_TIME=en_DK.UTF-8 # 关键是添加这一行

♦ 修改系统日志

可以参考这里
日志文件对比:

操作系统日志文件操作系统日志文件用途
CentOS/var/log/messagesUbuntu/var/log/syslog记录系统或服务程序的日志
CentOS/var/log/secureUbuntu/var/log/auth.log登陆,安全审计日志

日志配置文件

# /etc/rsyslog.d下
# 50-default.conf 配置系统默认日志
# 修改系统默认日志
vim /etc/rsyslog.d/50-default.conf

因为日志在logrotate的作用下会被压缩成zip文件(扩展名.gz)。可以使用下述命令简单查看其内容:

zcat syslog.2.gz | less
或者
zless /var/log/syslog.2.gz

♦ 添加新用户并授权sudo

# 禁止用户的登陆功能 -s /sbin/nologin
useradd -r -m -s /bin/bash newuser
getent passwd
# 只有sudo组的用户才能使用sudo命令
usermod -aG sudo newuser

♦ 限制用户对su命令的使用

只允许admin组的用户newuser能使用su命令来切换登陆用户。

groupadd admin
usermod -aG admin newuser
id newuser
dpkg-statoverride --update --add root admin 4750 /bin/su

♦ 删除用户取消附加组

# 删除用户newuser(包括他的家目录及spool目录)
userdel -r newuser
# 取消newuser用户的附加组admin(把用户newuser从附加组admin中删除)
gpasswd -d newuser admin
# 查看附加组admin中有哪些用户
getent group | grep admin
或者
cat /etc/group | grep admin
# 查看用户的所有组信息
id newuser

♦ 配置ssh公钥登陆

ssh-keygen
ssh-copy-id username@remote_host
或者
# This recursively removes all “group” and “other” permissions for the ~/.ssh/ directory.
cat ~/.ssh/id_rsa.pub | ssh username@remote_host "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"
或者
rsync --archive --chown=username:username ~/.ssh /home/sammy

♦ 配置sshd

安装

apt-get install openssh-server openssh-client

man sshd_config
vim /etc/ssh/sshd_config

# 基本配置
Port 2202
PubkeyAuthentication yes
PasswordAuthentication no
PermitEmptyPasswords no
Banner none
PermitRootLogin no
ChrootDirectory %h
UseDNS no

# 用户限制(HOST:address/masklen),先DenyUsers(黑名单)再AllowUsers(白名单)。AllowUsers overrides AllowGroups
DenyUsers USER USER@HOST
AllowUsers USER USER@HOST 

重启sshd服务 systemctl restart ssh

♦ 安装Docker

sudo apt-get update
sudo apt-get upgrade

apt-get install docker.io
docker version
sudo gpasswd –a ${USER} docker

#修改Docker的镜像源仓库
sudo vim /etc/docker/daemon.json
{
  "registry-mirrors": ["https://y0qd3iq.mirror.aliyuncs.com"]
}

sudo systemctl restart docker
sudo docker info | grep Mirrors -A 1
sudo systemctl enable docker

可以参考这里

♦ 安装JDK

官网 https://github.com/alibaba/dragonwell8
下载 https://github.com/alibaba/dragonwell8/releases

tar xzf Alibaba_Dragonwell_8.5.5-GA_Linux_x64.tar.gz
mv jdk8u275-b1/ /usr/local/jdk

vim /etc/profile
添加如下内容
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH

source /etc/profile
java -version

♦ 关闭不需要的服务

# 显示所有的服务
systemctl list-units --all
# 显示启动的服务
systemctl list-unit --type=service
# 关闭服务
systemctl stop xxx.service
systemctl disable xxx.service

♦ 可以删除的账号和组

可删除的账号:userdel -r ...
adm,lp,sync,shutdown,halt,news,uucp,operator,games,gopher

可删除的组:groupdel -f ...
adm,lp,news,uucp,games,dip,pppusers,popusers,slipusers

♦ 调整history

全局生效
vim /etc/profile.d/history_conf.sh

# 指定命令写入文件(默认~/.bash_history)
#export HISTFILE="$HOME/.bash_history" 
# history输出记录数
export HISTSIZE=1000
# HISTFILE文件记录数
export HISTFILESIZE=1000
# 忽略指定cmd1,cmd2...的命令不被记录到文件;(加参数时会记录)
#export HISTIGNORE="cmd1:cmd2:..."  
# ignoredups 不记录“重复”的命令;连续且相同 方为“重复” ;
# ignorespace 不记录所有以空格开头的命令;
# ignoreboth 表示ignoredups:ignorespace ,效果相当于以上两种的组合;
# erasedups 删除重复命令;
# 设置每条命令执行完立即写入HISTFILE(默认等待退出会话写入)
#export HISTCONTOL=ignoreboth   

#export PROMPT_COMMAND="history -a"  
# 设置命令执行时间格式,记录文件增加时间戳(`whoami`用户信息)
export HISTTIMEFORMAT="[`whoami` %F %T] "  
# 防止会话退出时覆盖其他会话写到HISTFILE的内容;
shopt -s histappend  

♦ 加入日志审计功能

注意:需先调整history的输出格式 export HISTTIMEFORMAT="[whoami %F %T] "。具体见“调整history”部分

将每个用户的shell命令执行历史以文件形式保存在/usr/share/.history/(可以定义得再隐蔽些)目录中。每个用户一个文件夹,并且文件夹下的每个文件以IP地址加shell命令操作时间的格式命名。

root登陆:vim + /etc/profile

#history
USER_IP=`who -u am i 2>/dev/null | awk '{print $NF}' | sed -e 's/[()]//g'`
HISTDIR=/usr/share/.history
if [ -z $USER_IP ]; then
        USER_IP=`hostname`
fi
if [ ! -d $HISTDIR ]; then
        mkdir -p $HISTDIR
        chmod 777 $HISTDIR
fi
if [ ! -d $HISTDIR/${LOGNAME} ]; then
        mkdir -p $HISTDIR/${LOGNAME}
        chmod 300 $HISTDIR/${LOGNAME}
fi
export HISTSIZE=4000
DT=`date +%Y%m%d_%H%M%S`
export HISTFILE="$HISTDIR/${LOGNAME}/${USER_IP}.history.$DT"
chmod 600 $HISTDIR/${LOGNAME}/*.history* 2>/dev/null

配置完成后:source /etc/profile

日志形式:

#1613563044
ll
#1613563047
cd root
#1613563047
ll

可以如下处理日志:

cat 192.168.74.1.history.20210217_190612 | sed "s/#\([0-9]\+\)/date -d @\1 '+[%Y-%m-%d %H:%M:%S]:'/e"

处理完成后:

[2021-02-17 19:57:24]:
ll
[2021-02-17 19:57:27]:
cd root
[2021-02-17 19:57:27]:
ll

♦ 安装monit监控服务

# 基于systemd管理
apt install monit
# 查看版本
monit -V

检查配置

monit -t -c /etc/monit/monitrc
或者
monit -t
# 查看服务状态(.service可省)
systemctl status monit.service
# 查看错误日志
journalctl -u monit.service

主配置文件:/etc/monit/monitrc
单项服务配置目录:/etc/monit/conf.d/ 可以在该目录下新增监控配置文件(文件名随意,命令要用全路径)
帮助:man monit
配置案例:

  • https://mmonit.com/wiki/Monit/ConfigurationExamples
  • https://blog.csdn.net/nowzhangjunzhe/category_8326282.html

♦ 安装MTR工具

apt install mtr
# 不连续运行只要结果报告(-n 不带域名/主机名;-c 指定发包的数量)
mtr -r www.xxx.com

windows下的配套:

  • https://cdn.ipip.net/17mon/besttrace.exe
  • https://github.com/oott123/WinMTR/releases

♦ Personal Package Archives(PPA)使用

可以参考这里

PPA 是放在 Ubuntu 上的一个 Apt repository ,PPAs 允许第三方的开发者在非官方渠道分发软件包。
那么如何找到特定软件的 PPA 呢?打开页面 https://launchpad.net/ubuntu/+ppas或者直接https://launchpad.net/
使用例子

add-apt-repository ppa:chris-lea/redis-server
apt-get update

add-apt-repository 是非常重要的一个命令行工具,如果没有安装,运行下列命令:

apt install software-properties-common

添加ppa仓库

sudo add-apt-repository ppa:webapps/preview

删除指定仓库

sudo add-apt-repository --remove ppa:someppa/ppa

备注:如果使用该仓库已经安装了软件,那么即使删除仓库,软件也不受影响。

删除PPA仓库和与之相关的软件包

sudo ppa-purge ppa:webapps/preview

备注:本命令删除ppa仓库较为完整。

♦ 管理sudo的授权

sudo的授权配置文件应放在/etc/sudoers.d/目录下单独进行管理,而不是直接修改 /etc/sudoers 文件。/etc/sudoers只进行基本环境的总体配置。

vim /etc/sudoers.d/mysudo.conf
==========================================================
bee ALL = (root) NOPASSWD: /path/to/somecmd

可以在 /etc/cron.daily 目录下面创建用于删除 /etc/sudoers.d/mysudo.conf 配置文件的 rm -rf 命令,一天之后相应的sudo授权就会被撤销。这样可以解决临时授权问题。

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
文字版,更适合学习使用,绝对完整! 目 录 第1篇 拥抱UbuntuServer 第1章 敲开Linux Server的大门 2 1.1 Linux到底是什么 2 1.2 为何选Linux,不选 1.2 Windows 3 1.2.1 Linux可以定制 4 1.2.2 Linux比Windows稳定 4 1.2.3 Linux比Windows响应快 4 1.2.4 Linux比Windows安全 5 1.2.5 Linux不用花钱买 6 1.2.6 Linux更适合远程管理 6 1.3 学习Linux的终南捷径 6 1.3.1 兴趣、试验 6 1.3.2 真正的捷径——LFS 7 1.4 选择哪个Linux发行版呢 7 1.4.1 先排除Gentoo 8 1.4.2 再排除Red Hat、CentOS和SUSE 8 1.4.3 Debian不错 8 1.4.4 推荐使用Ubuntu 8 1.5 应该买什么样的服务器呢 10 1.5.1 架构设计 10 1.5.2 服务器的选型 10 1.5.3 机房的选择 11 ............................................ 第9篇 数据备份与系统安全 第22章 最佳RAID方案:RAID10 330 22.1 RAID方案的选择 330 22.2 RAID10的实现 332 22.2.1 手动分区 333 22.2.2 第一块硬盘分区 334 22.2.3 分区复制 338 22.2.4 创建RAID阵列 339 22.2.5 在RAID上创建分区 340 22.2.6 保存分区 342 22.3 RAID10的日常维护 343 22.3.1 mdadm的主要工作模式 343 22.3.2 mdadm的选项 343 22.3.3 创建RAID阵列 345 22.3.4 查询RAID阵列 345 22.3.5 RAID的监控 346 22.3.6 RAID的启动/停止 346 22.4 故障处理 347 22.4.1 从RAID中移除设备 347 22.4.2 添加已有RAID物理卷 348 22.4.3 更换全新硬盘 348 22.5 添加备用硬盘 350 22.5.1 插入新硬盘 351 22.5.2 新硬盘分区 351 22.5.3 将新分区加入RAID 351 22.5.4 设置grub 352 22.5.5 故障模拟 352 22.6 RAID10的空间扩展 352 第23章 最佳数据安全方案: 第23章 RAID10+LVM 354 23.1 创建RAID物理卷 354 23.1.1 将第一块硬盘分区 354 23.1.2 剩余硬盘的分区处理 355 23.2 创建RAID阵列 355 23.2.1 创建RAID1阵列 355 23.2.2 创建RAID10阵列 356 23.3 LVM的创建和配置 356 23.3.1 创建LVM物理卷 356 23.3.2 LVM配置 356 23.4 创建/boot分区 358 23.5 LVM的相关命令 359 23.5.1 LVM物理卷相关命令 359 23.5.2 LVM卷组相关命令 360 23.5.3 LVM逻辑卷相关命令 363 23.6 添加新硬盘 365 23.6.1 插入新硬盘 365 23.6.2 配置RAID 365 23.6.3 在RAID上配置LVM 367 23.6.4 扩容文件系统 368 23.7 更换硬盘 368 23.8 LVM分区备份 368 23.8.1 创建快照 369 23.8.2 备份快照内容 369 23.8.3 删除快照 370 第24章 Ubuntu Server系统安全 371 24.1 系统安全更新 371 24.1.1 订阅安全列表 371 24.1.2 自动更新 371 24.2 控制台安全 372 24.3 用户、密码管理 372 24.3.1 关于root用户 372 24.3.2 关于sudo 373 24.3.3 关于/etc/sudoers 373 24.3.4 密码策略 375 24.4 ufw防火墙 376 24.4.1 启用、禁用ufw 376 24.4.2 基本规则设置 377 24.4.3 常用规则设置 378 24.4.4 高级规则设置 380 24.4.5 IP伪装 382 24.5 入侵检测 384 24.5.1 安装LAMP 384 24.5.2 安装、配置Snort 384 24.5.3 安装、配置BASE 387 24.6 肉鸡检测 392 24.6.1 chkrootkit的使用 393 24.6.2 rkhunter的使用 394 24.6.3 unhide的使用 396 24.7 数据完整性检测 397 24.7.1 安装Tripwire 398 24.7.2 配置Tripwire 400 24.7.3 初始化Tripwire数据库 403 24.7.4 执行完整性检测 403 24.7.5 检测报告分析 403 24.7.6 查看Tripwire数据库内容 405 24.7.7 使用Tripwire的注意事项 406 24.8 被入侵后的系统恢复 406 24.8.1 保持冷静 407 24.8.2 断开网络 407 24.8.3 找到黑客入侵的方法 407 24.8.4 黑客文件清理 412 24.8.5 恢复未受影响的服务 412 24.8.6 修复问题 412 24.8.7 恢复受影响的服务 412 24.8.8 监控系统 412

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值