Linux使用小技巧

摘要

最小化安装CentOS7后,想查看我的IP,发现 ifconfig命令不存在。

在最小化的CentOS7中,查看网卡信息

1
ip addr

ifconfig命令依赖于net-tools,为了方便起见 我们还是启用ifconfig 命令。

1
yum install -y net-tools
1
2
systemctl stop firewalld
systemctl disable firewalld
1
2
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
1
2
swapoff -a
sed -i_bak '/swap/d' /etc/fstab
1
yum install -y wget

备份系统旧配置文件

1
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

获取yum配置文件到/etc/yum.repos.d/

1
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

mirrorlist= 开头行前面加 # 注释掉

1
sed -e 's|^mirrorlist=|#mirrorlist=|g' -i.bak /etc/yum.repos.d/CentOS-*.repo

更新缓存

1
2
yum clean all
yum makecache

EPEL (Extra Packages for Enterprise Linux) 是由 Fedora Special Interest Group 为企业 Linux 创建、维护和管理的一个高质量附加包集合适用于但不仅限于 Red Hat Enterprise Linux (RHEL), CentOS, Scientific Linux (SL), Oracle Linux (OL)

备份系统旧配置文件

1
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.bak

获取epel配置文件到/etc/yum.repos.d/

1
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

更新缓存

1
2
yum clean all
yum makecache
1
yum install -y unzip

Centos7在使用最小化安装的时候,没有安装自动补全的包,需要自己手动安装。

1
yum install -y  bash-completion 

安装好后,重新登陆即可(刷新bash环境)

配置一台Linux到另一台Linux的免密登录

A192.168.1.1客户机
B192.168.1.2目标机

要达到的目的:A机器ssh登录B机器无需输入密码

加密方式选 rsa|dsa均可以,默认dsa

两种方法都可行

这种情况适用于自己有远程主机的账号密码

在192.168.1.1上执行命令:

(命令1有交互按自己的需求填写,简单的就全部直接回车)

(命令2需要输入192.168.1.2 的 root 密码)

1
2
3
4
ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.1.2 #复制公钥到远程主机
# 有的机器提示:-bash: ssh-copy-id: command not found
cat ~/.ssh/id_rsa.pub | ssh root@192.168.1.2 'cat >> .ssh/authorized_keys'

这种情况适用于把公钥交给服务器管理员

1
ssh-keygen -t [rsa|dsa]

将会生成密钥文件id_rsa,id_rsa.pubid_dsa,id_dsa.pub

.pub文件复制到B机器的.ssh目录,并执行以下命令

1
2
3
4
cat id_rsa.pub >> ~/.ssh/authorized_keys
#普通用户之间免密登陆,需要执行以下命令
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

从A机器登录B机器的目标账户,不再需要密码了(直接运行ssh 192.168.1.2

安装图形用户接口X Window System,在命令窗口输入:

1
yum groupinstall "X Window System"

安装完成会提示complete!

提示:X Window System本身是一个非常复杂的图形化作业环境,我们可以将它分成3个部分,分别是X ServerX ClientX ProtocolX Server主要是处理输入输出的信息,X Client执行大部分应用程序的运算功能,X Protocol则是建立X ServerX Client的沟通管道。

X Window通过软件工具及架构协议来建立操作系统所用的图形用户界面,此后则逐渐扩展适用到各形各色的其他操作系统上,几乎所有的操作系统都能支持与使用X WindowGNOMEKDE也都是以X Window为基础建构成的。

安装图形用界面gnome,在命令窗口输入:

1
yum groupinstall "GNOME Desktop"

安装完成,同样会提示compete!

提示:检查已经安装的软件以及可以安装的软件,用命令yum grouplist

同样在root用户权限下,设置centos系统默认的启动方式,输入命令如下:

systemctl set-default multi-user.target //设置成命令模式

systemctl set-default graphical.target //设置成图形模式

注意:centos7和centos6设置方式不同!

重启系统即可

快速设置方法:

实际一行命令就搞定

查看一下网卡名称

1
ip a

设置ens33网卡ip地址

1
ip addr 192.168.10.230/24 dev ens33

其它快速设置参考命令:

显示网卡IP信息

1
ip addr

显示ens33网卡IP信息

1
ip addr show ens33

开启或关闭ens33网卡

1
ip link set ens33 up/down

设置ens33网卡IP地址192.168.10.230

1
2
3
ip addr add 192.168.10.230/24 dev ens33
ifconfig ens33 192.168.10.230/24
ifconfig ens33 192.168.10.230 netmask 255.255.255.0

删除ens33网卡IP地址192.168.10.230

1
ip addr del 192.168.10.230/24 dev ens33

查看路由信息,也可以使用ip route命令

1
ip route list

设置默认网关,也可以在后面加上dev ens33

1
2
ip route add default via 192.168.10.1
ip route add default via 192.168.10.1 dev ens33

删除默认网关,也可以直接写ip route del default

1
ip route del default via 192.168.10.1

设置192.168.10.0网段的网关为192.168.10.1,数据走eth0接口

1
ip route add 192.168.10.0/24 via 192.168.10.1 dev eth0

删除192.168.100.123.0/24网段的路由

1
ip route del 192.168.10.0/24

获取当前启动级别

1
systemctl get-default

更改启动模式为多用户命令模式

1
2
3
systemctl set-default multi-user.target
rm '/etc/systemd/system/multi-user.target'
ln -s '/usr/lib/systemd/system/multi-user.target' '/etc/systemd/system/multi-user.target'

更改启动模式为图像模式

1
2
3
systemctl set-default graphical.target
rm '/etc/systemd/system/default.target'
ln -s '/usr/lib/systemd/system/graphical.target' '/etc/systemd/system/default.target'
1
locale

此处以zh_CN.UTF-8为例

编辑文件/etc/profie

1
2
3
vi /etc/profile
export LANG=zh_CN.UTF-8
source /etc/profile

通常来说boot分区都不大,满了怎么解决呢。

解决方法:删除旧版的kernel

查询安装的所有kernel

1
rpm -qa|grep kernel

https://img.bwcxtech.com/img/20200928153629.png

查看当前使用的kernel

1
uname -a

https://img.bwcxtech.com/img/20200928153635.png

删除未在使用的kernel

1
yum remove kernel-3.10.0-957.el7.x86_64

使用yum时提示yum.pid 已被锁定

https://img.bwcxtech.com/img/20200928153907.png

PackageKit 是Linux系统上用来检查、更新和安装软件的一个工具,当它在后台运行时,它会阻塞用户的Package管理命令。

1
2
3
[root@localhost ~]# systemctl stop packagekit
[root@localhost ~]# systemctl disable packagekit
[root@localhost ~]# yum remove PackageKit

1
vi /etc/yum/pluginconf.d/langpacks.conf 

将第一行:enable=1改为enable=0

执行 yum就不会被锁定了。

清华大学源

不同版本源不同,根据自己情况选择

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
sudo tee /etc/apt/sources.list <<-'EOF'
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
EOF
1
2
swapoff -a
sed -i_bak '/swap/d' /etc/fstab
1
sed -i 's/APT::Periodic::Unattended-Upgrade "1";/APT::Periodic::Unattended-Upgrade "0";/' /etc/apt/apt.conf.d/20auto-upgrades
1
apt install -y bash-completion iputils-ping net-tools telnet

Ubuntu预安装的是tiny版本,在使用vi编辑器编辑文本时实在难用,我们需要安装vim的full版本。

卸载预装的版本

1
sudo apt remove -y vim-common

安装full版vim

1
sudo apt install -y vim

ubuntu默认shell是dash

1
ls -l /bin/sh

修改默认shell

1
sudo dpkg-reconfigure dash

选择No

1
2
3
4
5
6
sudo apt-get update
sudo apt-get -y install xfce4
sudo apt-get -y install xrdp
echo xfce4-session >~/.xsession
sudo service xrdp start
sudo systemctl enable xrdp
1
2
timedatectl set-timezone Asia/Shanghai
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

在EOF前面多了一个减号-作用是去除文本里面所有行的tab前缀。

1
cat <<-EOF

EOF用引号括起来(单引号和双引号一样),但是结尾处的EOF不要引号;作用是是否对文本进行变量扩展。

不加引号

1
2
3
4
5
6
[root@localhost ~]# name=zhangsan
[root@localhost ~]# cat <<EOF >a.txt
> user="${name}"
> EOF
[root@localhost ~]# cat a.txt 
user="zhangsan"

加引号

1
2
3
4
5
6
[root@localhost ~]# name=zhangsan
[root@localhost ~]# cat <<'EOF' >a.txt
> user="${name}"
> EOF
[root@localhost ~]# cat a.txt 
user="${name}"

查看/etc/systemd/system/multi-user.target.wants目录是否有对应的软连接文件