本篇文章主要是针对将Ubuntu做为web开发和测试服务器搭建前的一些基本设置,如果您想把Ubuntu做为日常办公和影音娱乐等的用途,这里的基本设置将不符合您的要求,请参考网上的其它文章。
这里主要讲的是Ubuntu12.04个人桌面Desktop版和Ubuntu12.04.1服务器Server版系统新安装后的基础设置,基础设置主要是指打开一些常用的端口和安装ssh,实现远程登录等。
- 给root用户设置密码
sudo passwd root
按提示输入当前登录用户的密码,然后再输入两次root的新密码就OK了 - 切换到root用户,这样输入命令时不需要频繁的添加sudo前缀;
su -
注意:这里用的是“su -”,su后有个减号“-”,加了这个减号的目的是使环境变量和欲转换的用户相同、不加-是取得用户的临时权限,有可能会使部份命令不能正常使用,提示命令不存在!
如果是新手,容易会误操作,所以不建议直接切换到root用户进行操作,使用非root用户操作即可。对输入以下的命令,需要拥有root权限才能执行的,在命令前加sudo前缀即可。 - 安装 OpenSSH Server,实现远程登录
apt-get install openssh-server
(查看返回的结果,如果没有出错,则用putty、SecureCRT、SSH Secure Shell Client等SSH 客户端软件,输入您服务器的 IP 地址。如果一切正常的话,等一会儿就可以连接上了。并且使用现有的用户名和密码应该就可以登录了。)
然后确认sshserver是否启动了:(或用“netstat -tlp”命令)
ps -e | grep ssh
如果只有ssh-agent那ssh-server还没有启动,需要/etc/init.d/ssh start,如果看到sshd那说明ssh-server已经启动了。
ssh-server配置文件位于/ etc/ssh/sshd_config,在这里可以定义SSH的服务端口,默认端口是22,你可以自己定义成其他端口号,如222。然后重启SSH服务:
sudo /etc/init.d/ssh resart - 安装ubuntu自带的防火墙UFW,并对端口进行管理设置
1.安装
apt-get install ufw
2.启用
ufw enable
ufw default deny
运行以上两条命令后,开启了防火墙,并在系统启动时自动开启。关闭所有外部对本机的访问,但本机访问外部正常。
3.开启/禁用服务
ufw allow|deny [service]
打开或关闭某个端口,例如:
ufw allow smtp 允许所有的外部IP访问本机的25/tcp (smtp)端口
ufw allow 22/tcp 允许所有的外部IP访问本机的22/tcp (ssh)端口
ufw allow 53 允许外部访问53端口(tcp/udp)
ufw allow from 192.168.1.100 允许此IP访问所有的本机端口
ufw allow proto udp 192.168.0.1 port 53 to 192.168.0.2 port 53
ufw deny smtp 禁止外部访问smtp服务
ufw delete allow smtp 删除上面建立的某条规则
4.查看防火墙状态
ufw status
一般用户,只需如下设置:
apt-get install ufw
ufw enable
ufw default deny
以上三条命令已经足够安全了,如果你需要开放某些服务,再使用ufw allow开启。
开启/关闭防火墙 (默认设置是’disable’)
ufw enable|disable
转换日志状态
ufw logging on|off
设置默认策略 (比如 “mostly open” vs “mostly closed”)
ufw default allow|deny
许可或者屏蔽端口 (可以在“status” 中查看到服务列表)。可以用“协议:端口”的方式指定一个存在于/etc/services中的服务名称,也可以通过包的meta-data。 ‘allow’ 参数将把条目加入 /etc/ufw/maps ,而 ‘deny’ 则相反。基本语法如下:
ufw allow|deny [service]
显示防火墙和端口的侦听状态,参见 /var/lib/ufw/maps。括号中的数字将不会被显示出来。
ufw status
UFW 使用范例:
允许 53 端口
$ ufw allow 53
禁用 53 端口
$ ufw delete allow 53
允许 80 端口
$ ufw allow 80/tcp
禁用 80 端口
$ ufw delete allow 80/tcp
允许 smtp 端口
$ ufw allow smtp
删除 smtp 端口的许可
$ ufw delete allow smtp
允许某特定 IP
$ ufw allow from 192.168.254.254
删除上面的规则
$ ufw delete allow from 192.168.254.254在这里,我们设置下以下端口是对外开放的
ufw allow 22 # SSH访问端口
ufw allow 80 # web访问端口
ufw allow 443 # web的SSl访问端口
ufw allow 3306 # MySQL数据库访问端口 - 自动设置Shell脚本
#!/bin/bash # ------------------------------------------------------------------------------- # Filename: system_init_shell.sh # Revision: 1.0 # Date: 2012/08/22 # Author: 三木 # Email: linmaogan#gmail.com # Website: www.3mu.me # Description: Ubuntu12.04.1系统新安装后的设置 # Notes: 需要切换到root运行 # ------------------------------------------------------------------------------- # Copyright: 2012 (c) 三木 # License: GPL # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # you should have received a copy of the GNU General Public License # along with this program (or with Nagios); # # Credits go to Ethan Galstad for coding Nagios # If any changes are made to this script, please mail me a copy of the changes # ------------------------------------------------------------------------------- #Version 1.0 #2012-08-22 三木 初始版本建立 # ------------------------------------------------------------------------------- #禁止IPv6############################################################################### sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT/#GRUB_CMDLINE_LINUX_DEFAULT/g /GRUB_CMDLINE_LINUX_DEFAULT/a\\GRUB_CMDLINE_LINUX_DEFAULT="ipv6.disable=1 quiet splash"' /etc/default/grub #让修改生效 update-grub #让系统自动查找最快更新源############################################################### #更新源 apt-get update #安装 apt-spy apt-get install apt-spy #测试源 apt-spy -d stable -a asia -t 5 #先设置数据自动备份工具################################################################# #echo '0 17 * * * root nice /backup/backup.sh >> /data/wwwroot/log/backup.log' >> /etc/crontab #防火墙设置############################################################################# #安装防火墙工具UFW apt-get install ufw #开启防火墙 ufw enable #设置默认策略 ufw default deny #打开常用端口 ufw allow 22 # SSH访问端口 ufw allow 80 # web访问端口 ufw allow 443 # web的SSl访问端口 ufw allow 3306 # MySQL数据库访问端口 #显示防火墙和端口的侦听状态 ufw status
最近正在使用ubuntu 开始慢慢的喜欢上ubuntu了,看了你的博客觉得像这样自己制作自己风格的博客才是最好的选择,我选择的是别人的主题
http://www.ylsay029.com