Linux基础

Linux 最初是 Linus Torvalds 于 1991 年在大学写的操作系统。今天,Linux 拥有了优秀的开发团队、庞大的用户社区和成功的商业模式。虽然在桌面市场表现不佳,但是在 Linux 服务器(Top 10 网站中的六家)、科学计算(Top 500 中的 459 台)、嵌入式(大量的路由设备、工业控制产品)以及新兴的移动设备(Android)领域都取得巨大的成功。

Linux 是现今最成功的自由软件之一。

SSH 登录 Linux服务器 流程说明

Xshell : 极好用的免费SSH客户端

点此下载 Xshell

创建session

安装好xshell后,打开软件,点击菜单栏“File”中的“new”,将出现下图所示弹窗,填写相应信息。其中Host为邮件中所给的主机ip地址(name栏可以不用修改,但一般为便于在xshell中区分不同的主机,自己一般会修改为“用户名@主机”,也可以用“用户名@42qu”),然后点击“ok”。

首次登录

登录

在随后弹出的弹窗中选择自己创建的帐号,点击“connect”,在弹出窗口中输入邮件中提供的用户名,新窗口中选择“keyboard Interactive”

并确定,最后输入邮件中提供的密码,即可登录到自己的vps中。

  1. ../_images/login1.png
  2. ../_images/login2.png
  3. ../_images/login3.png

设置显示编码

简单设置一下xshell的字符显示,在下图所示的导航按钮中,勾选“UTF-8”。

../_images/encoding.png

顺便可以设置下字体

../_images/xshell_ft.png

如果觉得粗体看着碍眼, 那么可以修正下显示设置

  1. ../_images/xshell_font_btn.png
  2. ../_images/xshell_font.png

配置密钥登录 , 无需每次输入密码

为避免每次登录vps都需要重复输入用户名和密码的步骤,可以通过生成.ssh/authorized_keys来减少麻烦。

执行:

cd ~

命令,来到home(家)目录

执行:

ssh-keygen

命令 , 然后按两次回车, 生成密钥

执行:

cd  .ssh

进入.ssh目录

执行:

cat id_rsa.pub >> authorized_keys

将把当前目录下

id_rsa.pub中的数据拷贝一份到新建的authorized_keys档案中。

../_images/makekeys.png

点击导航中的“new file transfer”图标,如下图所示。

../_images/filetransfer.png

弹出窗口中忽视警告,确定后输入密码,在.ssh目录下执行“get id_rsa”命令,id_rsa将被保存到下图红线所示的本地目录中。

../_images/getkeys.png

在xshell菜单栏中依次点击“File”->“open”,选中你的session用户,并点击“Properties”,如下图所示。

../_images/reset1.png

做下图所示修改,点击“Browse”按钮。

../_images/reset2.png

点击import按钮,选择id_rsa,之后一路确定,再次登录是就可以不用再输入用户名和密码了。

../_images/reset3.png

ubuntu 安装说明

可以查看这个教程

http://wenku.baidu.com/course/view/4fdf50e2524de518964b7d00

搭建ftp 服务器

vsftp 快速配置

支持系统用户的快速配置:

# 默认配置文件: /etc/vsftpd.conf
#
#
#     下面是配置的选项及说明
#
#

######### 核心设置 ###########

# 允许本地用户登录
local_enable=YES

# 本地用户的写权限
write_enable=YES

# 使用FTP的本地文件权限,默认为077
# 一般设置为022
local_umask=022

# 切换目录时
# 是否显示目录下.message的内容
dirmessage_enable=YES

#验证方式
#pam_service_name=vsftpd

# 启用FTP数据端口的数据连接
connect_from_port_20=YES

# 以独立的FTP服务运行
listen=yes

# 修改连接端口
#listen_port=2121

######### 匿名登录设置 ###########

# 允许匿名登录
anonymous_enable=NO

# 如果允许匿名登录
# 是否开启匿名上传权限
#anon_upload_enable=YES

# 如果允许匿名登录
# 是否允许匿名建立文件夹并在文件夹内上传文件
#anon_mkdir_write_enable=YES

# 如果允许匿名登录
# 匿名帐号可以有删除的权限
#anon_other_write_enable=yes

# 如果允许匿名登录
# 匿名的下载权限
# 匿名为Other,可设置目录/文件属性控制
#anon_world_readable_only=no

# 如果允许匿名登录
# 限制匿名用户传输速率,单位bite
#anon_max_rate=30000

######### 用户限制设置 ###########

#### 限制登录

# 用userlist来限制用户访问
#userlist_enable=yes

# 名单中的人不允许访问
#userlist_deny=no

# 限制名单文件放置的路径
#userlist_file=/etc/vsftpd/userlist_deny.chroot

#### 限制目录

# 限制所有用户都在家目录
chroot_local_user=yes

# 调用限制在家目录的用户名单
#chroot_list_enable=YES

# 限制在家目录的用户名单所在路径
#chroot_list_file=/etc/vsftpd/chroot_list

######### 日志设置 ###########

# 日志文件路径设置
xferlog_file=/var/log/vsftpd.log

# 激活上传/下载的日志
xferlog_enable=YES

# 使用标准的日志格式
#xferlog_std_format=YES

######### 安全设置 ###########

# 用户空闲超时,单位秒
#idle_session_timeout=600

# 数据连接空闲超时,单位秒
#data_connection_timeout=120

# 将客户端空闲1分钟后断开
#accept_timeout=60

# 中断1分钟后重新连接
#connect_timeout=60

# 本地用户传输速率,单位bite
#local_max_rate=50000

# FTP的最大连接数
#max_clients=200

# 每IP的最大连接数
#max_per_ip=5

######### 被动模式设置 ###########

# 是否开户被动模式
pasv_enable=yes

# 被动模式最小端口
pasv_min_port=5000

# 被动模式最大端口
pasv_max_port=6000

######### 其他设置 ###########

# 欢迎信息
ftpd_banner=Welcome to Ftp Server!

ssh 代理使用

作者:
张卫

linux系统下的ssh代理使用方法

将SSH服务映射到本地端口来实现Socks代理服务器的功能

执行:

ssh -qTfnN -D 7070 xxx@x.x.x.x -p port

-D 加本地端口 xxx@x.x.x.x -p port 为ssh代理服务器

chrome可用Auto Switch Mode进行配置代理规则

../_images/switchysharp.png

windows系统ssh代理使用方法

SSH转Socks代理服务器工具:MyEnTunnel

点此下载 MyEnTunnel

使用MyEnTunnel将SSH服务映射到本地端口来实现Socks代理服务器的功能,默认socks代理的端口是7070,默认情况下是不启用代理的,需要选中“Enable Dynamic Socks”选项。

../_images/myentunnel.gif

vim

教程资源

  1. Learn Vimscript the Hard Way

git-flow

文章:

  1. http://www.jeffkit.info/2010/12/842/
  2. http://ihower.tw/blog/archives/5140/
  3. http://nvie.com/posts/a-successful-git-branching-model/
  4. git分支管理策略 http://www.ruanyifeng.com/blog/2012/07/git.html

git-flow 是什么?

github 主页:
https://github.com/nvie/gitflow

git flow是对 http://nvie.com/posts/a-successful-git-branching-model/ 这个分支模型的命令封装。

git flow定义了下列分支

主要分支:

  1. master: 永远在 production-ready 状态
  2. develop: 最新的下次发布开发状态
    支援性分支:

  3. Feature branches: 开发新功能都从 develop 分支出來,完成后 merge 回 develop

  4. Release branches: 准备要 release 的版本,只修 bugs。从 develop 分支出來,完成后 merge 回 master 和 develop
  5. Hotfix branches: 等不及 release 版本就必须马上修 master 上线的情況。会从 master 分支出來,完成后 merge 回 master 和 develop

安装git flow

wiki: https://github.com/nvie/gitflow/wiki/Installation

ubuntu 下官方源里就有:

sudo apt-get install git-flow

使用

初始化:

git flow init [-d]

-d 参数表示所有按默认的来,不加-d 会手动配置

λ ~/clojure/hello-world/ git flow init
Initialized empty Git repository in /home/lidashuang/clojure/hello-world/.git/
No branches exist yet. Base branches must be created now.
Branch name for production releases: [master]
Branch name for "next release" development: [develop]

How to name your supporting branch prefixes?
Feature branches? [feature/]
Release branches? [release/]
Hotfix branches? [hotfix/]
Support branches? [support/]
Version tag prefix? []

创建 feature/release/hotfix/support 分支

列出,开始,完成 feature 分支用下面的命令:

git flow feature
git flow feature start <name> [<base>]
git flow feature finish <name>

一个 feature完成 之后 ,会合并到develop 分支

对 feature 分支 push/pull:

git flow feature publish <name>
git flow feature pull <remote> <name>

release branches:

git flow release
git flow release start <release> [<base>]
git flow release finish <release>
For release branches, the <base> arg must be a commit on develop.

hotfix:

git flow hotfix
git flow hotfix start <release> [<base>]
git flow hotfix finish <release>
For hotfix branches, the <base> arg must be a commit on master.

branches:

git flow support
git flow support start <release> <base>
For support branches, the <base> arg must be a commit on master.

LVM (逻辑卷管理器)

作者:
zhwei http://zhangweide.cn

介绍

LVM Logical Volume Manager

LVM是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。

能解决那些问题

创建LVM

如果要在/dev/sda3, /dev/sda4, /dev/sda5上创建lvm

  1. 创建物理卷

    pvcreate /dev/sda{3..5}

  2. 创建为卷组

    vgcreate test0 /dev/sda{3..5}

  3. 在卷组test0上创建10G的逻辑卷 lv_0

    lvcreate -L 10G -n lv_0 test0

  4. 创建文件系统并挂载

    mkfs.ext4 /dev/test0/lv_0

扩容

需求: 将物理卷/dev/sda6 加入lvm并扩大为20G

  1. 创建物理卷

    pvcreate /dev/sda6

  2. 添加到卷组test0

    vgextend test0 /dev/sda6

  3. 扩展逻辑卷, 扩展到20G

    lvextend -L 20G /dev/test0/lv_0

  4. 使增加的容量生效

    resize2fs /dev/test0/lv_0

搞定

减容

需求: 将逻辑卷减小成10G

  1. 先将该分区卸载

    umount /data

  2. 检查逻辑卷

    e2fsck -f /dev/test0/lv_0

  3. 调整文件系统大小

    resize2fs /dev/test0/lv_0 10G

  4. 对逻辑卷进行调整

    lvreduce /dev/test0/lv_0 10G

删除lvm

lvremove /dev/test0/lv_0

vgremove /dev/test0

pvremove /dev/sda{3..5}

更多用法

$ sudo lvm

lvm> help
  Available lvm commands:
  Use 'lvm help <command>' for more information

  dumpconfig      Dump active configuration
  formats         List available metadata formats
  help            Display help for commands
  lvchange        Change the attributes of logical volume(s)
  lvconvert       Change logical volume layout
  lvcreate        Create a logical volume
  lvdisplay       Display information about a logical volume
  lvextend        Add space to a logical volume
  lvmchange       With the device mapper, this is obsolete and does nothing.
  lvmdiskscan     List devices that may be used as physical volumes
  lvmsadc         Collect activity data
  lvmsar          Create activity report
  lvreduce        Reduce the size of a logical volume
  lvremove        Remove logical volume(s) from the system
  lvrename        Rename a logical volume
  lvresize        Resize a logical volume
  lvs             Display information about logical volumes
  lvscan          List all logical volumes in all volume groups
  pvchange        Change attributes of physical volume(s)
  pvresize        Resize physical volume(s)
  pvck            Check the consistency of physical volume(s)
  pvcreate        Initialize physical volume(s) for use by LVM
  pvdata          Display the on-disk metadata for physical volume(s)
  pvdisplay       Display various attributes of physical volume(s)
  pvmove          Move extents from one physical volume to another
  pvremove        Remove LVM label(s) from physical volume(s)
  pvs             Display information about physical volumes
  pvscan          List all physical volumes
  segtypes        List available segment types
  vgcfgbackup     Backup volume group configuration(s)
  vgcfgrestore    Restore volume group configuration
  vgchange        Change volume group attributes
  vgck            Check the consistency of volume group(s)
  vgconvert       Change volume group metadata format
  vgcreate        Create a volume group
  vgdisplay       Display volume group information
  vgexport        Unregister volume group(s) from the system
  vgextend        Add physical volumes to a volume group
  vgimport        Register exported volume group with system
  vgmerge         Merge volume groups
  vgmknodes       Create the special files for volume group devices in /dev
  vgreduce        Remove physical volume(s) from a volume group
  vgremove        Remove volume group(s)
  vgrename        Rename a volume group
  vgs             Display information about volume groups
  vgscan          Search for all volume groups
  vgsplit         Move physical volumes into a new or existing volume group
  version         Display software and driver version information