Jinva's Blog

记录与分享

搭建简单的Git服务

安装 Git

1
$ sudo apt-get install git

接下来创建一个 git 用户组和用户,用来运行 git 服务:

1
2
3
$ sudo groupadd git
# 添加用户并使用 -g 指定用户组
$ sudo useradd git -g git

创建证书登录

收集所有需要登录的用户的公钥,公钥位于 id_rsa.pub 文件中,把我们的公钥导入到 /home/git/.ssh/authorized_keys 文件里,一行一个。

如果没有该文件创建它:

1
2
3
4
5
6
# 新建 git 用户目录,设置相关权限
$ cd /home/git/
$ mkdir .ssh
$ sudo chmod 755 .ssh
$ touch .ssh/authorized_keys
$ sudo chmod 644 .ssh/authorized_keys

初始化 Git 仓库

1
2
3
4
5
6
7
8
$ cd /data
$ mkdir gitrepo
$ sudo chown git:git gitrepo/
$ cd gitrepo

# 创建裸仓
$ git init --bare blog.git
Initialized empty Git repository in /data/gitrepo/blog.git/

以上命令 Git 创建一个空仓库,服务器上的Git仓库通常都以 .git 结尾。然后,把仓库所属用户改为 git:

1
$ sudo chown -R git:git blog.git

禁用 shell 登录

出于安全考虑,创建的 git 用户不允许登录 shell ,这可以通过编辑 /etc/passwd 文件完成。找到类似下面的一行:

1
git:x:1001:1001:,,,:/home/git:/bin/bash

改为

1
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

这样,git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出。

克隆仓库

1
2
3
4
5
# 请将 ip 和端口修改为自己的。
$ git clone ssh://git@192.168.1.1:22/data/gitrepo/blog.git
Cloning into 'runoob'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

Git 服务器搭建完成。

赞赏


Linux统计文件夹下的文件数目

Linux下有三个命令:​ls​、​grep​、​wc。通过这三个命令的组合可以统计目录下文件及文件夹的个数。

  • 统计当前目录下文件的个数(不包括目录)
1
$ ls -l | grep "^-" | wc -l
  • 统计当前目录下文件的个数(包括子目录)
1
$ ls -lR| grep "^-" | wc -l
  • 查看某目录下文件夹(目录)的个数(包括子目录)
1
$ ls -lR | grep "^d" | wc -l

命令解析:

  • ls -l

长列表输出该目录下文件信息(注意这里的文件是指目录、链接、设备文件等),每一行对应一个文件或目录,​ls -lR是列出所有文件,包括子目录。

  • grep "^-"​
    过滤​ls​的输出信息,只保留一般文件,只保留目录是​grep "^d"。
  • wc -l
    统计输出信息的行数,统计结果就是输出信息的行数,一行信息对应一个文件,所以就是文件的个数。

赞赏


Ubuntu 配置 Autoconnecting 蓝牙设备

操作系统:Ubuntu 16.04 LTS
软件:pulseaudio

每次连接蓝牙耳机之后,总是要手动选择音频输出,对于强迫症患者来说是极其难受的。

添加配置文件

添加配置文件

1
$ touch ~/.config/pulse/default.pa

在 ​default.pa 中追加或者添加代码

1
2
.include /etc/pulse/default.pa
load-module module-switch-on-connect

重启相关服务

1
2
3
4
# 重启 pulseaudio 服务
$ pulseaudio -k && pulseaudio --start
# 重启 bluetooth 服务
$ sudo /etc/init.d/bluetooth restart

完成,每当系统被唤醒或者重新连接蓝牙的时候,该配置会自动生效。

参考:

赞赏


Ubuntu 中使用 ntpdate 同步网络时间

问题

环境:Homestead > Ubuntu16.04 LTS

最近在测试腾讯云短信接口的时候返回错误信息:

1
# time offset too big, more then 600 seconds

社区问答:云短信“拉取单个手机短信接口“无法使用???

检查一遍代码,并没有问题,故查看了一下 Ubuntu 的系统时间

1
2
$ date
Mon Jul 16 22:27:23 CST 2018

对比了北京时间,发现出现了几个小时的时差,那么问题确定下来是服务器时间设置问题。

方法

安装 ntpdate 工具

1
$ sudo apt-get install ntpdate

设置系统时间与网络时间同步

1
$ ntpdate 0.centos.pool.ntp.org

将系统时间写入硬件时间

1
$ hwclock –systohc

再次使用 date 查看一下时间,时间同步了。
再次测试下短信接口,手机也收到验证码了。

赞赏


利用 Git-hooks 部署项目

设置远程仓库

1
2
# 转到裸仓目录(参考《搭建简单的Git服务》新建的裸仓)
$ cd /data/gitreop/blog.git

git 默认是禁止push的,所有要设置允许push:

1
$ vi config

修改或添加如下内容:

1
2
[receive]
      denyCurrentBranch = ignore

编辑自动部署脚本

自动部署用到git hooks,在git路径下有个hooks文件夹,里面有一些示例。我们把 post-update.sample 重命名为 post-update,并进行编辑:

1
2
$ mv post-update.sample post-update
$ vi post-update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".

#exec git update-server-info

unset GIT_DIR

NowPath=`pwd`

DeployPath="/data/www/blog"

cd $DeployPath
git pull origin master

cd $NowPath

echo 'deploy success'
exit 0

这样每当 push master 分支到服务器时,都会自动切换到 DeployPath,也就是 Nginx root 路径,执行 git pull origin master 从仓库拉去最新 master 分支。

部署路径初始化

先把空的仓库克隆到路径下,即为生产目录添加裸仓的URL。

1
2
$ cd /data/www
$ git clone /data/gitreop/blog.git

对于要部署的路径,要把所有者改为:git ,因为 git-hooks 需要该目录读写权限。

1
$ sudo chown -R git:git blog

至此,Git 自动部署已经设置完毕,每当在本地执行 git push 的时候,远程的 git-hooks 就会被自动触发。

赞赏


Shell 脚本中设置环境变量

正常情况下,向 ​~/.bashrc​ 文件添加环境变量,然后在终端执行 ​source ~/.bashrc 命令即可生效。

但如果想在 shell 脚本当中完成环境变量的添加,并且 在当前终端立马生效 的话就必须在执行脚本的时候使用 ​source ,下面举个例子:

以下是脚本 ​test.sh 的内容

1
2
3
  #!/bin/bash
 echo 'alias sa="some command"' >> ~/.bashrc
 source ~/.bashrc

执行脚本

1
$ ./test.sh

通常使用 . (source 缩写) 去执行的话,环境变量是不生效的;

必须使用 ​source 执行脚本,因为 shell 进程号和当前终端的进程号不一致。

使用 ​source 执行脚本

1
$ source test.sh

参考链接

赞赏


0%