Web狗的要懂的Linux基础知识

Linux知识众多,发行版又广,为了避免每次着急时查资料的尴尬情况出现,特此将我平时可能用到的命令都记录下来,以方便后面使用。

更新源

Ubuntu

# 备份更新源
mv /etc/apt/sources.list /etc/apt/sources.list.bak

# 20.04 清华源
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse" >> /etc/apt/sources.list

# 19.04 清华源
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ disco main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ disco main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ disco-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ disco-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ disco-backports main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ disco-backports main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ disco-security main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ disco-security main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ disco-proposed main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ disco-proposed main restricted universe multiverse" >> /etc/apt/sources.list

# 18.04 阿里源
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list

# 18.04 清华源
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list

# 16.04 清华源
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse" >> /etc/apt/sources.list

# 14.04 阿里源
echo "deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse" >> /etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse" >> /etc/apt/sources.list

Kali

# 备份更新源
mv /etc/apt/sources.list /etc/apt/sources.list.bak

# 中科大源
echo "deb http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib" >> /etc/apt/sources.list
echo "deb-src http://mirrors.ustc.edu.cn/kali kali-rolling main non-free contrib" >> /etc/apt/sources.list

# 浙大源
echo "deb http://mirrors.zju.edu.cn/kali kali-rolling main contrib non-free" >> /etc/apt/sources.list
echo "deb-src http://mirrors.zju.edu.cn/kali kali-rolling main contrib non-free" >> /etc/apt/sources.list

# 东软大学
echo "deb http://mirrors.neusoft.edu.cn/kali kali-rolling/main non-free contrib" >> /etc/apt/sources.list
echo "deb-src http://mirrors.neusoft.edu.cn/kali kali-rolling/main non-free contrib" >> /etc/apt/sources.list

Debian

# 备份更新源
mv /etc/apt/sources.list /etc/apt/sources.list.bak

# Debian 10 buster 网易源
echo "deb http://mirrors.163.com/debian/ buster main contrib non-free" >> /etc/apt/sources.list
echo "deb http://mirrors.163.com/debian/ buster-updates main contrib non-free" >> /etc/apt/sources.list
echo "deb http://mirrors.163.com/debian/ buster-backports main contrib non-free" >> /etc/apt/sources.list
echo "deb http://mirrors.163.com/debian-security buster/updates main contrib non-free" >> /etc/apt/sources.list


# Debian 9 stretch 网易源
echo "deb http://mirrors.163.com/debian/ stretch main contrib non-free" >> /etc/apt/sources.list
echo "deb http://mirrors.163.com/debian/ stretch-updates main contrib non-free" >> /etc/apt/sources.list
echo "deb http://mirrors.163.com/debian/ stretch-backports main contrib non-free" >> /etc/apt/sources.list
echo "deb http://mirrors.163.com/debian-security stretch/updates main contrib non-free" >> /etc/apt/sources.list

# Debian 8 jessie 网易源
echo "deb http://mirrors.163.com/debian/ jessie main contrib non-free" >> /etc/apt/sources.list
echo "deb http://mirrors.163.com/debian/ jessie-updates main contrib non-free" >> /etc/apt/sources.list
echo "deb http://mirrors.163.com/debian/ jessie-backports main contrib non-free" >> /etc/apt/sources.list
echo "deb http://mirrors.163.com/debian-security jessie/updates main contrib non-free" >> /etc/apt/sources.list

CentOS

# 备份更新源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

# 阿里云的CentOS-Base.repo 到/etc/yum.repos.d/
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 刷新源
yum update

# 生成缓存
yum makecache

这里我用的是CentOS7的更新源,其他源参考如下:

# CentOS 5 国内源替换
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo

# CentOS 6 国内源替换
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

proxychains

安装

Ubuntu/Debian

apt install  proxychains

CentOS

# 下载到指定目录
cd /usr/local/src
git clone https://github.com/rofl0r/proxychains-ng.git
cd proxychains-ng

# 安装GCC
yum install gcc

# 手动编译
./configure
make && make install
make install-config

配置

Kali/Ubuntu/Debian

vim /etc/proxychains.conf

CentOS

vim /usr/local/etc/proxychains.conf

测试

proxychains curl https://www.google.com

zsh

Ubuntu/Kali/Debian安装

# 安装zsh
apt install zsh

# 更改默认shell为zsh
chsh -s /bin/zsh

# 安装oh-my-zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

# 下载命令补全插件
git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions

# zshrc配置文件中修改如下内容
plugins=(git zsh-autosuggestions)

# 重新加载zsh配置
zsh

CentOS安装

# 安装zsh
yum install zsh

除了安装使用`yum以外,其他配置方法和上面其他发行版系统一样,这里就不再赘述了。

zsh插件

CentOS7安装zsh一些插件和macOS下安装相差不多,这里就简单提一下吧:

autojump

快捷目录跳转插件

yum install autojump autojump-zsh -y

~/.zshrc 中配置

plugins=(其他的插件 autojump)

输入zsh命令生效配置后即可正常使用j命令,下面是简单的演示效果:

# root @ centos in ~/test/23333 [18:45:14]
$ pwd
/root/test/23333

# root @ centos in ~/test/23333 [18:45:16]
$ cd ~


# root @ centos in ~ [18:45:17] 直接 j 2 就跳转到历史目录了
$ j 2
/root/test/23333

# root @ centos in ~/test/23333 [18:45:20]
$ pwd
/root/test/23333

zsh-syntax-highlighting

命令输入正确会绿色高亮显示,可以有效地检测命令语法是否正确

cd ~/.oh-my-zsh/custom/plugins
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git

~/.zshrc 中配置

plugins=(其他的插件 zsh-syntax-highlighting)

输入zsh命令生效配置

autosuggestions

终端下自动提示接下来可能要输入的命令,这个实际使用效率还是比较高的

cd ~/.oh-my-zsh/custom/plugins
git clone https://github.com/zsh-users/zsh-autosuggestions.git

~/.zshrc 中配置

plugins=(其他的插件 zsh-autosuggestions)

输入zsh命令生效配置

SSH

安装SSH

Ubuntu

# 直接安装报错
 apt install openssh-server

居然报错了,内容如下:

下列软件包有未满足的依赖关系:
 openssh-server : 依赖: openssh-client (= 1:7.6p1-4)
                  依赖: openssh-sftp-server 但是它将不会被安装
                  推荐: ssh-import-id 但是它将不会被安装
E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系。

手动解决依赖,这里是因为openssh-server是依赖于openssh-clien的,那我们手动安装下指定的版本的openssh-client

# 手动解决依赖
apt install openssh-client=1:7.6p1-4

# 重新安装openssh-server
apt install openssh-server

CentOS

# 安装SSH
yum install openssh-server

Kali/Debian

apt install openssh-server

允许root远程登陆

Ubuntu

# 允许root远程登陆
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config

# SSH开机自启
systemctl enable ssh

# 开启SSH
/etc/init.d/ssh start

CentOS/Debian/Kali

# 允许root远程登陆
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config

# SSH开机自启
systemctl enable sshd

# 开启SSH
systemctl start sshd

ssh连接非22端口

 ssh -p 端口 x.x.x.x

ssh使用公私钥登陆

 # 生成公私钥
 ssh-keygen

 #     到ssh公私钥放的文件夹
 cd /root/.ssh/

 # 将公钥的内容拷贝到authorized_keys文件中
cat id_rsa.pub >> authorized_keys

 # 关闭密码登陆
 echo "PasswordAuthentication no" >> /etc/ssh/sshd_config

 # 重启ssh
 /etc/init.d/ssh restart

效果如下:
将服务器的rsa私钥下载下来,然后拷贝到自己的机器上。此时必须使用私钥匙登陆才可以成功:

# 将拷贝下来的私钥设置600权限
chmod 600 ~/Downloads/test_rsa

# 使用私钥登陆
ssh -i ~/Downloads/test_rsa root@10.211.55.9

一台电脑保存多SSH KEY

假设有这样一个场景

服务器IP 私钥位置
1.1.1.1 ~/.ssh/id_rsa_a
2.2.2.2 ~/.ssh/id_rsa_b

想要自己的电脑保存这两台服务器的私钥的话,可以这样配置:

~/.ssh 目录下创建 config 文件:

vim ~/.ssh/config

输入以下信息:

Host 1.1.1.1
    IdentityFile ~/.ssh/id_rsa_a

Host 2.2.2.2
    IdentityFile ~/.ssh/id_rsa_b

然后设置私钥的权限:

chmod 600 ~/.ssh/id_rsa_a
chmod 600 ~/.ssh/id_rsa_b

ssh取消公私钥登陆

# 清空.ssh文件夹
rm -rf /root/.ssh/*

# 允许密码登陆
echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config

# 重启SSH服务
/etc/init.d/ssh restart

# CentOS 服务器这样重启
systemctl restart sshd

scp拷贝文件

scp 本地文件 用户名@服务器地址:要拷贝的路径

scp拷贝非22端口文件

 scp -P端口 本地文件 用户名@服务器地址:要拷贝的路径

FTP

连接FTP

最基本的连接方式:

ftp ip

FTP连接非默认端口

ftp ip port

或者:

➜  ~ ftp
ftp> open ip port

一个完整的登录示例:

➜  ~ ftp 116.xx.xx.xx 21
Connected to 116.xx.xx.xx.
220 Welcome to www.net.cn FTP service.
Name (116.xx.xx.xx:sqlsec): hyuxxxxxxxxxx
331 Please specify the password.
Password:
230 Login successful.
ftp>

基本操作

目录相关

pwd               # 显示远程计算机上的当前目录
ls/dir            # 列出当前远程目录的内容,可以使用该命令在Linux下的任何合法的ls选项
cd                # 移动到cd 后的目录
cdup/cd ..        # 返回上一级目录
lcd               # 列出当前本地目录路径

mkdir             # 在远程系统中创建目录
rname             # 重命名一个文件或目录
redir             # 删除远程目录
delete            # 删除远程文件
mdelete           # 删除多个远程文件

文件上传下载

binary            # 用于二进制文件传送(图像文件等)
ascii             # 用于文本文件传送
get/mget          # 在当前远程目录下复制(一个/多个)文件到本地文件系统当前目录
put/mput          # 从当前目录把文件复制到当前远程目录

退出

!                 # 临时退出ftp模式,返回本地Linux Shell模式,键入exit返回
close             # 关闭当前连接
bye/quit          # 关闭连接并退出ftp命令模式

JDK

OracleJDK

因为 JDK 官网需要登录验证才可以下载的原因,不能直接通过 wget 来下载 tar.gz 安装包了,得自己手动 copy 一下。

# 解压到/usr/lib/目录下
sudo tar -zxvf jdk-8u221-linux-x64.tar.gz -C /usr/lib/

# 编辑/etc/profile 配置文件
sudo vim /etc/profile

末尾添加如下内容:

JAVA_HOME=/usr/lib/jdk1.8.0_221
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar
export PATH JAVA_HOME CLASSPATH
# 重新载入配置文件
source /etc/profile

# 系统注册此 JDK 300 是一个整数,在自动模式下,这个数字越高的选项,其优先级也就越高
sudo update-alternatives --install /usr/bin/java java /usr/lib/jdk1.8.0_221/bin/java 300

# 验证是否安装成功
java -version

OpenJDK

# 搜索软件列表里的 JDK
apt search openjdk

# 安装 JDK
apt install openjdk-11-jdk

# 验证是否安装成功
java -version

版本切换

安装了多个版本的jdk,你可以通过以下命令在这些版本之间切换:

sudo update-alternatives --config java

img

Python

Centos 7

Centos7 自带Python2版本和pip2,所以我们只要直接安装Python3即可:

# 搜索可用的Python3版本
yum search python3

# 安装Python3.6版本
yum install python36

# 查看Python版本
python3 -V
Python 3.6.8

# 查看pip版本
pip3 -V
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)

# 升级pip版本
pip install --upgrade pip
pip3 install --upgrade pip

# 安装ipython
pip install ipython
pip3 install ipython

安装pyenv

pyenv Python多版本管理神器,安装起来也很简单:

# 安装基础依赖
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y

# 下载pyenv
git clone https://github.com/pyenv/pyenv.git ~/.pyenv

# 配置pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
zsh

基础入门使用可以参考我的这篇文章macOS pyenv入门使用记录

Ubuntu

pip3安装

sudo apt update
sudo apt install python3-pip

搜索替换

grep 搜索文件内容

# 搜索/home/sqlsec/Desktop/BBS/中的所有 ph p后缀中的 password 关键词
grep -r "password" --include="*.php"  /home/sqlsec/Desktop/BBS/

# 可以添加多个后缀
grep -r "关键词" --include="*.后缀1"  --include="*.后缀2" 目标路径

# 搜索/home/sqlsec/Desktop/BBS/中的所有php后缀中的password关键词
# 不搜索js后缀
grep -r "password" --include="*.php" --exclude="*.js"  /home/sqlsec/Desktop/BBS/

find 查找文件名

# 查找当前目录下所有 .php 的文件
find . -name "*.php"

# 查找所有文件中的 log 文件
find / -name "*.log"

# 目前目录及其子目录下所有最近 20 天内更新过的文件列出
find . -ctime -20

# 查找系统中所有文件长度为0的普通文件,并列出它们的完整路径
find / -type f -size 0 -exec ls -l {} \

查找内容并替换

# 将 file_name文件中的 old 全部替换为 new
sed -i 's/old/new/g'  file_name

# 修改 MySQL 的默认端口为 3307
sed -i 's/3306/3307/g' my.cnf

# 批量替换文件夹内容
sed -i "s/old/new/g" `grep "old" -rl floder_path`

用户

修改用户密码

# 修改当前用户密码
passwd

# 修改指定用户密码
passwd 用户名

# 修改root密码
sudo passwd

adduser 新建用户

adduser test 

useradd 新建用户

# 创建test用户并自动生成主目录且指定shell为bash
useradd -d /home/test -m test -s /bin/bash

# 设置test用户密码
passwd test

删除用户

# 只删除用户
sudo userdel 用户名

# 连同用户主目录一块删除
sudo userdel -r 用户名

MySQL

Ubuntu

# 安装MySQL客户端
apt install mysql-client

# 安装MySQL服务端
apt install mysql-server

# 启动MySQL
/etc/init.d/mysql start

# 初始化MySQL
sudo mysql_secure_installation

Debian/Kali

# 安装MySQL客户端
apt install mariadb-client

# 安装MySQL服务端
apt install mariadb-server

# 启动MySQL
/etc/init.d/mysql start

# 初始化MySQL
sudo mysql_secure_installation

CentOS

# 安装客户端和服务端 默认root密码为空
yum install mariadb-server

# 启动 mariadb
sudo systemctl start mariadb

# 设置开机自启
sudo systemctl enable mariadb

# 查看mariadb状态
sudo systemctl status mariadb

# 初始化MySQL
sudo mysql_secure_installation

允许 root 远程登陆

# 允许root外部访问连接
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '这里是root密码' WITH GRANT OPTION;

# 刷新权限
FLUSH PRIVILEGES; 

查看此时的用户状态:

mysql> select user, host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | %         |
| root | 127.0.0.1 |
| root | ::1       |
| root | localhost |
+------+-----------+

关闭 root 远程登录

# 删除 host 为 % 的用户
DELETE FROM mysql.user WHERE User= "root" and Host="%";

# 刷新权限
FLUSH PRIVILEGES; 

查看此时的用户状态:

mysql> select user, host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
| root | localhost |
+------+-----------+

修改密码

# 修改 root 密码的 SQL语句
use mysql;
set password for 'root'@'localhost' = password('你设置的密码');

# 刷新权限 并退出
flush privileges;
quit; 

或者直接命令行下修改:

mysqladmin -uroot -poldpassword password newpassword

连接非3306默认端口

mysql -h目标IP地址 -u用户名 -p密码 -P端口
mysql -h10.211.55.9 -uroot -ppassword -P 33060

导出 SQL 文件

# 导出所有数据库
mysqldump -uroot -proot --all-databases >/tmp/all.sql

# 导出 db1、db2 两个数据库的所有数据
mysqldump -uroot -proot --databases db1 db2 >/tmp/user.sql

# 导出 db1 中的 a1、a2 表
mysqldump -uroot -proot --databases db1 --tables a1 a2  >/tmp/db1.sql

# 导出 db1 表 a1 中 id=1 的数据
mysqldump -uroot -proot --databases db1 --tables a1 --where='id=1'  >/tmp/a1.sql

# 只导出表结构不导出数据
mysqldump -uroot -proot --no-data --databases db1 >/tmp/db1.sql

导入 SQL 文件

mysql -uroot -proot < databases.sql

进程

查看进程

ps -a
ps -au
ps -aux
ps -ef

杀掉进程

kill -9 pid号

杀掉端口进程

# 手工查找 3306 的 PID
netstat -nlp | grep :3306

# 读取 3306 进程的 PID 号
netstat -nlp | grep :3306 | awk '{print $7}' | awk -F"/" '{ print $1 }'

# 杀掉 PID
kill -9 PID

也可以使用下面 1 条命令解决:

kill [']netstat -nlp | grep :3306 | awk '{print $7}' | awk -F"/" '{ print $1 }'[']

Docker

安装

CentOS 7

以下所有操作都是以 root 用户执行

# 更新到最新 yum 包
yum update -y

# 安装Docker以及Docker-compose
yum install -y yum-utils device-mapper-persistent-data lvm2 \
&& yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo \
&& yum install  -y docker-ce docker-ce-cli containerd.io \
&& systemctl start docker \
&& yum -y install epel-release \
&& yum -y install python-pip \
&& pip install --upgrade pip \
&& pip install docker-compose


# 查看Docker版本
$ docker version
Client: Docker Engine - Community
 Version:           19.03.5
 API version:       1.40
 Go version:        go1.12.12
 Git commit:        633a0ea
 Built:             Wed Nov 13 07:25:41 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.5
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.12
  Git commit:       633a0ea
  Built:            Wed Nov 13 07:24:18 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

# 查看 Docker-compose 版本
$ docker-compose version
docker-compose version 1.25.4, build unknown
docker-py version: 4.1.0
CPython version: 3.6.8
OpenSSL version: OpenSSL 1.0.2k-fips  26 Jan 201

Ubuntu

使用官方自动安装脚本,美滋滋:

sudo apt instal curl

# 自动安装脚本
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

# 将当前用户添加到 docker 用户组里
sudo usermod -aG docker `whoami`

# 重启生效
reboot

# 检验版本输出是否正常
$ docker version
Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:45:44 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.12
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       48a66213fe
  Built:            Mon Jun 22 15:44:15 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

Docker 安装完成后那么开始准备安装 Docker Compose 了,首先去官网查看最新的稳定版为多少:

https://github.com/docker/compose/releases

然后手动下载最新版本 赋予执行权限即可:

# 下载我写这篇文章的最新版本 1.27.2
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 赋予执行权限
sudo chmod +x /usr/local/bin/docker-compose

# 查看 Docker-compose 版本
$ docker-compose version
docker-compose version 1.27.2, build 18f557f9
docker-py version: 4.3.1
CPython version: 3.7.7
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

国内 Docker 加速还是很有必要配置一下的,编辑一下配置文件:

sudo vim /etc/docker/daemon.json
{
  "registry-mirrors": ["https://xxxxxxxx.mirror.aliyuncs.com"]
}

内容如下,修改完后重启一下 Docker 服务:

# 配置完成重启 Docker 服务 让配置生效
sudo systemctl daemon-reload
sudo systemctl restart docker

入门使用

Docker温故知新

压缩与解压

压缩

# tar
tar zcvf FileName.tar DirName

# tar.bz2
tar jcvf x.tar xxxx

# gz
gzip FileName

# Z
compress FileName

# tar.Z
tar Zcvf FileName.tar.Z DirName

# zip
zip -r FileName.zip DirName

# war 将 shell.jsp 压缩为 shell.war
jar cvf shell.war shell.jsp

解压

# tar
tar zxvf FileName.tar

# tar.bz2
bzip2 -d FileName.bz2
bunzip2 FileName.bz2

# gz
gzip -d FileName.gz
gunzip FileName.gz

# Z
uncompress FileName.Z

# tar.Z
tar Zxvf FileName.tar.Z

# zip
unzip FileName.zip

常见案例

CentOS 7 升级 OpenSSH

CentOS 自带的 SSH 版本为 OpenSSH 7.4 可以升级到 8 点几的版本。

下载 SSH 包

OpenSSH 软件包:https://ftp.riken.jp/pub/OpenBSD/OpenSSH/portable/

wget https://ftp.riken.jp/pub/OpenBSD/OpenSSH/portable/openssh-8.3p1.tar.gz

编译安装

解压到 /usr/local/src/目录下

tar -zxvf openssh-8.3p1.tar.gz -C /usr/local/src

然后进入到 OpenSSH 解压后的目录下 编译安装:

# 进入到目录
cd /usr/local/src/openssh-8.3p1

# 编译安装
./configure --prefix=/usr/local/openssh --with-ssl-dir=/usr/local/ssl --without-openssl-header-check
make && make install

修改配置

# 添加 SSH 配置
echo "PermitRootLogin yes" >> /usr/local/openssh/etc/sshd_config
echo "PubkeyAuthentication yes" >> /usr/local/openssh/etc/sshd_config
echo "PasswordAuthentication yes" >> /usr/local/openssh/etc/sshd_config                                                                

# 拷贝配置文件
cp /usr/local/src/openssh-8.3p1/contrib/redhat/sshd.init /etc/init.d/sshd

# SSH 服务
chkconfig --add sshd

mv /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
cp /usr/local/openssh/etc/sshd_config /etc/ssh/sshd_config

# 提示覆盖 覆盖掉
cp /usr/local/openssh/sbin/* /usr/sbin/sshd -f

# 这一步提示覆盖 都覆盖掉
cp /usr/local/openssh/bin/* /usr/bin/

重启验证

# 重启服务器
reboot

# 注意是大写V
ssh -V


文章作者: 国光
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 国光 !
自从点了 👇 广告,腰也不酸了,腿也不疼了
点一下 👆 玩一年 装备不花一分钱!
 上一篇
Hashcat学习记录 Hashcat学习记录
Hashcat是一款密码爆破神器,信息安全必备工具之一,特此写篇文章记录总结之,以备不时之需。 简介Hashcat是世界上最快的密码破解程序,是一个支持多平台、多算法的开源的分布式工具。 官方:https://hashcat.net/
2019-10-31
下一篇 
Kali Linux配置记录 Kali Linux配置记录
Kali Linux白帽子必备操作系统,有时候重新安装又得折腾下,特此这里总结一下,减少一些不必要的坑。 更新源vim /etc/apt/sources.list 官方源记住这个官方源就好了,不要被网上辣鸡文章误导,官方源会自动选择最
2019-10-30
  目录