入手阿里云 ECS 后首先应该做的事

Posted by Allen on 2015-11-24
Coding

持续更新中……

更改 root user 密码

1
2
3
4
$ passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

创建普通 user

1
2
3
4
5
$ useradd -m hello
$ passwd hello
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

-m, –create-home 创建用户的主目录

给普通 user 添加 sodo 命令使用权限

1
$ vi /etc/sudoers

找到root ALL=(ALL:ALL) ALL,把新建的 user 添加到这行下面:

1
2
3
# User privilege specification
root ALL=(ALL:ALL) ALL
newuser ALL=(ALL:ALL) ALL

设置 user 的 shell:sh -> bash

/etc/passwd

1
newuser:x:1000:1000::/home/newuser:

发现 root 是这样的:

1
root:x:0:0:root:/root:/bin/bash

照抄一下:

1
allen:x:1000:1000::/home/allen:/bin/bash

更改 SSH 默认端口

  1. 修改/etc/ssh/sshd_config里的Port字段

    Port 22改为Port 1000(你自定义的端口)

  2. 重启sshd服务

1
$ service ssh restart

禁用 root 登录

/etc/ssh/sshd_config 里找到 PermitRootLogin yes 设置成 no

修改 Hostname 主机名

  1. hostname 命令
1
$ hostname newname
  1. /etc/hostname 文件
1
$ vi /etc/hostname
  1. /etc/hosts

logout 生效

修改 Welcome Msg

1
$ vi /etc/motd 

安装 Git

1
2
$ apt-get install libcurl4-gnutls-dev libexpat1-dev gettext \
libz-dev libssl-dev
1
$ apt-get install git

安裝Git

安装 Nodejs

印象中 Ubuntu 14.04 apt-get 自带的 Node 版本很老,前往 Node 官网寻找最新版本的包安装方式:《Installing Node.js via package manager》。

基本步骤如下:

curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
sudo apt-get install -y nodejs

装完查看 Node 版本:

node -v

接着安装 NPM:

sudo apt-get install npm

国内 aliyun 的话还要装个 nrm,不然装东西慢到死!

sudo npm install nrm -g

装完查看可用源:

$ nrm ls

显示如下:

1
2
3
4
5
6
7
8
* npm ---- https://registry.npmjs.org/
cnpm --- http://r.cnpmjs.org/
taobao - http://registry.npm.taobao.org/
eu ----- http://registry.npmjs.eu/
au ----- http://registry.npmjs.org.au/
sl ----- http://npm.strongloop.com/
nj ----- https://registry.nodejitsu.com/
pt ----- http://registry.npmjs.pt/

切换源:

nrm use taobao

安装 Nginx

1
$ sudo apt-get install nginx

禁用 IP 访问

一般用户不会用 IP 访问网站,基本访问 IP 的都是恶意攻击。所以这边在 /etc/nginx/sites-available 里新建一个 config 文件,内容如下:

1
2
3
4
5
6
server {
listen 80 default;
server_name _;
return 500;
# rewrite ^(.*) http://www.xxx.com permanent;
}

可以根据自己的需求直接返回 500 或者重定向到其他网址,不过直接访问 IP 的流量一般也没什么价值重定向。保存,然后 ln -ssites-enable 目录,sudo service nginx restart 生效。

* 但是这样的话,localhost 也会无法使用,暂时没有找到其他方法。

安装 Apache

1
$ sudo apt-get install apache2

禁用 server-status

Apache 有时候默认开启了 server-status mod,在主机后面加上 //server-status/ 会显示 server 相关的信息,禁用方法如下:

1
$ sudo vi /etc/apache2/mods-available/status.conf 

配置文件内容大致如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<IfModule mod_status.c>
#
# Allow server status reports generated by mod_status,
# with the URL of http://servername/server-status
# Uncomment and change the ".example.com" to allow
# access from other hosts.
#
<Location /server-status>
SetHandler server-status
Order deny,allow
Allow from all
Allow from localhost ip6-localhost
# Allow from .example.com
</Location>
</IfModule>

将其中的 Allow from all 改成 Deny from all,然后 sudo service apache2 restart 生效。

安装 PHP

1
$ sudo apt-get install php5
1
$ sudo apt-get install php5-mysql

UFW Firewall

关闭所有外部对本机的访问(本机访问外部正常)

1
$ sudo ufw default deny

开启/禁用相应端口

1
2
$ sudo ufw allow 80 允许外部访问80端口
$ sudo ufw delete allow 80 禁止外部访问80 端口
1
2
3
4
5
6
7
8
$ sudo ufw allow from 192.168.1.1 允许此IP访问所有的本机端口
$ sudo ufw deny smtp 禁止外部访问smtp服务
$ sudo ufw delete allow smtp 删除上面建立的某条规则
$ sudo ufw deny proto tcp from 10.0.0.0/8 to 192.168.0.1 port 22 要拒绝所有的TCP流量从10.0.0.0/8 到192.168.0.1地址的22端口
# 可以允许所有RFC1918网络(局域网/无线局域网的)访问这个主机(/8,/16,/12是一种网络分级):
$ sudo ufw allow from 10.0.0.0/8
$ sudo ufw allow from 172.16.0.0/12
$ sudo ufw allow from 192.168.0.0/16
1
2
3
$ sudo ufw enable
$ sudo ufw disable
$ sudo ufw status

UFW防火墙简单设置
Ufw使用指南

[linux]入手 VPS 后首先该做的事情


Comments: