Author: jpuyy

  • ubuntu工具之apt的使用 | apt-spy的应用

    办公室一台弄过来的电脑
    装了debian6.0

    debian/ubuntu 是好东西,也是我平时用的最多的linux

    apt给人们带来的很大的方便

    总是找源的问题
    还是不想麻烦了,以后就用apt-spy解决找源
    到这里去下载对应的版本http://packages.debian.org/sid/i386/apt-spy/download

    下好后
    apt-spy -d unstable -a asia -t 5
    ……………………………………………………………………………………………………
    Tips

    * To retrieve the latest list of Debian mirrors, run (as root)

    # apt-spy update

    * To find the fastest mirror in North America for testing, run

    # apt-spy -d testing -a north-america

    * To find the fastest mirror in Germany for stable, run

    # apt-spy -d stable -s de

    FAQ’s

    Q. What are the region (-d argument) codes?

    A. africa, asia, europe, north-america, oceania, and south-america

    Q. What if I want apt-spy to only text x mirrors?

    A. Add the argument -e x. For example, apt-spy -d unstable -a asia -e 5 would test 5 mirrors hosting unstable, and located in Asia.

    把sources.list覆盖
    #cat /etc/apt/sources.list.d/apt-spy.list > /etc/apt/sources.list

    接下来想把这两条命令写到一个bash里,并用cron每天执行一次
    #vim /root/renewapt.sh

    #for update /etc/sources.list using apt-spy
    #!/bin/bash
    apt-spy update
    apt-spy -d unstable -a asia -t 5
    cat /etc/apt/sources.list.d/apt-spy.list > /etc/apt/sources.list

    #crontab -e
    0 0 * * * bash /root/renewapt.sh

  • lsof命令详解

    lsof – list open files显示程序打开的文件(linux下所有的文件,程序,进程都是文件)

    root用户才能执行lsof命令,普通用户会被拒绝显示或无显示结果。

    根据实例学习用法:

    显示已经打开的文件

    lsof

    显示所有打开的internet链接(-i),unix socket (-U)

    lsof -i -U

    显示被进程1197打开的ipv4网络文件,可以看到是pptpd的进程,正在监听状态

    # lsof -i 4 -a -p 1197
    COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
    pptpd 1197 root 6u IPv4 4181 0t0 TCP *:1723 (LISTEN)

    显示打开ipv6的网络文件

    # lsof -i 6

    显示使用tcp协议,22端口(或ssh,在/etc/service中)的ipv4和ipv6文件

    # lsof -i tcp:22
    或
    # lsof -i tcp:ssh
    COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
    sshd 14873 root 3u IPv4 4100995 0t0 TCP ibm.jpuyy.com:ssh->114.221.19.245:56544 (ESTABLISHED)
    sshd 17248 root 3r IPv4 4149257 0t0 TCP ibm.jpuyy.com:ssh->114.221.19.245:52610 (ESTABLISHED)
    sshd 27849 root 3u IPv4 3235729 0t0 TCP *:ssh (LISTEN)
    sshd 27849 root 4u IPv6 3235731 0t0 TCP *:ssh (LISTEN)

    显示某个命令打开的文件,如pptpd

    # lsof -c pptpd

    显示和ip:1.2.3.4有关的打开的文件

    # lsof -i @1.2.3.4

    显示和ip:1.2.3.4有关的tcp协议ftp

    # lsof -i [email protected]:ftp

    显示和jpuyy有关的打开的文件

    # lsof -u jpuyy

    不断显示连到jpuyy.com的80端口的打开的文件,(default fifteen)

    # lsof -i @jpuyy.com:80 -r

    显示/dev/sdb1打开的文件

    # lsof /dev/sdb1

     

    lsof |sort -k7 -n

  • Mysql忘记密码时重置密码

    方法一:

    一个中心:停止服务,载入安全模式

    killall -TERM mysqld

    然后

    mysqld --skip-grant-tables &

    两个基本点:更新user表,刷新权限

    use mysql;
     update user set password=password("newpass") where user="root";
     flush privileges;

    如果不方便用mysql的cli界面交互
    调用mysqladmin即可

    mysqladmin -u root flush-privileges password "newpassword"

     

    方法二(debian亲测):

    直接使用/etc/mysql/debian.cnf文件中[client]节提供的用户名和密码 备用

    mysql -u debian-sys-maint -p
    Enter password: <输入[client]节的密码>

    mysql> show databases;

    mysql> use mysql;

    mysql> show tables;

    这个时候使用user表

    mysql> UPDATE user SET Password=PASSWORD(‘newpassword’) where USER=’root’;

    Query OK, 3 rows affected (0.00 sec)
    Rows matched: 3  Changed: 3  Warnings: 0
    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.00 sec)
    mysql> quit

    # mysql -u root -p
    Enter password: <输入新设的密码newpassword>
    mysql>

    方法三(centos 6.2亲测,等同于方法一):

    1.确认服务器处于安全的状态,在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护,不连网操作最好,实在不行挑个好时候,操作要快
    2.修改MySQL的登录设置:

    # vi /etc/my.cnf

    在[mysqld]的段中加上一句:skip-grant-tables
    例如:

    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    skip-grant-tables

    保存并且退出vi。
    3.重新启动mysqld

    # /etc/init.d/mysqld restart
    Stopping MySQL: [ OK ]
    Starting MySQL: [ OK ]

    4.登录并修改MySQL的root密码

    [root@vps ~]# mysql
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.1.61 Source distribution
    Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    mysql> use mysql;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    Database changed
    mysql> update user set password=password('jpuyy') where user = 'root';
    Query OK, 2 rows affected (0.00 sec)
    Rows matched: 2 Changed: 2 Warnings: 0
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    mysql> quit
    Bye

    5.将MySQL的登录设置修改回来

    # vi /etc/my.cnf

    将刚才在[mysqld]的段中加上的skip-grant-tables删除
    保存并且退出vi。
    6.重新启动mysqld

    # /etc/init.d/mysqld restart
    Stopping MySQL: [ OK ]
    Starting MySQL: [ OK ]

    附加windows下的重置方法

    开始-运行-cmd

    net stop mysql

    然后进入到mysql的安装位置,运行如下命令

    mysqld-nt.exe –skip-grant-tables

    再打开一个cmd窗口

    运行msyql -u root -p,不用输入密码即可进入

    > use mysql

    > UPDATE user SET Password=PASSWORD(‘newpassword’) where USER=’root’;

    >flush privileges;

    最后在任务管理器里将mysql-nt的任务和进程都停止掉(如果不停掉会出现“mysql服务无法启动。系统出错。系统发生1067错误。进程意外中止。”),接下来

    net start mysql

    密码重置完成。

  • nginx常用配置及操作

    此博文意味着已经安装好了nginx php mysql : http://jpuyy.com/?p=764

    0.nginx配制文件 /usr/local/nginx/conf/nginx.conf
    1.启动nginx # /usr/local/nginx/sbin/nginx
    2.Nginx 配置文件测试:#/usr/local/nginx/sbin/nginx -t //Debug 配置文件的关键命令需要重点撑握
    3.使用ngx_http_access_module限制ip访问
    location / {
    deny 192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    deny all;
    }
    4.Nginx 配置文件修改重新加载
    方法一:
    首先查看进程PID值
    #ps aux |grep nginx
    root 2643 0.0 0.0 4804 664 ? Ss 16:36 0:00 nginx: master process /usr/local/nginx/sbin/nginx
    平滑重启,输入 kill -HUP 2643

    方法二:
    可以reload配置文件,也等于动态加载了。
    #/usr/local/nginx/sbin/nginx -s reload

    5.把nginx弄为开机启动

    Go to /etc/init.d and run sudo vim ngnix-passenger.sh
    在里面写入

    #!/bin/bash
    # this script starts the nginx process attached to passenger
    sudo /usr/local/nginx/sbin/nginx
    sudo chmod +x /etc/init.d/ngnix-passenger.sh.
    sudo /etc/init.d/ngnix-passenger.sh this will run all the code in the script.
    Verify that it launches nginx before continuing.

    Then run sudo update-rc.d ngnix-passenger.sh defaults while still in the /etc/init.d directory.
    Once all of this is in place, reboot your server and ngnix should now be automatically spawned on startup

  • 利用批处理导出注册表中的单独某项

    regedit参数

    filename 导入 .reg 文件进注册表
    /s 导入 .reg 文件进注册表(安静模式)
    /e 导出注册表文件

    例:
    导出XXXX
    regedit /e filename.reg HKEY_LOCAL_MACHINE\SOFTWARE\XXXX

    /L:system 指定 system.dat

    /R:user 指定 user.dat

    /C 压缩 [文件名] (Windows 98)

    还有一些参数也是可以用的,比如/a,但是我尝试后发现功能无非就是上述这些!而且测试参数很容易导致你的注册表加倍庞大,实在是一个很危险的工作。

    批处理的话,把象上面的命令保存成bat文件就可以了

  • nginx安装| lnmp | apache换为nginx | 禁用apache

    办公室的电脑,拿来用用,试试nginx的特性
    初步觉得要是卸载可能比较麻烦,反正电脑空间也大

     

    • 采取不卸载,停止apache服务,并禁止其开机启动的方法

    先查看apache服务

    #ps -ef | grep apache2

    root 1243 1 0 19:50 ? 00:00:00 /usr/sbin/apache2 -k start
    www-data 1264 1243 0 19:50 ? 00:00:00 /usr/sbin/apache2 -k start
    www-data 1265。。。。。。apache2 -k start
    root 3154 2796 0 20:30 pts/0 00:00:00 grep –color=auto apache2
    停止apache2

    #service apache2 stop

    * Stopping web server apache2 … waiting [ OK ]
    重启了果真又出现了,那么说明要在启动上下手

    It works!
    This is the default web page for this server.

    网上查找例子(其他都类似):
    删除apache2随机器启动的服务

    #update-rc.d -f apache2 remove

    Removing any system startup links for /etc/init.d/apache2 …
    /etc/rc0.d/K09apache2
    /etc/rc1.d/K09apache2
    /etc/rc2.d/S91apache2
    /etc/rc3.d/S91apache2
    /etc/rc4.d/S91apache2
    /etc/rc5.d/S91apache2
    /etc/rc6.d/K09apache2
    奏效

    接下来想安装nginx
    也不知道是从源里直接安装好还是编译安装好
    编译了后没成功,缺少格物致知精神
    后来用的lnmp 脚本
    nginx是起来了,但是还是有权根问题
    对各个文件目录不太熟
    导致还是请ihipop来帮忙
    经过多次实验,完整安装过程如下(以下内容 added at 2011-8-7):

    以下安装在debian下进行(ubuntu下自己有php,不需要,ubuntu的安装方法http://wiki.ubuntu.org.cn/Nginx)

    安装基本环境:

    apt-get install build-essential
    • 编译安装nginx:

    先下载好最新版本的nginxhttp://nginx.org/en/download.html

    wget http://nginx.org/download/nginx-0.8.54.tar.gz
     tar vxzf nginx-0.8.54.tar.gz
     cd nginx-0.8.54
     ./configure --with-http_ssl_module
     make && make install

    下面为编译里遇到的问题,缺少了某些库,apt-get以后就没有问题了:

    ./configure: error: the HTTP rewrite module requires the PCRE library.
    这时要执行下面命令(因为rewrite模块需要)
    apt-get install libpcre3-dev

    ./configure: error: the HTTP gzip module requires the zlib library.
    这时要执行下面命令(因为gzip模块需要)
    apt-get install zlib1g-dev

    在./configure –with-http_ssl_module时提示如下信息
    ./configure: error: SSL modules require the OpenSSL library.
    这时要执行下面命令(因为OpenSSL模块需要)
    apt-get install libssl-dev

    • 安装mysql:

    apt-get install mysql-server  php5-mysql

    过程中会提示输入mysql根用户密码

    • 安装php:

    php5-fpm的源

    deb http://packages.dotdeb.org stable all
    deb-src http://packages.dotdeb.org stable all
    deb http://php53.dotdeb.org stable all

    为省力执行下面命令:

    echo "deb http://packages.dotdeb.org stable all" >> /etc/apt/sources.list;echo "deb-src http://packages.dotdeb.org stable all" >> /etc/apt/sources.list;echo "deb http://php53.dotdeb.org stable all" >> /etc/apt/sources.list

    加入后apt-get update出现以下提示

    正在读取软件包列表… 完成
    W: GPG 错误:http://packages.dotdeb.org stable Release: 由于没有公钥,无法验证下列签名: NO_PUBKEY E9C74FEEA2098A6E
    W: GPG 错误:http://php53.dotdeb.org stable Release: 由于没有公钥,无法验证下列签名: NO_PUBKEY E9C74FEEA2098A6E

    故通过下面命令导入公钥

    wget http://www.dotdeb.org/dotdeb.gpg
    cat dotdeb.gpg | apt-key add -

    apt-get update

    apt-get install php5-fpm

    至此,lnmp安装完成。

    要想成功解译出php,在nginx.conf 里相应 server 部分做如下修改:

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    location ~ \.php$ {
    root           /web/;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /web/$fastcgi_script_name;
    include        fastcgi_params;
    }

    :)EOT