全新vps 上 安裝 基本的服務 與基本的安全防禦

首先,請務必先在虛擬機上先模擬,而後再到vps 上操做 . 大牛請飄過.. linux

本文是以 ubuntu 12.04 LTS server 爲例 shell

SSH篇


新建用戶且擁有sudo 權限

如下操做以root 用戶操做 ubuntu

useradd test
passwd test
cd /home

mkdir test

chown test /home/test
更改 /etc/sudoers 的可讀權限

chmod 640 /etc/sudoers 

vi /etc/sudoers 

找到這一 行:"root ALL=(ALL) ALL"在起下面添加"xxx ALL=(ALL) ALL"(這裏的xxx是你的用戶名)
:wq 保存退出

chmod 440 /etc/sudoers


SSH安裝與配置

sudo apt-get install openssh-server

安裝好後, 接下來就是配置 密鑰 驗證, 去除密碼登入 (防止vps 被 人密碼爆破) 安全

如下7個步驟參考自 這裏 服務器

  1. 如下操做請先在虛擬機試驗!
  2. 建立目錄:mkdir ~/.ssh/,並進入這個新目錄:cd ~/.ssh/
  3. 執行:ssh-keygen -t rsa 或 ssh-keygen -d  產生出 id_rsa, id_rsa.pub兩個文件。
  4. 重命名:mv id_rsa.pub authorized_keys
  5. 注意:.ssh目錄的權限必須是700,即除了用戶本身,其餘人沒有任何讀寫察看該目錄的權限,不然ssh服務器會拒絕登陸。ssh默認的公鑰文件是用戶home目錄下的.ssh目錄下的authorized_keys文件,所以須要把產生的公鑰以這個文件名放到服務器的用戶的~/.ssh/目錄下。 更改目錄權限:chmod 700 ~/.ssh/ 
  6. 把id_rsa文件下載到本地,用ssh客戶端導入密鑰。 
  7. 修改/etc/ssh/sshd_config,把下面這兩行去掉註釋: 
    PubkeyAuthentication  yes
    AuthorizedKeysFile  .ssh/authorized_keys
    再把「PasswordAuthentication yes」改成「PasswordAuthentication no
    」,執行:/etc/init.d/sshd reload

注意上面第6 步,須要 puttyGen轉換成  id_rsa.ppk 才能 引入, 不然 會報這個錯誤(假如你也用putty 的話) session

unable to use key file (openssh ssh-2 private key) ssh


ssh 上傳下載命令: 

下載命令  socket

格式:  scp username@hostIP:文件所在地址   存儲的本地文件目錄 async

eg:   ide

從遠程vps 把密鑰 下載到本地的home 

 scp swrite@172.16.31.128:/home/swrite/.ssh/id_rsa   ~ 


上傳命令

從本地上傳 文件到 遠程vps

格式: scp 本地文件路徑   username@hostIP:目標文件所在地址

 scp ./linux命令  swrite@172.16.31.128:~





禁止root 用戶登入

sudo vi /etc/ssh/sshd_config

更改  
PermitRootLogin no


FTP篇

安裝vsftp


sudo apt-get install vsftpd

配置 vsftp


通常配置 也就 那麼幾個須要更改的

listen=YES       # 服務器監聽
anonymous_enable=YES       # 匿名訪問容許
local_enable=YES    # 本地主機訪問容許
write_enable=YES    # 寫容許
anon_upload_enable=YES

anon_mkdir_write_enable=YES  # 匿名建立文件夾容許
dirmessage_enable=YES  # 進入文件夾容許
xferlog_enable=YES   #  ftp 日誌記錄容許

我把個人配置貼出來吧,可供參考(關閉了匿名訪問等權限)


# Example config file /etc/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
#
# Run standalone?  vsftpd can run either from an inetd or as a standalone
# daemon started from an initscript.
listen=YES
#
# Run standalone with IPv6?
# Like the listen parameter, except vsftpd will listen on an IPv6 socket
# instead of an IPv4 one. This parameter and the listen parameter are mutually
# exclusive.
#listen_ipv6=YES
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=no
#
# Uncomment this to allow local users to log in.
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# If enabled, vsftpd will display directory listings with the time
# in  your  local  time  zone.  The default is to display GMT. The
# times returned by the MDTM FTP command are also affected by this
# option.
use_localtime=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
#chown_uploads=YES
#chown_username=whoever
#
# You may override where the log file goes if you like. The default is shown
# below.
xferlog_file=/var/log/vsftpd.log
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
#xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
#ascii_upload_enable=YES
#ascii_download_enable=YES
#
# You may fully customise the login banner string:
#ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd.banned_emails
#
# You may restrict local users to their home directories.  See the FAQ for
# the possible risks in this before using chroot_local_user or
# chroot_list_enable below.
#chroot_local_user=YES
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
chroot_local_user=YES
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd.chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# Customization
#
# Some of vsftpd's settings don't fit the filesystem layout by
# default.
#
# This option should be the name of a directory which is empty.  Also, the
# directory should not be writable by the ftp user. This directory is used
# as a secure chroot() jail at times vsftpd does not require filesystem
# access.
secure_chroot_dir=/var/run/vsftpd/empty
#
# This string is the name of the PAM service vsftpd will use.
pam_service_name=vsftpd
#
# This option specifies the location of the RSA certificate to use for SSL
# encrypted connections.
rsa_cert_file=/etc/ssl/private/vsftpd.pem


好了,如今  建立一個ftpuser 的ftp目錄  

sudo mkdir  /home/ftp

建立ftp 登入用戶:  ftpuser  , ftp 目錄爲 /home/ftp 

sudo useradd -g ftp -d /home/ftp/ -M ftpuser    

重置密碼

sudo passwd ftpuser

改變  /home/ftp 的 所屬用戶

sudo chown ftpuser /home/ftp

---ps , 假如 你的  /etc/vsftpd.conf  文件 配置了 以下幾項


chroot_local_user=YES
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd.chroot_list
須要 建立  /etc/vsftpd.chroot_list 文件 , 並把 你的ftpuser  添加進去(ftp 用戶控制)


touch /etc/vsftpd.chroot_list 

而後把你建立的用戶添加進去, 每一個用戶一行 


 好了, 你ftp 配置完成



防火牆篇 


哎呀, 忽然困了.. 先去睡覺.. 等之後在來寫..

不過, 你能夠暫時先參考這裏, 

1 iptable 基本原理

2 iptable 配置舉例

3  如何保證linux 服務器安全


good night , 早睡早起。

相關文章
相關標籤/搜索