Linux 学习笔记

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


更新源

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

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

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
清华源

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 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

14.04

echo deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main restricted universe multiverse >> /etc/apt/sources.list echo deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main restricted universe multiverse >> /etc/apt/sources.list echo deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse >> /etc/apt/sources.list echo deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse >> /etc/apt/sources.list echo deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse >> /etc/apt/sources.list echo deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse >> /etc/apt/sources.list echo deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main restricted universe multiverse >> /etc/apt/sources.list echo deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security 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

手动编译

./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以为,其他配置方法和上面其他发行版系统一样,这里就不再赘述了。

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-clientBash

#手动解决依赖 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
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

效果如下: tp

将拷贝下来的私钥设置600权限
chmod 600 ~/Downloads/test_rsa
使用私钥登陆
ssh -i ~/Downloads/test_rsa root@YOUR IP

tp

ssh取消公私钥登陆

清空.ssh文件夹
rm -rf /root/.ssh/*
允许密码登陆
echo PasswordAuthentication yes >> /etc/ssh/sshd_config
重启SSH服务
/etc/init.d/ssh restart
scp拷贝文件
scp 本地文件 用户名@服务器地址:要拷贝的路径

tp

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

JDK

因为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

验证是否安装成功

java -version

grep

搜索文件内容
#搜索/home/sqlsec/Desktop/BBS/中的所有php后缀中的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/

用户

修改用户密码
#修改当前用户密码 passwd #修改指定用户密码 passwd 用户名 #修改root密码 sudo passwd
adduser 新建用户
adduser test

useradd 新建用户
#创建test用户并自动生成主目录且指定shell为 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

SQL允许root远程登陆

SQL
#允许root外部访问连接 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '这里是root密码' WITH GRANT OPTION; #刷新权限 FLUSH PRIVILEGES;
连接非3306默认端口
mysql -h目标IP地址 -u用户名 -p密码 -P端口 mysql -h10.211.55.9 -uroot -ppassword -P 33060

系统进程

查看进程
ps -a ps -au ps -aux ps -ef
杀掉进程
kill -9 pid号

END

文章作者: 国光 文章链接: https://www.sqlsec.com/2019/10/docker2.html