Linux常用命令
解压文件
unzip解压
[root@localhost ~]#
unzip test.zip -d test
解压当前目录下的test.zip文件到当前目录test文件夹中,会自动创建test文件夹。
可以解压.war和.zip文件。
unzip命令安装
有时候会提示没有安装unzip命令,yum安装需要linux可以联网。
[root@localhost ~]#
yum install -y unzip zip
查看磁盘空间使用情况
[root@localhost ~]#
df -h
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 30G 21G 59% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 4.0K 7.8G 1% /dev/shm
tmpfs 7.8G 9.7M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/sda1 1014M 169M 845M 17% /boot
/dev/mapper/centos-home 142G 7.2G 134G 6% /home
tmpfs 1.6G 0 1.6G 0% /run/user/2
tmpfs 1.6G 12K 1.6G 1% /run/user/42
tmpfs 1.6G 0 1.6G 0% /run/user/0
统计每个文件的大小
统计当前目录下每个文件(文件夹)的大小
[root@localhost ~]#
du -sh * | sort -n
[root@localhost ~]# cd /
[root@localhost /]# du -sh * | sort -n
0 bin
0 lib
0 lib64
0 media
0 mnt
0 opt
0 proc
0 sbin
0 srv
0 sys
1.2G var
4.0K dev
7.0G home
9.7M run
14M logs
28G usr
37M etc
37M Users
127M root
137M boot
332K tmp
查看最大连接数
[root@localhost ~]#
ulimit -n
修改最大连接数
[root@localhost ~]#
vi /etc/security/limits.conf
按下I后,添加以下内容
* soft nofile 65535
* hard nofile 65535
#End of file
按下Esc后,然后输入 :wq,保存并退出vi编辑。
然后需要断开当前会话,重新打开一个会话,这时重新查看最大连接数就会显示最新的。
服务器时间
查看服务器时间
[root@localhost ~]#
date
2019年 10月 22日 星期二 10:13:38 CST
修改服务器时间
[root@localhost ~]#
date -s "20191008 10:49:00"
同步服务器时间到硬件时钟
在修改服务器时间后,需要把时间同步到硬件时钟,如果不同步,下次重启Linux时间会变成硬件的时钟。
[root@localhost ~]#
hwclock -w
设置服务器时间与网络定时同步
中国ntp服务器:cn.pool.ntp.org
- 配置开机启动校验
[root@localhost ~]#
vim /etc/rc.d/rc.local
/usr/sbin/ntpdate -u cn.pool.ntp.org> /dev/null 2>&1; /sbin/hwclock -w
- 配置定时任务
每天18:00同步
[root@localhost ~]#
crontab -e
00 18 * * * /usr/sbin/ntpdate -u cn.pool.ntp.org > /dev/null 2>&1; /sbin/hwclock -w
文件分配权限
分配给当前目录下的clean.sh文件最大权限777
[root@localhost ~]#
chmod 777 clean.sh
添加定时任务
[root@localhost ~]#
crontab -e
按下I后,添加内容:
0 1 * * * /home/tomcat/logs/clean.sh
每天凌晨1点执行clean.sh脚本。
0 12 1 * * cat /dev/null > /usr/local/nginx/log/access.log
0 12 1 * * cat /dev/null > /usr/local/nginx/log/error.log
每月1日中午12点,把access.log、error.log的内容清空
minute | hour | day-of-month | month | day-of-week | full-path-to-shell-script} |
---|---|---|---|---|---|
区间为 0 – 59 | 区间为0 – 23 | 区间为0 – 31 | 区间为1 – 12. 1 是1月. 12是12月 | 区间为0 – 7. 周日可以是0或7 | 内容 |
按下Esc后,然后输入 :wq,保存并退出编辑。
然后使定时任务立即生效
[root@localhost ~]#
service crond reload
定时任务中添加定时执行的.sh脚本需要拥有可执行的权限。
查看网卡IP信息
[root@localhost ~]#
ifconfig -a
重启网卡
当开放端口后需要重启网卡后端口才会生效。
[root@localhost ~]#
service network restart
删除文件
删除当前目录下所有内容,rf表示不提示是否删除,不需要进行二次确认,需要谨慎操作
[root@localhost ~]#
rm -rf ./*
telnet相关
telnet命令
telnet IP 端口
[root@localhost ~]#
telnet 192.168.1.100 8888
Trying 192.168.1.100…
Connected to 192.168.1.100.
Escape character is ‘^]’.
以上就是端口通了的返回结果。
如果提示:
[root@localhost ~]#
telnet 192.168.1.100 8888
bash: telnet: 未找到命令...
需要安装telnet命令。
安装telnet命令
- telnet-server
先检查telnet-server是否安装了
[root@localhost ~]#
rpm -qa telnet-server
[root@localhost ~]#
没有安装需要先安装
[root@localhost ~]#
yum -y install telnet-server
- telnet
先检查telnet是否安装了
[root@localhost ~]#
rpm -qa telnet
[root@localhost ~]#
没有安装需要先安装
[root@localhost ~]#
yum -y install telnet
MySQL相关
安装mysql数据库出现问题
: scripts/mysql_install_db: /usr/bin/perl: 坏的解释器: 没有那个文件或目录
Copy my.cnf to /etc/my.cnf
cp: 无法获取"my.cnf" 的文件状态(stat): 没有那个文件或目录
Modify the MySQL configuration file
sed:无法读取 /etc/my.cnf:没有那个文件或目录
[root@localhost ~]#
yum install -y perl-Module-Install.noarch
mysqldump命令找不到
这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下,会找不到命令,我们需要做的就是映射一个链接到/usr/bin目录下,相当于建立一个链接文件。
首先要知道mysqldump命令的完整路径
比如mysqldump的路径是:/usr/local/mysql/bin/mysqldump,我们则可以这样执行命令:
[root@localhost ~]#
ln -s /usr/local/mysql/bin/mysqldump /usr/bin
mysql 添加慢查询日志输出
mysql版本不同设置不同,以下是数据库版本>=5.6的设置方法
修改my.cnf文件,添加以下2行
general_log_file = /var/log/mysql/mysql.log
general_log = 3
general_log_file是文件输出的文件位置,需要提前把mysql.log文件在对应目录创建好,并授权允许数据操作这个文件
[root@localhost mysql]#
chown mysql mysql.log
general_log是查询用时,单位秒(s)
设置完成后重启mysql。