Wednesday, July 18, 2012

Iptables开放22,53,80端口


01iptables -F
02# 允许包从22端口进入
03iptables -A INPUT -p tcp --dport 22  -j ACCEPT
04# 允许从22端口进入的包返回
05iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
06iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
07iptables -A INPUT -p udp --sport 53 -j ACCEPT
08#允许本机访问本机
09iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
10iptables -A OUTPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
11#允许所有IP访问80端口
12iptables -A INPUT -p tcp -s 0/0 --dport 80 -j ACCEPT
13iptables -A OUTPUT  -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
14#更改默认策略
15iptables -P INPUT DROP
16iptables -P FORWARD DROP
17iptables -P OUTPUT DROP
18#保存配置
19iptables-save > /etc/sysconfig/iptables
20iptables -L

Friday, July 06, 2012

让putty显示中文

对于经常在windows下远程ssh到linux的用户而言,putty可能是你最好的选择。

可是缺省情况下,putty对中文的支持却让人不敢恭维,如果远程linux的locale设置为zh_CN.*(bg2312,gbk,utf8等等),显示就是乱码。经研究发现,其实putty的中文支持还是很好的,呵呵

打开putty主程序,选择window-〉Appearance-〉Font settings-〉Change...,选择Fixedsys字体,字符集选择CHINESE_GB2312。
在window-〉Appearance-〉Translation中,Received data assumed to be in which character set 中,把Use font encoding改为UTF-8.
如果经常使用,把这些设置保存在session里面.

现在打开putty,登录成功后,在shell中输入:export LC_ALL='zh_CN.utf8',现在已经可以完美的支持中文了

可以打开vim输入中文测试一下,而且也不会出现删除半个汉字的问题.

Friday, April 20, 2012

yum安装LAMP(Apache+MySQL+PHP)+VSFTPD


LAMPLinux,Apache,MySQL,PHP的缩写。这个教程主要是讲在centos安装有php和mysql支持的apache web服务器。在这个教程我使用主机名server1.example.com对应IP地址192.168.0.100。这些设置可能不同于你,你必须替换成你正确的。

添加外部源,保证MySQL和PHP最新

因为系统基本的源的MySQL和PHP程序比较旧,所以我们添加比较新的外部源。
  1. # rpm --import http://www.jasonlitka.com/media/RPM-GPG-KEY-jlitka
  2. # vi /etc/yum.repos.d/CentOS-Base.repo
在底部增加下面信息
  1. [utterramblings]
  2. name=Jason's Utter Ramblings Repo
  3. baseurl=http://www.jasonlitka.com/media/EL$releasever/$basearch/
  4. enabled=1
  5. gpgcheck=1
  6. gpgkey=http://www.jasonlitka.com/media/RPM-GPG-KEY-jlitka

安装MySQL

执行yum程序安装MySQL
  1. yum install mysql mysql-server
添加MySQL进启动项(这样系统启动时会自动启动MySQL),并立即启动MySQL服务器:
  1. chkconfig --levels 235 mysqld on
  2. /etc/init.d/mysqld start
设置MySQL root帐号密码:
  1. mysql_secure_installation
  1. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
  2.       SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
  3.  
  4.  
  5. In order to log into MySQL to secure it, we'll need the current
  6. password for the root user.  If you've just installed MySQL, and
  7. you haven't set the root password yet, the password will be blank,
  8. so you should just press enter here.
  9.  
  10. Enter current password for root (enter for none):
  11. OK, successfully used password, moving on...
  12.  
  13. Setting the root password ensures that nobody can log into the MySQL
  14. root user without the proper authorisation.
  15.  
  16. Set root password? [Y/n] <-- ENTER
  17. New password: <-- 你的MySQL root密码
  18. Re-enter new password: <-- 你的MySQL root密码
  19. Password updated successfully!
  20. Reloading privilege tables..
  21.  ... Success!
  22.  
  23.  
  24. By default, a MySQL installation has an anonymous user, allowing anyone
  25. to log into MySQL without having to have a user account created for
  26. them.  This is intended only for testing, and to make the installation
  27. go a bit smoother.  You should remove them before moving into a
  28. production environment.
  29.  
  30. Remove anonymous users? [Y/n] <-- ENTER
  31.  ... Success!
  32.  
  33. Normally, root should only be allowed to connect from 'localhost'.  This
  34. ensures that someone cannot guess at the root password from the network.
  35.  
  36. Disallow root login remotely? [Y/n] <-- ENTER
  37.  ... Success!
  38.  
  39. By default, MySQL comes with a database named 'test' that anyone can
  40. access.  This is also intended only for testing, and should be removed
  41. before moving into a production environment.
  42.  
  43. Remove test database and access to it? [Y/n] <-- ENTER
  44.  - Dropping test database...
  45.  ... Success!
  46.  - Removing privileges on test database...
  47.  ... Success!
  48.  
  49. Reloading the privilege tables will ensure that all changes made so far
  50. will take effect immediately.
  51.  
  52. Reload privilege tables now? [Y/n] <-- ENTER
  53.  ... Success!
  54.  
  55. Cleaning up...
  56.  
  57.  
  58.  
  59. All done!  If you've completed all of the above steps, your MySQL
  60. installation should now be secure.
  61.  
  62. Thanks for using MySQL!

安装Apache

Apache在CentOS软件包可用,因此我们可以使用yum安装:
  1. yum install httpd
设置系统启动时自启动Apache
  1. chkconfig --levels 235 httpd on
并启动Apache
  1. /etc/init.d/httpd start

安装PHP

我们可以按照下面的命令安装PHP和Apache PHP模块:
  1. yum install php
然后重启Apache服务器:
  1. /etc/init.d/httpd restart

安装相关模块使用PHP支持MySQL

为了让PHP支持MySQL,我们可以安装php-mysql软件包。你也可以安装其它的php模块。可以使用以下命令搜索可用的php模块:
  1. yum search php
选择一些你需要的模块进行安装:
  1. yum install php-mysql php-common php-mbstring php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc
现在重启Apache:
  1. /etc/init.d/httpd restart

安装phpMyAdmin

phpMyAdmin是一个可视化管理你的MySQL数据库的软件。
安装很简单,下载解压到根目录即可.
下载地址:http://www.phpmyadmin.net/home_page/downloads.php
接着我们配置phpMyAdmin,为了使用phpMyAdmin不仅仅只允许localhost连接,我们修改一下apache的配置。
  1. vi /etc/httpd/conf.d/phpmyadmin.conf
填入如下内容:
  1. #
  2. #  Web application to manage MySQL
  3. #
  4.  
  5. #<Directory "/usr/share/phpmyadmin">
  6. #  Order Deny,Allow
  7. #  Deny from all
  8. #  Allow from 127.0.0.1
  9. #</Directory>
  10.  
  11. Alias /phpmyadmin /usr/share/phpmyadmin
  12. Alias /phpMyAdmin /usr/share/phpmyadmin
  13. Alias /mysqladmin /usr/share/phpmyadmin
重启apache:
/etc/init.d/httpd restart
然后你就可以通过http://192.168.0.100/phpmyadmin/进入phpMyAdmin了。

 简单配置文件:
apache的配置文件是/etc/httpd/conf下
modules放在/usr/lib/httpd下
php的配置文件在/etc/php.d/下 和/etc/php.ini
php的modules放在/usr/lib/php/modules下


在iptables里开放80端口
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save



vsftpd作为FTP服务器,在Linux系统中是非常常用的。下面我们介绍如何在centos系统上安装vsftp。

什么是vsftpd

vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序。特点是小巧轻快,安全易用。
vsftpd 的名字代表”very secure FTP daemon”, 安全是它的开发者 Chris Evans 考虑的首要问题之一。在这个 FTP 服务器设计开发的最开始的时候,高安全性就是一个目标。

安装vsftpd

1、以管理员(root)身份执行以下命令
  1. yum install vsftpd
2、设置开机启动vsftpd ftp服务
  1. chkconfig vsftpd on
3、启动vsftpd服务
  1. service vsftpd start
管理vsftpd相关命令:
停止vsftpd:  service vsftpd stop
重启vsftpd:  service vsftpd restart

配置防火墙

打开/etc/sysconfig/iptables文件
  1. vi /etc/sysconfig/iptables
在REJECT行之前添加如下代码
  1. -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
保存和关闭文件,重启防火墙
  1. service iptables start

配置vsftpd服务器

默认的配置文件是/etc/vsftpd/vsftpd.conf,你可以用文本编辑器打开。
  1. vi /etc/vsftpd/vsftpd.conf

添加ftp用户

下面是添加ftpuser用户,设置根目录为/home/wwwroot/ftpuser,禁止此用户登录SSH的权限,并限制其访问其它目录。
1、修改/etc/vsftpd/vsftpd.conf
将底下三行
  1. #chroot_list_enable=YES
  2. # (default follows)
  3. #chroot_list_file=/etc/vsftpd.chroot_list
改为
  1. chroot_list_enable=YES
  2. # (default follows)
  3. chroot_list_file=/etc/vsftpd/chroot_list
3、增加用户ftpuser,指向目录/home/wwwroot/ftpuser,禁止登录SSH权限。
  1. useradd -d /home/wwwroot/ftpuser -g ftp -s /sbin/nologin ftpuser
4、设置用户口令
  1. passwd ftpuser
5、编辑文件chroot_list:
  1. vi /etc/vsftpd/chroot_list
内容为ftp用户名,每个用户占一行,如:
peter
john
6、重新启动vsftpd
  1. service vsftpd restart
另外,如果觉得以后管理ftp用户名嫌麻烦,可以使用centos官方发布的脚本管理。地址如下:

出现的错误

1、500 OOPS: cannot change directory
解决方法:http://www.centos.bz/2011/10/ftp-500-oops-cannot-change-directory/