Linux常用命令

安装deb

1
sudo dpkg -i baidunetdisk_linux_2.0.2.deb

安全检测

查看恶意IP登录

1
lastb root | awk '{print $3}' | sort | uniq -c | sort -nr| more

查看是否有异常的系统用户

1
cat /etc/passwd

检查是否有新用户尤其是UID和GID为0的用户

1
awk -F":" '{if($3 == 0){print $1}}' /etc/passwd

检查是否存在空口令账户

1
awk -F: '{if(length($2)==0) {print $1}}' /etc/passwd

检查系统异常进程

使用ps -ef命令查看进程(尤其注意UID为root的进程

1
ps -ef

查看该进程所打开的端口和文件

1
2
netstat -ntlup
lsof -p 22

检查隐藏进程

1
ps -ef | awk '{print $2}'| sort -n | uniq >1; ls /proc |sort -n|uniq >2;diff -y -W 40 1 2

安装jdk

1
2
3
tar -zxvf jdk-8u171-linux-x64.tar.gz
sudo mv jdk1.8.0-171 /usr/local/jdk1.8
sudo vim /etc/profile

在文件末尾加入

1
2
3
4
export JAVA_HOME=/usr/local/jdk1.8
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=.:${JAVA_HOME}/bin:$PATH

更新生效

1
source /etc/profile

检查是否生效

1
2
java -version
javac

ubuntu安装mysql5.7

安装mysql

1
sudo apt-get install mysql-server

检查状态

1
sudo netstat -tap | grep mysql

设置root密码

1
2
3
4
5
6
7
8
9
sudo su
mysql
select user,plugin from mysql.user;
update mysql.user set authentication_string=PASSWORD('password'), plugin='mysql_native_password' where user='root';
flush privileges;
exit

sudo /etc/init.d/mysql restart
mysql -uroot -p

配置mysql远程登录
修改配置文件,bind-address = 0.0.0.0

1
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf

保存退出,然后进入mysql服务,执行授权命令

1
2
3
4
5
mysql -u root -p
grant all on *.* to root@'%' identified by 'guqiao*2011_' with grant option;
flush privileges;
exit
sudo /etc/init.d/mysql restart

卸载mysql

安装nginx

1
sudo yum install -y nginx

设置开机启动

1
sudo systemctl enable nginx

关闭开机启动

1
sudo systemctl disable nginx

启动nginx

1
sudo systemctl start nginx

停止nginx

1
sudo systemctl stop nginx

查看nginx启动状态

1
sudo systemctl status nginx

重新加载nginx配置

1
sudo systemctl reload nginx

nginx默认的静态HTML文件位置

1
/usr/share/nginx/html

如果要使用NGINX作为默认的web服务器,则可以使用/var/www文件夹来存储不同的网站。但是如果将请求代理到Apache,则只需修改NGINX配置文件,就可以使用Apache网站文件的/var/www路径

安装redis

1
sudo apt-get install redis-server

查看tcp连接

1
netstat -ap | grep 6379

启动
服务:redis-server
客户端:redis-cli
修改配置文件允许局域网内的其他电脑连接redis
文件位置:/etc/redis/redis.conf
详细的使用:https://juejin.im/entry/5bac46dd5188255c960c3d27

开启SSH服务

查看ssh是否安装

1
sudo ps -e | grep ssh

安装ssh

1
2
3
4
5
sudo apt-get install openssh-server
sudo /etc/init.d/ssh start
sudo service ssh status
sudo service ssh stop
sudo service ssh restart

更改sshd_config配置

1
vim /etc/ssh/sshd_config 
1
PermitRootLogin yes
1
ssh root@192.168.1.115

配置说明

使用xshell进行文件上传和下载

1
apt-get install lrzsz

rz 上传文件

z file 下载文件

附件

linux查看公网ip

1
curl ipinfo.io/json

查看系统资源占用

查看ubuntu的资源占用

1
top

查看内存使用情况

1
free

更新系统运行时间,负载信息

1
uptime 

显示磁盘活动情况

1
vmstat 

统计CPU的使用情况及tty设备、硬盘和CD-ROM的I/O量

1
iostat

查看磁盘使用情况

1
2
df -h 
du -sh

查看mysql信息

1
ps -aux | grep mysql

安装htop工具查看

1
sudo apt-get install htop

安装seafile

https://cloud.seafile.com/published/seafile-manual-cn/deploy/using_mysql.md

安装ffmpeg

1
2
3
sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next
sudo apt-get update
sudo apt-get install ffmpeg

查看访问数

1
cat /var/log/auth.log  | grep "sshd" | awk '/Failed/{print $(NF-3)}' | sort | uniq -c 

centos安装mysql

https://www.centos.bz/2019/02/centos7-%E5%AE%89%E8%A3%85-mysql/

卸载腾讯云自带配置

1
2
3
4
5
6
/usr/local/qcloud/stargate/admin/uninstall.sh
/usr/local/qcloud/YunJing/uninst.sh
/usr/local/qcloud/monitor/barad/admin/uninstall.sh
sudo rm -rf /usr/local/qcloud/
sudo rm -rf /usr/local/sa/
sudo rm -rf /usr/local/agenttools

安装监控(主机面板)

宝塔linux:
https://www.bt.cn/bbs/thread-19376-1-1.html
Ajenti:
Feathur:
ISPConfig:
VestaCP:
Virtualmin:
ZPanel:
WDCP:
AMH:
WDCP:
LuManager:
VPSMate:
Oneinstack:
AppNode:
cpanel:

基本命令

1
2
3
4
5
ls
ls -al
cd
mkdir aaa
mkdir -p aaa/bbb/ccc

删除文件

1
rm -r  aaa

删除文件夹

1
rm -rf  aaa

修改文件夹名

1
mv aaa bbb

创建新文件

1
2
3
touch name.txt
echo "content" > a.txt
cat a.txt

权限管理

本组删除读写权限

1
chmod g-rw  name.txt

其他组删除读写权限

1
chmod o-rw name.txt 

个人添加可执行权限

1
chmod u+x name.txt

如果要将一个文件夹的所有内容权限统一修改,则可以-R参数

1
2
chmod -R 770 aaa
chown xxx:xxx aaa/

常用操作系统命令

1
2
useradd  username
passwd username

切换身份

1
su user 

执行这条指令用root身份,执行完成后用原来的身份

1
sudo useradd zhangsan

查看主机名

1
hostname

修改主机名(重启后无效)

1
hostname  hadoop

修改主机名(重启后有效)

1
vi /etc/sysconfig/network

修改ip(重启后无效)

1
ifconfig eth0 192.168.12.22

修改ip(重启后生效)

1
vi /etc/sysconfig/network-scripts/ifcfg-eth0

查看系统命令

1
2
uname -a
uname -r

查看日期

1
date
1
2
3
mount
umount
mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom

查看文件大小

1
2
du -sh
du -ah

查看分区

1
df -h

ssh命令

1
ssh root@192.168.2.11

关机

1
2
halt
shutdown -h now /init 0

重启

1
2
reboot
shutdown -r now /reboot

远程拷贝

1
scp install.log  root@192.168.1.2:/home

公钥 私钥生成

1
ssh-keygen

复制公钥到远程主机

1
ssh-copy-id 192.168.33.111

一次性将文件内容全部输出(控制台)

1
cat somefile 

可以翻页查看,下翻一页(空格),上翻一页(b),退出(q)

1
more somefile

可以翻页查看,下翻一页(空格) 上翻一页(b) 上翻一行(↑),下翻一行(↓) 可以搜索关键字(/keyword)

1
less somefile  

查看文件尾部的10行

1
tail -10 install.log

小f跟踪文件的唯一inode号,就算文件名改后,还是跟踪原来这个inode表示的文件

1
tail  -f install.log

大F按照文件名来跟踪

1
tail -F install.log

查看文件头部的10行

1
head -10 install.log

后台服务管理

查看指定服务的状态

1
service network status

停止指定服务

1
service network stop

启动指定服务

1
service network start

重启指定服务

1
service network restart

查看系统中所有的后台服务

1
service --status-all

设置后台服务的自启配置

chkconfig 查看所有服务器自启配置

chkconfig iptables off 关掉指定服务的自动启动

chkconfig iptables on 开启指定服务的自动启动

上传安装包到服务器

可以使用图形化工具,如:filezile
可以使用sftp工具, securecrt alt+p 调出后,用put命令上传
上传(如果不cd指定目录,则上传到当前用户的主目录)

1
2
sftp> cd /home/
sftp> put c:\xxx.xxx

下载

1
sftp> get xxxxx.xxxx

压缩和解压缩

1
gzip -d install.log

将1.xxx 2.xxx 343/文件夹打包到xxx.tar文件中

1
tar -cvf xxx.tar 1.xxx 2.xxx  343/
1
tar -xvf xxx.tar 

一次性压缩

1
tar -zcvf my.tar.gz  /somefile 

一次性解压缩

1
tar -zxvf my.tar.gz 

linux jdk环境变量设置

1
vi /etc/profile

大G 到最后一行

o 插入一行

1
2
3
export JAVA_HOME=/root/apps/jdkhome
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile

查询外网IP的办法

Curl纯文本格式输出

1
2
3
4
5
6
7
curl icanhazip.com
curl ifconfig.me
curl curlmyip.com
curl ip.appspot.com
curl ipinfo.io/ip
curl ipecho.net/plain
curl www.trackip.net/i

Curl JSON格式输出

1
2
3
curl ipinfo.io/json
curl ifconfig.me/all.json
curl www.trackip.net/ip?json

Curl XML格式输出

1
curl ifconfig.me/all.xml

Curl 得到所有IP细节 (挖掘机)

1
curl ifconfig.me/all

使用 DYDNS (当你使用 DYDNS 服务时有用)

1
2
curl -s 'http://checkip.dyndns.org' | sed 's/.*Current IP Address: \([0-9\.]*\).*/\1/g'
curl -s http://checkip.dyndns.org/ | grep -o "[[:digit:].]\+"

使用 Wget 代替 Curl

1
2
wget http://ipecho.net/plain -O - -q ; echo
wget http://observebox.com/ip -O - -q ; echo

使用 host 和 dig 命令

1
2
host -t a dartsclink.com | sed 's/.*has address //'
dig +short myip.opendns.com @resolver1.opendns.com

bash 脚本示例

1
2
3
#!/bin/bash
PUBLIC_IP=`wget http://ipecho.net/plain -O - -q ; echo`
echo $PUBLIC_IP

安装ftp

1
yum -y install vsftpd

CentOS上用python调用FFMepg将m3u8合成MP4后显示异常的问题

(1)判断是否有中文包

1
locale -a

(2)如果没有中文包,则使用如下命令下载

1
yum install langpacks-zh_CN.noarch

multipass虚拟机安装

官网:https://multipass.run/

/proc/kcore 128T内存爆满问题处理

1
2
3
4
5
6
7
8
# 列出磁盘使用率
df -h
# 列出删除列表
lsof | grep delete
# 删除任务
kill XXXX
kill YYYY
....

查询大文件

1
find / -type f -size +1G

#(Centos7.9)端口号管理

开放端口

1
2
3
4
5
6
# 开放5672端口
firewall-cmd --zone=public --add-port=5672/tcp --permanent
# 关闭5672端口
firewall-cmd --zone=public --remove-port=5672/tcp --permanent
# 配置立即生效
firewall-cmd --reload

查看防火墙所有开放的端口

1
firewall-cmd --zone=public --list-ports

关闭/开启防火墙

1
2
systemctl stop firewalld.service
systemctl start firewalld.service

查看防火墙状态

1
firewall-cmd --state

查看监听的端口

1
netstat -lnpt

检查端口被哪个进程占用

1
netstat -lnpt | grep 18788
Author

jiangyao

Posted on

2021-06-12

Updated on

2022-11-14

Licensed under