29.ubuntu

 



1.设置IP

识别以太网接口名称
ip link

ip addr

在/etc/netplan目录中,找到xxx.yaml的文件

设置IP地址
注意:yaml文件格式:每一层的缩进,比上一层多2个空格
vim /etc/netplan/00-installer-config.yaml

# This is the network config written by 'subiquity'
network:
ethernets:
ens192:
dhcp4: no ##关闭DHCP,如果需要打开DHCP则写yes
addresses: ##可以写在这行,但要用[ ]
- 10.146.3.2/22 #配置的静态ip地址和掩码
optional: true
gateway4: 10.146.0.1 ##不需要用[ ]
nameservers:
addresses: [10.134.128.21] ###DNS服务器地址,多个DNS服务器地址需要用英文逗号分隔开
version: 2
renderer: networkd #指定后端采用systemd-networkd或者Network Manager,可不填写则默认使用systemd-workd


让配置生效
netplan apply
netplan generate ##校验配置
netplan --debug apply ##启用时调错

 

2.设置DNS另外的方法:

vim /etc/systemd/resolved.conf

[Resolve]
DNS=10.134.128.21 10.134.128.123

重启
systemctl restart systemd-resolved
systemctl enable systemd-resolved


mv /etc/resolv.conf /etc/resolv.conf.bak
ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

查看当前生效的DNS:
systemd-resolve --status | grep ‘DNS Server’
resolvectl status
resolvectl

三个命令结果一样,同时也可看到根DNS


networkctl status 也可看到ip及dns

 

如果Ubuntu云实例配置有cloud-init,则需要将其禁用

sudo vim /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
network: {config: disabled}

 

3.重启网络

networkctl


networkctl status

4.路由

man ip-route

 

5.查看版本信息

cat /etc/os-release

6.取消ssh的登录欢迎界面

7.主机名

hostnamectl set-hostname xxx

云实例上运行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

 

8.更新源

/etc/apt/source.list

apt-get update

apt-get

 

 

9.时区

tzselect
# 查看时区
timedatectl
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ##设置

# 显示当前系统时区
cat /etc/timezone
# 列出所有可用的时区
timedatectl list-timezones

timedatectl set-timezone Asia/Shanghai

 

10.修改时间戳显示格式

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

systemctl restart rsyslog

11.修改ls命令输出日期格式

vim /etc/profile
export TIME_STYLE='+%Y/%m/%d %H:%M:%S'

source /etc/profile

 

12.修改时间为24小时制

先修改时区tzselect
vim /etc/default/locale

LANG=en_US.UTF-8
LC_TIME=en_DK.UTF-8 ##添加一行

13.日志文件

默认系统日志文件
vim /etc/rsyslog.d/50-default.conf

系统/服务日志 /var/log/syslog 对应CentOS /var/log/messages

登陆、安全日志/var/log/auth.log 对应CentOS /var/log/secure

logrotate会把过期日志进行压缩zip存储
zcat syslog.2.gz|less

14.添加新用户并授权sudo

禁止登陆 -s /sbin/nologin

getent passwd #与cat /etc/passwd效果一样

usermod -aG sudo

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

dpkg-statoverride --update --add root admin 4750 /bin/su

--------
dpkg-statoverride命令用于Debian Linux中覆盖文件的所有权和模式,让dpkg于包安装时使得文件所有权与模式失效。

语法
dpkg-statoverride(选项)
选项
-add:为文件添加一个改写;
--remove:为文件删除一个改写;
--list:显示所有改写列表;
--update:如果文件存在,则立刻执行改写操作。
实例
修改文件夹的权限属性:

sudo dpkg-statoverride --update --add nagios nagios 751 /var/lib/nagios3
强制修改文件夹的权限属性:

sudo dpkg-statoverride --force --update --add root sasl 755 /var/spool/postfix/var/run/saslauthd
将文件从数据库中删除:

sudo dpkg-statoverride --remove /usr/bin/wall
---------

 

15.配置SSH公钥登陆

跟CentOS一样
ssh-keygen
ssh-copy-id username@remote_host

或者

cat ~/.ssh/id_rsa.pub | ssh username@remote_host "mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys && chmod -R go= ~/.ssh && cat >> ~/.ssh/authorized_keys"

16.配置sshd

apt-get install openssh-server openssh-client

vim /etc/ssh/sshd_config

17.安装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

 

17.可以删除的账号和组

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

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


18.设置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 删除重复命令;
export HISTCONTOL=ignoreboth

# 设置每条命令执行完立即写入HISTFILE(默认等待退出会话写入)
export PROMPT_COMMAND="history -a"
# 设置命令执行时间格式,记录文件增加时间戳(`whoami`用户信息)
export HISTTIMEFORMAT="[`whoami`
# 防止会话退出时覆盖其他会话写到HISTFILE的内容;
shopt

 

19.单独记录每个用户登录的操作history

将每个用户的shell命令执行历史以文件形式保存在/usr/share/.history/目录中。每个用户一个文件夹,每个文件以IP地址加shell命令操作时间的格式命名。


先调整history的输出格式 export HISTTIMEFORMAT="[​​whoami​​ %F %T] "

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


日志
cat 192.168.74.1.history.20210217_190612 | sed "s/#\([0-9]\+\)/date -d @\1

 


20.配置代理proxy

Git设置代理:
git config --global http.proxy http://192.168.x.xxx:xxxx
git config --global https.proxy https://192.168.x.xxx:xxxx
取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

apt单独设置代理:
仅apt-get使用,其它不用
sudo vim /etc/apt/apt.conf.d/proxy.conf
加入如下内容:注意,分号不能少
命令格式:Acquire::http::Proxy “http://proxyusr:password@yourproxyaddress:proxyport;

Acquire::http::Proxy "http://192.168.x.xxx:xxxx";
Acquire::https::Proxy "https://192.168.x.xxx:xxxx";

所有应用都可使用代理

格式:export http_proxy=http://proxyusr:password@yourproxyaddress:proxyport

在/etc/enviroment文件中直接加入代理,也可以在~/.bashrc中添加

export https_proxy='https://proxy_ip:port'
export http_proxy='http://proxy_ip:port'

 

21.查看已安装的软件

dpkg -l ##查看安装的软件
dpkg -L 或whereis ##查看安装路径
apt list --installed

 

22.配置apt源

vi /etc/apt/sources.list

删除原文内容,修改内容如下:

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

刷新apt列表

sudo apt update

安装某个包

apt install -y ffmpeg

lsb_release -a #查看系统内核版本号

 

23.时间同步

apt-get install chrony
vi /etc/chrony.conf
#pool 2.ubuntu.pool.ntp.org iburst maxsources 2
server 10.134.128.21 iburst

systemctl restart chronyd.service
# 执行时间同步操作
chronyc sources -v

# 查看当前系统时间
date


在20.0.4及之后版本中,内置了时间同步功能,默认使用systemd的timesyncd服务来激活。timesyncd替代了旧的ntpdate功能。

timedatectl status


/etc/systemd/timesyncd.conf ##设置同步源
NTP为主时间同步服务器,FallbackNTP 为备用服务器


timedatectl set-ntp on ###如果没有启动timedatectl,用此命令启动。
重启timesyncd服务
service systemd-timesyncd restart

timesyncd与ntpdate是断点更新,粗爆。
用ntpd渐进式更新

timedatectl set-ntp no ##关闭timesyncd

配置ntpd
vim /etc/ntp.conf
server 10.134.128.21
#restrict -4 default kod notrap nomodify nopeer noquery limited ##各参数含义
#ignore : 拒绝所有类型的ntp连接
#nomodify : 客户端不能使用ntpc与ntpq两支程式来修改服务器的时间参数
#noquery : 客户端不能使用ntpq、ntpc等指令来查询服务器时间,等于不提供ntp的网络校时
#notrap : 不提供trap这个远程时间登录的功能
#notrust : 拒绝没有认证的客户端
#nopeer : 不与其他同一层的ntp服务器进行时间同步

restrict default nomodifynotrapnoquery # 关闭所有的 NTP 要求封包
restrict 127.0.0.1    #这是允许本级查询
restrict 192.168.0.1 mask 255.255.255.0 nomodify
#在192.168.0.1/24网段内的服务器就可以通过这台NTP Server进行时间同步了
2. 上层主机的设定
#  要设定上层主机主要以 server 这个参数来设定,语法为:
#  server [IP|HOST Name] [prefer]
#  Server 后面接的就是我们上层 Time Server 啰!而如果 Server 参数
#  后面加上perfer的话,那表示我们的 NTP 主机主要以该部主机来作为
#  时间校正的对应。另外,为了解决更新时间封包的传送延迟动作,
#  所以可以使用driftfile来规定我们的主机
#  在与 Time Server 沟通时所花费的时间,可以记录在driftfile
#  后面接的文件内,例如下面的范例中,我们的 NTP server 与
#  cn.pool.ntp.org联机时所花费的时间会记录在 /etc/ntp/drift文件内
server 0.pool.ntp.org

server 1.pool.ntp.org

server 2.pool.ntp.org

server cn.pool.ntp.org prefer ##优先主机

#其他设置值,以系统默认值即可

server 127.127.1.0 # local clock

fudge 127.127.1.0 stratum 10

driftfile /var/lib/ntp/drift
broadcastdelay 0.008
keys /etc/ntp/keys

 


重加载ntpd
systemctl reload ntp.service


ntpq -p结果
remote refid st t when poll reach delay offset jitter
========================================================================
*10.134.128.21 10.134.128.123 5 u 49 64 7 1.638 282.351 7.691


星号 ( * ) :表示该源是当前引用
remote:响应请求的NTP服务器的名称(IP地址或域名),带“*”的表示本地NTP服务器与该服务器同步

refid:远程NTP服务器使用的上一级ntp服务器的IP地址

st:远程NTP服务器的级别,由于NTP是层级结构,有顶端的服务器,多层的Relay Server再到客户端。所以服务器从高到低,级别可以设定为1~16级。为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器。

t:本地NTP服务器与远程NTP服务器的通信方式。u:单播;b:广播;I:本地

when:上次成功请求后到现在的秒数

poll:本地NTP服务器与远程NTP服务器同步的时间间隔。

reach:这是一个八进制的值,用来测试衡量前八次查询是否成功和服务器连接。377表示都成功,0表示不成功

delay:网络延时,单位为微秒(μs)

offset:本地NTP服务器与远程NTP服务器的时间偏移,单位为毫秒(ms)。offset越接近于0,主机与NTP服务器的时间越接近

jitter:查询偏差的分布值,用于表示远程NTP的网络延时是否稳定,单位为微秒(μs)。

 

23.2客户端

ntpdate -t 2 -p 2 -u pool.ntp.org  
同步时间,如果快速同步时间,可以适当修改-t / -p参数的数值
-t : 指定等待响应的时间
-p : 指定从每个服务器获取的样本数量
-d : 开启调试接口,并不设置时钟

 

24.关闭防火墙

ufw disable

25.关闭selinux

默认没有安装,默认的安全组件为AppArmor,如要安装则先移除
systemctl stop apparmor

apt purge apparmor

apt update

安装SELinux及相关包
sudo apt install policycoreutils selinux-utils selinux-basics

查看SELinux安装情况
sudo selinux-activate

如果出现如上内容,则安装成功。
将SELinux置于enforcing状态
sudo selinux-config-enforcing

重启系统,SELinux将给系统资源加标签。会花费一定时间。


26.apparmor

强制 - 在强制模式下,系统开始强制执行规则并在 syslog 或 auditd 中报告违规尝试(仅当安装了 auditd 时)并且不允许操作。
投诉 - 在投诉模式下,系统不执行任何规则。它只会记录违规尝试。

apparmor_status #查看apparmor状态
aa-complain /usr/sbin/mysqld ##把配置模式改为抱怨模式
aa-enforce /usr/sbin/mysqld ##把配置模式改为强制模式

systemctl stop apparmor ##停止apparmor

# /etc/init.d/apparmor stop ##清除配置文件缓存

# /etc/init.d/apparmor teardown ##卸载配置文件

 

27.开机欢迎画面

Ubuntu 使用的是,它是一个动态 motd 生成工具。

UNIX/Linux 系统管理员通常通过在文件 中维护文本来向控制台和远程用户传达重要信息,该文件由 pam_motd(8) 模块在交互式 shell 登录时显示。

centos,此文件是静态文本,通常由发行版安装并仅在版本升级时更新,或者由本地管理员使用相关信息覆盖。

Ubuntu 引入了框架,通过该框架,motd(5) 在登录时从一组脚本中动态获取。

/etc/update-motd.d/* 中的可执行脚本在每次登录时由 pam_motd(8) 作为 root 用户执行,并且这些信息连接在 /var/run/motd 中。

查看cat /var/run/motd.dynamic可以看到登录页面

脚本存放的位置在目录中:

bob@ubuntu-20-04:~$ ls -l /etc/update-motd.d/


bob@ubuntu-20-04:~$ cat /etc/update-motd.d/00-header


可以禁用所有脚本

bob@ubuntu-20-04:~$ sudo chmod -R 644 /etc/update-motd.d/

如果要禁用单个脚本,请运行以下命令:

bob@ubuntu-20-04:~$ sudo chmod -x /etc/update-motd.d/00-header


如何在 /etc/update-motd.d/ 目录中创建我自己的脚本?

只需创建一个 shell 脚本,名称自定义,名称前面的序号根据需要填写,数字越大优先级越低。如下所示:

bob@ubuntu-20-04:~$ sudo touch /etc/update-motd.d/99-custom-msg

bob@ubuntu-20-04:~$ sudo vim /etc/update-motd.d/99-custom-msg

Hello Ubuntu 20-04

添加可执行权限:

bob@ubuntu-20-04:~$ sudo chmod +x /etc/update-motd.d/99-custom-msg

28.修改默认编辑器

方法一:
update-alternatives --config editor

方法二:
vim /root/.selected_editor
SELECTED_EDITOR="/usr/bin/vim.basic"

29.重启开机运行

方法一:rcconf 或 sysv-rc-conf

方法二:/etc/rc0.d ~ /etc/rc6.d、rcS.d
S开头的表示启动,K开头的表示不启动

 

错误
Ubuntu出现E: Failed to fetch
DNS,添加一个阿里DNS /etc/resolved.conf

29.密码登录失败锁定设置

方法一:pam_tally2模块

vim /etc/pam.d/sshd ##限制远程
在第二行添加 ##注意必须添加在前面

auth required pam_tally2.so deny=3 unlock_time=5 even_deny_root root_unlock_time=10

onerr=fail 表示定义了当出现错误时的缺省返回值;###可以加上这个参数
even_deny_root 也限制root用户;
deny 设置普通用户和root用户连续错误登陆的最大次数,超过最大次数,则锁定该用户;
unlock_time 设定普通用户锁定后,多少时间后解锁,单位是秒;
root_unlock_time 设定root用户锁定后,多少时间后解锁,单位是秒;


vim /etc/pam.d/login ##限制终端登录
auth required pam_tally2.so deny=3 unlock_time=5 even_deny_root root_unlock_time=10

解锁指定用户:
pam_tally2 --user

pam_tally2 -r -u ubuntu


方法二:pam_faillock 模块
将登录尝试失败的数据储存在 /var/run/faillock 目录下每位用户的独立文件中
添加以下命令行到 /etc/pam.d/system-auth文件和/etc/pam.d/password-auth文件中的对应区段:

auth required pam_faillock.so preauth silent audit deny=3 unlock_time=600
auth sufficient pam_unix.so nullok try_first_pass
auth [default=die] pam_faillock.so authfail audit deny=3
account required pam_faillock.so

注意:
auth required pam_faillock.so preauth silent audit deny=3 必须在最前面。

even_deny_root 选项 ##适用于root

查看每个用户的尝试失败次数

# faillock

# faillock --user lt --reset #解锁一个用户的账户

faillock --user username #使用以下命令查看失败计数

faillock --user username --reset #重置失败计数

faillock --dir /var/run/faillock #设置保存失败记录的目录

30. 软件包管理

dpkg --get-selections #查看已安装的软件

apt-get --purge remove 程序包名 ##卸载程序和所有配置文件

apt-get remove <programname> ##只卸载程序。如果你移除程序但保留配置文件


31. 密码复杂度

方法一:使用cracklib模块
apt-get install libpam-cracklib
复杂度: 编辑 /etc/pam.d/common-password, ##centos是/etc/pam.d/system-auth
安装cracklib模块后会在common-password文件下自动增加一行password requisite pam_cracklib.so retry=3 minlen=8 difok=3

修改为如下:

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 use_authtok try_first_pass sha512 remember=12


type=XXX 这个的意思就是修改密码时的提示符

retry=N: 此选项定义用户在重试输入多少次密码后,返回一个错误信息,然后不准继续输入。缺省是1次。

difok=N:新密码中必须有几个字符要与旧密码不同,如果新密码中有1/2以上的字符与旧密码不同时,该新密码就会被接受。

minlen=N:新密码的最小长度。

dcredit=N:新密码中可以包含数字。

ucredit=N:新密码中可以包含的大写字母。

lcredit=N:新密码中可以包含的小写字母。lcredit=-1 ,至少一个;一般用负数,好理解

ocredit=N:新密码中可以包含的特殊字符。

 remember=12   12次密码不能重复

使用过的密码保存在/etc/security/opasswd中


连续登录失败处理功能:
编辑/etc/pam.d/common-auth,在首行中插入下面的语句 ##centos是/etc/pam.d/password-auth
auth required pam_tally2.so deny=5 unlock_time=300 even_deny_root root_unlock_time=300

 

方法二:pwquality.conf
先安装apt-get install libpam-pwquality

# vim /etc/security/pwquality.conf
# 设置最小长度(下面的示例表示 8 个字符)
minlen = 8

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

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

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

# 要求新密码中至少有一个小写字符
lcredit =-1

# 要求新密码中至少有一个大写字符
ucredit = -1

# 新密码至少需要一位数字
dcredit = -1

# 新密码中至少需要一个其他字符
ocredit = -1

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

# 设置新密码中不能出现在旧密码中的字符数
difok = 5

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

# 设置密码中不得包含的以空格分隔的单词列表
badwords = denywords1 denywords2 denywords3

32.设置过期天数

vi /etc/login.defs
# 设置密码到期天数
PASS_MAX_DAYS 90

# 可用的最少天数
PASS_MIN_DAYS 1

# 设置到期警告天数
PASS_WARN_AGE 

 

33.新建用户

ubuntu新建用户跟centos下有点不一样

在ubuntu下如果只用useradd username来创建用户则1.没有home目录,2.没有bash,登录之后进入没有bash的模式

 ubuntu在新建用户时须指定home目录,指定bash,或者用adduser向导建立用户

useradd -m -s /bin/bash username

或者建好后chmod -s /bin/bash修改

posted @   天涯160  阅读(157)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示