一、常用命令
1、查看操作系统
> lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.1 LTS
Release: 16.04
Codename: xenial
centos:
[root@localhost ~]# cat /etc/issue
CentOS release 5.5 (Final)
Kernel r on an m
2、下载,安装、解压
> apt-get install build-essential
> wget ftp://ftp.openldap.org/pub/OpenLDAP/openldap-release/openldap-2.4.31.tgz
> tar -zxf openldap-2.4.31.tgz
3、修改root密码
切换root用户
su root
Ubuntu的
默认root密码是随机的,即每次开机都有一个新的root密码。我们可以在终端输入命令 sudo passwd
,然后输入当前用户的密码:
vagrant@homestead:/usr/local/src/db-5.1.29.NC/build_unix$ sudo passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
vagrant@homestead:/usr/local/src/db-5.1.29.NC/build_unix$
4、删除文件夹及文件命令
// 删除文件夹实例:
rm -rf /var/log/httpd/access
将会删除/var/log/httpd/access目录以及其下所有文件、文件夹
删除文件使用实例:
rm -f /var/log/httpd/access.log
将会强制删除/var/log/httpd/access.log这个文件
5、查看软件文件安装路径
whereis
在linux中文件与软件一般都是安装在到/usr/share
和/usr/local
中了,如果我们需要查看软件安装路径linux为我们提供了查看命令,whereis
就可以帮我查找文件安装路径在哪里了。
比如你想查找eclipse文件,那么就:
# whereis eclipse
会显示:
eclipse: /usr/bin/eclipse /usr/lib/eclipse /usr/share/eclipse
6、查询运行文件所在路径
1.如果你只要查询文件的运行文件所在地址,直接用下面的命令就可以了(还是以Oracle为例):
which oracle
结果会显示:
/usr/bin/oracle
以上只适合安装路径加入PATH
里
如果是源码安装的话,你可以使用-prefix
指定安装路径,这样,你可以很轻松知道软件的安装路径;
2.如果是rpm
包安装的话,可以使用rpm -qal
查询一下
rpm -qf /路径/程序名
可以查到一个程序属于哪个rpm包,然后rpm -ql
那个包可以看到那个包的所有文件和路径等等。还可以解开成一个cpio文件等等之类的,仔细研究一下rpm的参数吧。
完全可以做到任何你想做的。
源码的安装一般由3个步骤组成:配置(configure)、编译(make)、安装(make install)
,具体的安装方法一般作者都会给出文档,这里主要讨论配置(configure)。
Configure是一个可执行脚本,它有很多选项,使用命令./configure –help
输出详细的选项列表,如下:
-bash-3.00# ./configure --help
Usage: configure [options] [host]
Options: [defaults in brackets after descriptions]
Configuration:
--cache-file=FILE cache test results in FILE
--help print this message
--no-create do not create output files
--quiet, --silent do not print `checking...' messages
--version print the version of autoconf that created configure
Directory and file names:
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local]
--exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
[same as prefix]
--bindir=DIR user executables in DIR [EPREFIX/bin]
7、查看服务器本机IP
Ubuntu,Unix
ifconfig
CentOS:
ip addr
windows
ipconfig
8.find 命令汇总
find -name april* 在当前目录下查找以april开始的文件
find -name april* fprint file 在当前目录下查找以april开始的文件,并把结果输出到file中
find -name ap* -o -name may* 查找以ap或may开头的文件
find /mnt -name tom.txt -ftype vfat 在/mnt下查找名称为tom.txt且文件系统类型为vfat的文件
find /mnt -name t.txt ! -ftype vfat 在/mnt下查找名称为tom.txt且文件系统类型不为vfat的文件
find /tmp -name wa* -type l 在/tmp下查找名为wa开头且类型为符号链接的文件
find /home -mtime -2 在/home下查最近两天内改动过的文件
find /home -atime -1 查1天之内被存取过的文件
find /home -mmin +60 在/home下查60分钟前改动过的文件
find /home -amin +30 查最近30分钟前被存取过的文件
find /home -newer tmp.txt 在/home下查更新时间比tmp.txt近的文件或目录
find /home -anewer tmp.txt 在/home下查存取时间比tmp.txt近的文件或目录
find /home -used -2 列出文件或目录被改动过之后,在2日内被存取过的文件或目录
find /home -user cnscn 列出/home目录内属于用户cnscn的文件或目录
find /home -uid +501 列出/home目录内用户的识别码大于501的文件或目录
find /home -group cnscn 列出/home内组为cnscn的文件或目录
find /home -gid 501 列出/home内组id为501的文件或目录
find /home -nouser 列出/home内不属于本地用户的文件或目录
find /home -nogroup 列出/home内不属于本地组的文件或目录
find /home -name tmp.txt -maxdepth 4 列出/home内的tmp.txt 查时深度最多为3层
find /home -name tmp.txt -mindepth 3 从第2层开始查
find /home -empty 查找大小为0的文件或空目录
find /home -size +512k 查大于512k的文件
find /home -size -512k 查小于512k的文件
find /home -links +2 查硬连接数大于2的文件或目录
find /home -perm 0700 查权限为700的文件或目录
find /tmp -name tmp.txt -exec cat {} /;
find /tmp -name tmp.txt -ok rm {} /;
find / -amin -10 # 查找在系统中最后10分钟访问的文件
find / -atime -2 # 查找在系统中最后48小时访问的文件
find / -empty # 查找在系统中为空的文件或者文件夹
find / -group cat # 查找在系统中属于 groupcat的文件
find / -mmin -5 # 查找在系统中最后5分钟里修改过的文件
find / -mtime -1 #查找在系统中最后24小时里修改过的文件
find / -nouser #查找在系统中属于作废用户的文件
find / -user fred #查找在系统中属于FRED这个用户的文件
查当前目录下的所有普通文件
--------------------------------------------------------------------------------
# find . -type f -exec ls -l {} /;
-rw-r--r-- 1 root root 34928 2003-02-25 ./conf/httpd.conf
-rw-r--r-- 1 root root 12959 2003-02-25 ./conf/magic
-rw-r--r-- 1 root root 180 2003-02-25 ./conf.d/README
查当前目录下的所有普通文件,并在- e x e c选项中使用ls -l命令将它们列出
=================================================
在/ l o g s目录中查找更改时间在5日以前的文件并删除它们:
$ find logs -type f -mtime +5 -exec -ok rm {} /;
=================================================
查询当天修改过的文件
[root@book class]# find ./ -mtime -1 -type f -exec ls -l {} /;
=================================================
查询文件并询问是否要显示
[root@book class]# find ./ -mtime -1 -type f -ok ls -l {} /;
< ls ... ./classDB.inc.php > ? y
-rw-r--r-- 1 cnscn cnscn 13709 1月 12 12:22 ./classDB.inc.php
[root@book class]# find ./ -mtime -1 -type f -ok ls -l {} /;
< ls ... ./classDB.inc.php > ? n
[root@book class]#
=================================================
查询并交给awk去处理
[root@book class]# who | awk '{print $1"/t"$2}'
cnscn pts/0
=================================================
awk---grep---sed
[root@book class]# df -k | awk '{print $1}' | grep -v 'none' | sed s"///dev////g"
文件系统
sda2
sda1
[root@book class]# df -k | awk '{print $1}' | grep -v 'none'
文件系统
/dev/sda2
/dev/sda1
1)在/tmp中查找所有的*.h,并在这些文件中查找“SYSCALL_VECTOR",最后打印出所有包含"SYSCALL_VECTOR"的文件名
A) find /tmp -name "*.h" | xargs -n50 grep SYSCALL_VECTOR
B) grep SYSCALL_VECTOR /tmp/*.h | cut -d':' -f1| uniq > filename
C) find /tmp -name "*.h" -exec grep "SYSCALL_VECTOR" {} /; -print
2)find / -name filename -exec rm -rf {} /;
find / -name filename -ok rm -rf {} /;
3)比如要查找磁盘中大于3M的文件:
find . -size +3000k -exec ls -ld {} ;
9、yum命令
yum
(Yellowdog Updater Modified)是一个集与查找,安装,更新和删除程序
的Linux软件。它运行在RPM包兼容的Linux发行版本上,如:RedHat, Fedora, SUSE, CentOS, Mandriva。
今天我在用CentOS 5.5的yum命令安装的时候出现如下错误:
[root@malu2 ~]# yum install vim
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.hosting90.cz
* extras: mirror.hosting90.cz
* updates: mirror.hosting90.cz
Setting up Install Process
No package vim available.
Nothing to do
对于此错误,我一般习惯性先去更新一下yum仓库:
#yum -y update
再次运行安装,发现错误依旧,看来这种猜包名的方法不适用;所以接下来可以通过yum的查找参数来列出vim相关的包名:
[root@malu2 yum.repos.d]# yum search vim
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.hosting90.cz
* extras: mirror.hosting90.cz
* updates: mirror.hosting90.cz
===================== Matched: vim ===================
vim-X11.x86_64 : The VIM version of the vi editor for the X Window System.
vim-common.x86_64 : The common files needed by any version of the VIM editor.
vim-enhanced.x86_64 : A version of the VIM editor which includes recent
: enhancements.
vim-minimal.x86_64 : A minimal version of the VIM editor.
通过查看,我们可以看到,yum列出了详细的vim相关的包名,这样就可以顺利的安装vim了:
#yum -y install vim-enhanced.x86_64
以上命令就能执行成功了。当然不仅仅vim安装,其他程序比如MySQL我们也同样只要先进行yum查找,再通过匹配详细包名来进行安装,就不会出现上述错误了
3)如果还是不行,应该是 EPEL未被启动,且指示的 rpm包不可用
具体原因:使用yum搜索某些rpm包,找不到包是因为CentOS是RedHat企业版编译过来的,去掉了所有关于版权问题的东西。安装EPEL后可以很好的解决这个问题。EPEL(Extra Packages for Enterprise Linux )即企业版Linux的扩展包,提供了很多可共Centos使用的组件,安装完这个以后基本常用的rpm都可以找到。
下载与安装EPEL的rpm文件包
1.下载:
// 根据不同版本下载,我的是centos 6.8,所以,下载第一个链接epel-release-6-8.noarch.rpm
# wget http://mirrors.ustc.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm
# wget http://mirrors.ustc.edu.cn/fedora/epel/5/i386/epel-release-5-4.noarch.rpm
2.安装:
#rpm -ivh epel-release-5-4.noarch.rpm
安装完毕后,可以使用yum安装软件。
注意:
- epel的版本要与centos的版本相对应,否则安装失败,即centos5版本要对应epel5版本。
- rpm安装完后,rpm -ql epel*,确提示未安装,此时要导入rpm的签名信息
#rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 (root身份执行)
10. 各linux版本重启apache命令
Slackware Linux命令:
/etc/rc.d/rc.httpd restart
ubuntu、Debian 系统命令:
/etc/init.d/apache2 restart
Fedora 、Redhat、CentOS系统重启Apache命令:
/etc/init.d/httpd restart
或
service httpd restart(CentOS 成功)
11、Linux下好用的编辑器gedit
以前我都是用vi/vim
编辑器编辑文件,但是这样的环境很难操作,不能粘贴和复制,开发效率非常慢,还需要许多的命令,很不友好,忽然发现geidt
这款编辑器非常棒,我们可以使用这款编辑器就像是在windows下使用txt编辑器一样方便.
安装:
> yum install gedit
使用,其实和vim的用法一样:
> gedit php_log.log
12、配置Apache访问路径
// 修改访问路径virtual-hosts.conf
[root@baidu extra]# pwd
/usr/local/apache/conf/extra
编辑virtual-hosts.conf
配置文件
<VirtualHost *:80>
DocumentRoot "/usr/local/apache/htdocs"
ServerName sso.com
ErrorLog "sso.com-error_log"
CustomLog "sso.com-access_log" common
<Directory "/usr/local/apache/htdocs">
Options Indexes FollowSymLinks
AllowOverride All
Order deny,allow
Allow From all
</Directory>
</VirtualHost>
13、linux下重命名文件或文件夹
linux下重命名文件或文件夹的命令 mv
既可以重命名,又可以移动文件或文件夹.
例子:将目录A重命名为B
mv A B
例子:将/a目录移动到/b下,并重命名为c
mv /a /b/c
其实在文本模式中要重命名文件或目录的话也是很简单的,我们只需要使用mv命令就可以了,比如说我们要将一个名为abc的文件重命名为1234就可以这样来写:mv abc 1234
,但是要注意的是,如果当前目录下也有个1234的文件的话,我们的这个文件是会将它覆盖的
14、修改Linux系统时间
先修改时区,然后再调整具体时间
[root@camel Asia]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp: overwrite ‘/etc/localtime’? y
[root@camel Asia]# date
Thu Jul 27 18:50:49 CST 2017
[root@camel Asia]# date
Thu Jul 27 18:51:25 CST 2017
[root@camel Asia]#
date命令不仅可以显示系统当前时间,还可以用它来修改系统时间,下面简单的介绍下,方便需要的朋友
命令格式为:
date -s 时间字符串
例如只修改系统的日期,不修改时间(时分秒)
date -s 2012-08-02
或只修改时间不修改日期
date -s 10:08:00
当然也可以同时修改日期和时间
date -s "2012-05-18 04:53:00"
注意:由于日期和时间之间有空格,所以必须用引号引起来,否则命令会报错。
上述修改只是修改了linux的系统时间,CMOS中的时间可能还没有改变,所以为了保险,需要使用 clock -w 把当前系统时间写入到CMOS中。
系统时间和CMOS时间的关系。系统时间是由linux操作系统来维护的;CMOS时间是CMOS芯片保存的时间。系统启动时,操作系统将从CMOS读出时间记录为系统时间,同时操作系统也会自动每隔一段时间将系统时间写入CMOS中。如果使用date命令修改系统时间后马上重启电脑,操作系统还没有将系统时间同步到CMOS,这样开机后就还是没有修改前的时间了,所以为了保险起见,最还还是手动使用命令 clock 将系统时间同步到CMOS中。
15、shell并发执行
使用curl模拟并发请求网页
test.sh
#!/bin/bash
#betin time
begin=$(date +%s)
#批量处理
count=100
for(( i = 0; i < $count; i++ ))
do
{
/usr/bin/curl http://172.18.82.21/xampps/web/login_test/index.php
}&
done
#结束时间
end=$(date +%s)
spend=$(expr $end - $begin)
echo "花费时间为$spend秒"
执行脚本
// 执行脚本
> bash test.sh
16、Ubuntu apt-get安装包报错
Ubuntu Unable to parse package file [duplicate]
// 删除apt软件缓存:
sudo rm -r /var/lib/apt/lists/*
// 更新apt软件列表:
sudo apt-get update
17、安装LAMP 环境
在Ubuntu下装了一下php的环境,也就是装了一下MySQL、PHP、Apache.
#安装mysql
sudo apt-get install mysql-server
#这个完成后会有一个要求输入root用户密码的部分
sudo apt-get install mysql-client
#安装apache
sudo apt-get install apache2
#安装php
sudo apt-get install php5
#安装连接关系(直接都配置好了,windows下改这个改那个的情况完全不用担心)
sudo apt-get install libapache2-mod-auth-mysql
sudo apt-get install php5-mysql
然后就是检测成果了:
在浏览器输入localhost看到如下界面就说明Apache安装成功了
在终端输入
mysql -username –password
成功登陆就是mysql成功率。
检验php由于权限问题稍微麻烦一点,Apache的的默认安装路径为/var/www/
。我们需要到其下建立一个php文件,但是用户是没有权限的,首先我们要获得权限:
su root
chown username /var/www
然后写个php文件输出phpinfo
文件如下:
<?php
phpinfo();
?>
18、查看php进程
首先,用ps查看进程,方法如下:
$ ps -ef
或者:
$ ps -aux
此时如果我想杀了test.php的进程就在终端输入:
$ kill -s 9 8098
其中-s 9 制定了传递给进程的信号是9,即强制、尽快终止进程。各个终止信号及其作用见附录。
1827则是上面ps查到的test.php的PID。
简单吧,但有个问题,进程少了则无所谓,进程多了,就会觉得痛苦了,无论是ps -ef
还是ps -aux
,每次都要在一大串进程信息里面查找到要杀的进程,看的眼都花了。
进阶篇:
改进1:
把ps的查询结果通过管道给grep查找包含特定字符串的进程。管道符“|”用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入。
$ ps -ef | grep firefox
smx 1827 1 4 11:38 ? 00:27:33 /usr/lib/firefox-3.6.18/firefox-bin
smx 12029 1824 0 21:54 pts/0 00:00:00 grep --color=auto firefox
这次就清爽了。然后就是
$kill -s 9 1827
还是嫌打字多?
19.php判断脚本是否已经在执行
在定时脚本中,如果判断脚本已经在进程中执行,则不再执行
// 传入脚本路径,也可以传入脚本路径+参数
function check_is_run($command) {
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
return true;
} else {
$handle = popen("ps -ef | grep php 2>&1", 'r');
$cmd_cont = '';
while(!feof($handle)) {
$buffer = fgets($handle);
$cmd_cont .= $buffer;
}
pclose($handle);
if (strpos($cmd_cont, $command) === false){
return false;
}else{
return true;
}
}
}
20.在 Linux 下执行 php 脚本
在 Linux 下执行 php 脚本时通常是可以使用以下命令:
先通过which命令来找到PHP的执行路径:
which php
/usr/bin/php
#/usr/bin/php/ test.php
为简化输入可把此php执行程序 copy
到系统命令目录下:
#cp /usr/bin/php /usr/sbin/
验证:输入以下命令,如正确获取 php 版本,说明操作成功。
#php -v
这样,以后再执行PHP脚本时可直接采用以下输入方式即可。
#php a.php
21.进入目录默认访问index.php 或index.html目录
进入目录让默认访问index.php 或index.html目录
找到apache的配置文件 httpd.conf
[root@ldap /]# find -name httpd.conf
./root/httpd-2.2.11/docs/conf/httpd.conf
./usr/local/apache/conf/original/httpd.conf
./usr/local/apache/conf/httpd.conf
[root@ldap /]# cd /usr/local/apache/conf/
[root@ldap conf]# cat httpd.conf
修改Apache配置文件
<IfModule dir_module>
DirectoryIndex index.html test.php
</IfModule>
22.Linux 系统定时备份
备份是不可忽略的一环。如果我们的系统因为不同原因宕机,此时,冗余备份就显得非常重要。
系统中比较重要的目录有 /etc/,/home,root,/var/spool/mail
等,你想要在每天2:00am 进行备份,且备份数据保存到 /backup
内,并使用 tar
将备份数据打包,那该如何处理呢?
通常我们会使用 shell script
来进行备份数据的汇总,然后将该脚本添加到定时任务中,如下例:
[root@localhost ~] # mkdir /root/bin; vim /root/bin/backup.sh
#!/bin/bash
backdir="/etc /home /root /var/spool/mail"
basedir=/backup
# [ ] 部分是判断表达式,-d 表示判断是否是目录(directory)
[ ! -d "$basedir" ] && mkdir $basedir
backfile=$basedir/backup.tar.gz
tar -zcvf $backfile $backdir
[root@localhost ~] # vim /etc/crontab
0 2 * * * root sh /root/bin/backup.sh
23、查看文件大小
查看文件的大小,一般是ls 下面会显示,你要看具体的大小,可以用ls -lh来显示,M会显示出来的
ls -lh
24、生成一个大文件
createBigFile.php
<?php
$url = "http://www.jianshu.com/p/3884a6b7a780";
$content = file_get_contents($url);
// write
for($i = 0; $i <= 10000; $i++)
{
file_put_contents("big_file.txt", $content, FILE_APPEND);
}
该脚本可以在服务器下生成 500M 的文件
# /usr/local/php/bin/php createBigFile.php
将该文件通过 curl 传输,可以测试占用的宽带及网速:
<?php
$time1=microtime(true);
$post = array(
"excel"=>"@big_file.txt",//这里是要上传的文件,key与后台处理文件对应
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://120.12.11.9:9999/upload/receive.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$a=curl_exec($ch);
var_dump($a);
curl_close($ch);
$b=round($a,2);
$size='size='.$b.'MB<br>';
echo $size;
$time2=microtime(true);
$time3=$time2-$time1;
echo 'diff='.round($time3,2).'<br>';
echo 'rate='.round(($b/$time3),2).'MB/S';
?>
查看各个进程使用的宽带情况
# 查看各个进程使用的宽带情况
# iftop -i eth1
25、查看开放端口
命令一:
netstat -a 查看所有服务端口
命令二:
yum install nmap // 安装nmap软件包
nmap +ip地址 // nmap 127.0.0.1
linux开放服务端口的设置
/etc/sysconfig/iptables
vi iptables
加入如下内容
-A INPUT -p tcp -m tcp --dport 6001 -j ACCEPT
然后保存退出,重启服务
/etc/init.d/iptables restarts
查看端口是否开放
/sbin/iptables -L -n
26、du命令查看磁盘空间
du(disk usage)命令可以计算文件或目录所占的磁盘空间。没有指定任何选项时,它会测量当前工作目录与其所有子目录,分别显示各个目录所占的快数,最后才显示工作目录所占总快数。
有时候搜索出来的结果太多了(譬如,我从根目录开始搜索),一直在刷屏,如果我只想查出最大的12个文件夹,怎么办呢?此时就要借助head命令来显示了
du -hm --max-depth=1 | sort -nr | head -12
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。