0%

官方安装

下面是官网复制过来的官方安装方法,最简单的安装,在我大天朝,只能望天兴叹,你可翻墙安装或者略过这里,看下面的。

  1. 安装并配置必要的依赖项

If you install Postfix to send email please select ‘Internet Site’ during setup. Instead of using Postfix you can also use Sendmail or configure a custom SMTP server and configure it as an SMTP server.

On Centos 6 and 7, the commands below will also open HTTP and SSH access in the system firewall.

1
2
3
4
sudo yum install curl openssh-server openssh-clients postfix cronie
sudo service postfix start
sudo chkconfig postfix on
sudo lokkit -s http -s ssh
  1. 添加gitlab服务器包和安装包
1
2
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo yum install gitlab-ce

If you are not comfortable installing the repository through a piped script, you can find the entire script here and select and download the package manually and install using
gitlab/gitlab-ce

1
2
3
curl -LJO https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/6/gitlab-ce-XXX.rpm/download
curl -LJO https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm/download
rpm -i gitlab-ce-XXX.rpm
  1. 配置并启动GitLab
1
sudo gitlab-ctl reconfigure
  1. 浏览器打开并登录

On your first visit, you’ll be redirected to a password reset screen to provide the password for the initial administrator account. Enter your desired password and you’ll be redirected back to the login screen.

The default account’s username is root. Provide the password you created earlier and login. After login you can change the username if you wish.

第三方镜像安装

编辑源

新建 /etc/yum.repos.d/gitlab-ce.repo,内容为

使用清华大学 TUNA 镜像源 打开网址将内容复制到gitlab-ce.repo文件中,编辑路径vim /etc/yum.repos.d/gitlab-ce.repo

1
2
3
4
5
6
7
[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key

更新本地YUM缓存

1
sudo yum makecache

安装社区版

1
2
sudo yum install gitlab-ce #(自动安装最新版)
sudo yum install gitlab-ce-8.15.2-ce.0.el6 #(安装指定版本)

更改配置

1
2
3
vim /etc/gitlab/gitlab.rb
# 找到 external_url 'http://000.00.00.00:8081'
# 修改成你的地址

配置并启动GitLab

1
2
3
4
5
6
# 打开`/etc/gitlab/gitlab.rb`,
# 将`external_url = 'http://git.example.com'`修改为自己的IP地址:`http://xxx.xx.xxx.xx`,
# 然后执行下面的命令,对GitLab进行编译。
sudo gitlab-ctl reconfigure
# 清除缓存
sudo gitlab-rake cache:clear RAILS_ENV=production

登录GitLab

1
2
Username: root 
Password: 5iveL!fe

Docker安装

Docker 安装 Gitlab 教程

卸载

1
sudo gitlab-ctl uninstall

运维

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 修改默认的配置文件
sudo vim /etc/gitlab/gitlab.rb

# 查看版本
sudo cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
# echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
# sysctl -p
# echo never > /sys/kernel/mm/transparent_hugepage/enabled

# 检查gitlab
gitlab-rake gitlab:check SANITIZE=true --trace
gitlab-rake gitlab:check
gitlab-rake gitlab:check SANITIZE=true
# 查看日志
gitlab-ctl tail
# 数据库关系升级
gitlab-rake db:migrate
# 清理缓存
gitlab-rake cache:clear

# 更新gitlab包
yum update gitlab-ce

# 升级gitlab
yum install gitlab-ce

# 升级数据命令
gitlab-ctl pg-upgrade

服务管理

1
2
3
4
5
6
7
8
9
10
gitlab-ctl start # 启动所有 gitlab 组件:
gitlab-ctl stop # 停止所有 gitlab 组件:
gitlab-ctl stop postgresql # 停止所有 gitlab postgresql 组件:
# 停止相关数据连接服务
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
gitlab-ctl restart # 重启所有 gitlab 组件:
gitlab-ctl restart gitlab-workhorse # 重启所有 gitlab gitlab-workhorse 组件:
gitlab-ctl status # 查看服务状态
gitlab-ctl reconfigure # 生成配置启动服务

日志查看

1
2
3
4
5
6
7
8
sudo gitlab-ctl tail # 查看日志
sudo gitlab-ctl tail redis # 检查redis的日志
sudo gitlab-ctl tail postgresql # 检查postgresql的日志
sudo gitlab-ctl tail gitlab-workhorse # 检查gitlab-workhorse的日志
sudo gitlab-ctl tail logrotate # 检查logrotate的日志
sudo gitlab-ctl tail nginx # 检查nginx的日志
sudo gitlab-ctl tail sidekiq # 检查sidekiq的日志
sudo gitlab-ctl tail unicorn # 检查unicorn的日志

重置管理员密码

Gitlab管理员密码忘记,怎么重置密码,Gitlab 修改root用户密码,How to reset your root password

使用rails工具打开终端

1
sudo gitlab-rails console production

查询用户的email,用户名,密码等信息,id:1 表示root账号

1
user = User.where(id: 1).first

重新设置密码

1
2
user.password = '新密码'
user.password_confirmation = '新密码'

保存密码

1
user.save!

完整的操作ruby脚本

1
2
3
4
user = User.where(id: 1).first
user.password = '新密码'
user.password_confirmation = '新密码'
user.save!

备份恢复

使用Gitlab一键安装包安装Gitlab非常简单, 同样的备份恢复与迁移也非常简单,用一条命令即可创建完整的Gitlab备份:

修改备份文件默认目录

修改/etc/gitlab/gitlab.rb来修改默认存放备份文件的目录:

1
gitlab_rails['backup_path'] = '/mnt/backups'

创建备份

1
gitlab-rake gitlab:backup:create

以上命令将在 /var/opt/gitlab/backups 目录下创建一个名称类似为xxxxxxxx_gitlab_backup.tar的压缩包, 这个压缩包就是Gitlab整个的完整部分, 其中开头的xxxxxx是备份创建的时间戳。

修改后使用gitlab-ctl reconfigure命令重载配置文件。

开始备份

这里放你的备份文件文件夹,和仓库源文件。

1
2
/var/opt/gitlab/backups                   # 备份文件文件夹
/var/opt/gitlab/git-data/repositories # git仓库源文件

自动备份

通过crontab使用备份命令实现自动备份

1
2
3
4
crontab -e
# 每天2点备份gitlab数据
0 2 * * * /usr/bin/gitlab-rake gitlab:backup:create
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create

上面两行保存之后,重新载入配置

1
2
3
service crond reload
# or
systemctl reload crond.service

备份保留七天

设置只保存最近7天的备份,编辑 /etc/gitlab/gitlab.rb 配置文件,找到如下代码,删除注释 # 保存

1
2
# /etc/gitlab/gitlab.rb 配置文件 修改下面这一行
gitlab_rails['backup_keep_time'] = 604800

重新加载gitlab配置文件

1
sudo gitlab-ctl reconfigure

开始恢复

迁移如同备份与恢复的步骤一样, 只需要将老服务器 /var/opt/gitlab/backups 目录下的备份文件拷贝到新服务器上的 /var/opt/gitlab/backups 即可(如果你没修改过默认备份目录的话)。 然后执行恢复命令。
如果修改了,首先进入备份 gitlab 的目录,这个目录是配置文件中的 gitlab_rails['backup_path'] ,默认为 /var/opt/gitlab/backups

然后停止 unicorn 和 sidekiq ,保证数据库没有新的连接,不会有写数据情况。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 停止相关数据连接服务
gitlab-ctl stop unicorn
# ok: down: unicorn: 0s, normally up
gitlab-ctl stop sidekiq
# ok: down: sidekiq: 0s, normally up

# 从xxxxx编号备份中恢复
# 然后恢复数据,1406691018为备份文件的时间戳
gitlab-rake gitlab:backup:restore BACKUP=1406691018

# 新版本 1483533591_2017_01_04_gitlab_backup.tar
gitlab-rake gitlab:backup:restore BACKUP=1483533591_2017_01_04_gitlab_backup.tar

# 启动Gitlab
sudo gitlab-ctl start

判断是执行实际操作的gitlab相关用户:git,没有得到足够的权限。依次执行命令:

1
2
3
4
5
6
7
8
9
10
# 恢复过程中没有权限
mkdir /var/opt/gitlab/backups
chown git /var/opt/gitlab/backups
chmod 700 /var/opt/gitlab/backups

# 恢复成功页面报没有权限的错误
sudo chown -R git:git /var/opt/gitlab/git-data/repositories
sudo chmod -R ug+rwX,o-rwx /var/opt/gitlab/git-data/repositories
sudo chmod -R ug-s /var/opt/gitlab/git-data/repositories
sudo find /var/opt/gitlab/git-data/repositories -type d -print0 | sudo xargs -0 chmod g+s

如果备份文件报没有权限,通过ls -al查看权限是不是git,而不是root,通过下面方式给git用户权限

1
sudo chown -R git:git 1483533591_2017_01_04_gitlab_backup.tar

连接数据库

1
2
3
4
5
6
# 登陆gitlab的安装服务查看配置文件
cat /var/opt/gitlab/gitlab-rails/etc/database.yml

vim /var/opt/gitlab/postgresql/data/postgresql.conf
# listen_addresses = '192.168.1.125' # 修改监听地址为ip
# 或者改为 "*"

修改 pg_hba.conf 配置

1
2
3
vim  /var/opt/gitlab/postgresql/data/pg_hba.conf
# 将下面这一行添加到配置的最后面
# host all all 0.0.0.0/0 trust

如果不希望允许所有IP远程访问,则可以将上述配置项中的0.0.0.0设定为特定的IP值。

重启 postgresql 数据库

1
gitlab-ctl restart postgresql

查看 /etc/passwd 文件里边 gitlab 对应的系统用户

1
2
3
[root@localhost ~]$ cat /etc/passwd
...
gitlab-psql:x:493:490::/var/opt/gitlab/postgresql:/bin/sh # gitlab的postgresql用户

一些常规目录

1
2
3
4
5
6
# 配置目录
/etc/gitlab/gitlab.rb
# 生成好的nginx配置
/var/opt/gitlab/nginx/conf/gitlab-http.conf
# 备份目录
/var/opt/gitlab/backups

使用HTTPS

直接将nginx配置复制到你自己的nginx配置中,停掉gitlab的nginx

1
cp /var/opt/gitlab/nginx/conf/gitlab-http.conf /usr/local/nginx/conf/vhost/

将你的SSL证书配置复制进去

1
2
3
4
5
6
7
server {
listen 443 ssl;
server_name g.doman.cn;
ssl_certificate /etc/letsencrypt/live/*****/certificate.crt;
ssl_certificate_key /etc/letsencrypt/live/*****/private.key;
# .....
}

编辑vi /usr/local/nginx/conf/nginx.conf你的nginx配置,引用你复制过来的配置。

1
2
3
4
http {
# .....
include vhost/gitlab-http.conf;
}

同时要把/var/opt/gitlab/nginx/conf/nginx.conf中的一些变量复制到自己的nginx配置中nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
http {
# .....
log_format gitlab_access '$remote_addr - $remote_user [$time_local] "$request_method $filtered_request_uri $server_protocol" $status $body_bytes_sent "$filtered_http_referer" "$http_user_agent"';
log_format gitlab_mattermost_access '$remote_addr - $remote_user [$time_local] "$request_method $filtered_request_uri $server_protocol" $status $body_bytes_sent "$filtered_http_referer" "$http_user_agent"';

proxy_cache_path proxy_cache keys_zone=gitlab:10m max_size=1g levels=1:2;
proxy_cache gitlab;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

# Remove private_token from the request URI
# In: /foo?private_token=unfiltered&authenticity_token=unfiltered&rss_token=unfiltered&...
# Out: /foo?private_token=[FILTERED]&authenticity_token=unfiltered&rss_token=unfiltered&...
map $request_uri $temp_request_uri_1 {
default $request_uri;
~(?i)^(?<start>.*)(?<temp>[\?&]private[\-_]token)=[^&]*(?<rest>.*)$ "$start$temp=[FILTERED]$rest";
}
# Remove authenticity_token from the request URI
# In: /foo?private_token=[FILTERED]&authenticity_token=unfiltered&rss_token=unfiltered&...
# Out: /foo?private_token=[FILTERED]&authenticity_token=[FILTERED]&rss_token=unfiltered&...
map $temp_request_uri_1 $temp_request_uri_2 {
default $temp_request_uri_1;
~(?i)^(?<start>.*)(?<temp>[\?&]authenticity[\-_]token)=[^&]*(?<rest>.*)$ "$start$temp=[FILTERED]$rest";
}
# Remove rss_token from the request URI
# In: /foo?private_token=[FILTERED]&authenticity_token=[FILTERED]&rss_token=unfiltered&...
# Out: /foo?private_token=[FILTERED]&authenticity_token=[FILTERED]&rss_token=[FILTERED]&...
map $temp_request_uri_2 $filtered_request_uri {
default $temp_request_uri_2;
~(?i)^(?<start>.*)(?<temp>[\?&]rss[\-_]token)=[^&]*(?<rest>.*)$ "$start$temp=[FILTERED]$rest";
}
# A version of the referer without the query string
map $http_referer $filtered_http_referer {
default $http_referer;
~^(?<temp>.*)\? $temp;
}
}

暴力升级

暴力升级前先备份,然后停止所有服务运行,记得备份的良好习惯

1
2
3
gitlab-ctl stop  # 停止所有 gitlab 组件:
# 更新gitlab包
yum update gitlab-ce

直接编辑源 /etc/yum.repos.d/gitlab-ce.repo,安装 GitLab 社区版

1
2
3
yum list gitlab-ce # 查看版本
sudo yum install gitlab-ce #(自动安装最新版)
sudo yum install gitlab-ce-8.15.2-ce.0.el6 #(安装指定版本)

注意:10.7 版本升级到 11.x 版本需要先升级到 10.8 版本

1
2
# 安装指定版本 10.8 的版本
sudo yum install gitlab-ce-10.8.0-ce.0.el6

安装完成记得将所有服务启起来哦

1
2
3
gitlab-ctl start # 启动所有数据库
# postgresql 数据库如果启动不了,通过重启启动
gitlab-ctl restart postgresql

安装过如果报错,查看提示根据提示操作,版本跨度太大会报错哦。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
gitlab preinstall: Automatically backing up only the GitLab SQL database (excluding everything else!)
Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... pg_dump: [archiver (db)] connection to database "gitlabhq_production" failed: could not connect to server: 没有那个文件或目录
Is the server running locally and accepting
connections on Unix domain socket "/var/opt/gitlab/postgresql/.s.PGSQL.5432"?
Backup failed
[FAILED]
gitlab preinstall:
gitlab preinstall: Backup failed! If you want to skip this backup, run the following command and
gitlab preinstall: try again:
gitlab preinstall:
gitlab preinstall: sudo touch /etc/gitlab/skip-auto-migrations
gitlab preinstall:
error: %pre(gitlab-ce-8.15.2-ce.0.el6.x86_64) scriptlet failed, exit status 1
Error in PREIN scriptlet in rpm package gitlab-ce-8.15.2-ce.0.el6.x86_64
error: install: %pre scriptlet failed (2), skipping gitlab-ce-8.15.2-ce.0.el6
gitlab-ce-8.11.5-ce.0.el6.x86_64 was supposed to be removed but is not!
Verifying : gitlab-ce-8.11.5-ce.0.el6.x86_64 1/2
Verifying : gitlab-ce-8.15.2-ce.0.el6.x86_64 2/2

Failed:
gitlab-ce.x86_64 0:8.11.5-ce.0.el6

看上面一堆错误,瞬间就懵逼了,看到一条救星命令让我尝试运行 sudo touch /etc/gitlab/skip-auto-migrations 于是我二逼的重新yum install gitlab-ce运行了,结果真的安装成功了,😄。

1
2
3
4
# 重新安装命令
yum reinstall gitlab-ce
# or
yum install gitlab-ce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
...
gitlab: Thank you for installing GitLab!
gitlab: To configure and start GitLab, RUN THE FOLLOWING COMMAND:

sudo gitlab-ctl reconfigure

gitlab: GitLab should be reachable at http://114.55.148.71:8081
gitlab: Otherwise configure GitLab for your system by editing /etc/gitlab/gitlab.rb file
gitlab: And running reconfigure again.
gitlab:
gitlab: For a comprehensive list of configuration options please see the Omnibus GitLab readme
gitlab: https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md
gitlab:

gitlab: GitLab now ships with a newer version of PostgreSQL (9.6.1), and will be used
gitlab: as the default in the next major relase. To upgrade, RUN THE FOLLOWING COMMANDS:

sudo gitlab-ctl pg-upgrade

gitlab: For more details, please see:
gitlab: https://docs.gitlab.com/omnibus/settings/database.html#upgrade-packaged-postgresql-server
gitlab:
清理 : gitlab-ce-8.11.5-ce.0.el6.x86_64 2/2
Found /etc/gitlab/skip-auto-migrations, exiting...
Verifying : gitlab-ce-8.15.2-ce.0.el6.x86_64 1/2
Verifying : gitlab-ce-8.11.5-ce.0.el6.x86_64 2/2

更新完毕:
gitlab-ce.x86_64 0:8.15.2-ce.0.el6

完毕!

重启配置,可以解决大部分502错误。

1
gitlab-ctl reconfigure

优化内存使用

修改配置文件 /etc/gitlab/gitlab.rb

1
2
3
4
5
6
7
8
9
10
11
# 减少 postgresql 数据库缓存
postgresql['shared_buffers'] = "256MB"
# 减少sidekiq的并发数
sidekiq['concurrency'] = 1

# worker进程数
postgresql['max_worker_processes'] = 4

unicorn['worker_processes'] = 2 ## worker进程数
unicorn['worker_memory_limit_min'] = "400 * 1 << 20" ##worker最小内存
unicorn['worker_memory_limit_max'] = "650 * 1 << 20" ##worker最大内存

错误处理

解决80端口被占用

nginx配置解决 80 端口被占用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
upstream gitlab {
server 114.55.111.111:8081 ;
}
server {
# 侦听的80端口
listen 80;
server_name git.diggg.cn;
location / {
proxy_pass http://gitlab; #在这里设置一个代理,和upstream的名字一样
#以下是一些反向代理的配置可删除
proxy_redirect off;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout 300; #nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 300; #后端服务器数据回传时间(代理发送超时)
proxy_read_timeout 300; #连接成功后,后端服务器响应时间(代理接收超时)
proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
}
}

nginx配置检查和立即生效

1
2
3
4
# 检查配置
/usr/local/nginx/sbin/nginx -tc conf/nginx.conf
# nginx 重新加载配置
/usr/local/nginx/sbin/nginx -s reload

头像无法正常显示

原因:gravatar被墙
解决办法:
编辑 /etc/gitlab/gitlab.rb,将

1
# gitlab_rails['gravatar_plain_url'] = 'http://gravatar.duoshuo.com/avatar/%{hash}?s=%{size}&d=identicon'

修改为:

1
gitlab_rails['gravatar_plain_url'] = 'http://gravatar.duoshuo.com/avatar/%{hash}?s=%{size}&d=identicon'

然后在命令行执行:

1
2
sudo gitlab-ctl reconfigure 
sudo gitlab-rake cache:clear RAILS_ENV=production

internal API unreachable

这个错误是一个自己制造的坑,我克隆和提交都没有办法搞,但是网站能正常运行,尝试了非常多的方法,最终我的问题是22端口没有隐射出去,好尴尬。

1
GitLab: Failed to authorize your Git request: internal API unreachable

解决办法:https://gitlab.com/gitlab-org/gitlab-ce/issues/33702
通过防火墙规则 127.0.0.1

proxy_temp 目录没有权限

1
[crit] 14788#0: *215 open() "/usr/local/nginx/proxy_temp/5/01/0000000015" failed (13: Permission denied) while reading upstream

以下方式解决

1
2
3
4
5
chown -R root:root /usr/local/nginx/proxy_temp
# 编辑 nginx.conf
sudo vi /usr/local/nginx/conf/nginx.conf
# 在第一行添加
user root;

webhooks 错误

错误显示不允许发送本地请求

1
Url is blocked: Requests to the local network are not allowed

解决方法,在设置中设置允许本地连接即可

admin => Settings => Outbound requests

服务无法启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost gitlab]# gitlab-ctl status
fail: alertmanager: runsv not running
fail: gitaly: runsv not running
fail: gitlab-monitor: runsv not running
fail: gitlab-workhorse: runsv not running
fail: logrotate: runsv not running
fail: nginx: runsv not running
fail: node-exporter: runsv not running
fail: postgres-exporter: runsv not running
fail: postgresql: runsv not running
fail: prometheus: runsv not running
fail: redis: runsv not running
fail: redis-exporter: runsv not running
fail: sidekiq: runsv not running
fail: unicorn: runsv not running


Omnibus gitlab do not restart on CentOS7
开机自动启动服务

1
2
3
4
[root@localhost ~]# systemctl status gitlab-runsvdir.service -l
● gitlab-runsvdir.service - GitLab Runit supervision process
Loaded: loaded (/usr/lib/systemd/system/gitlab-runsvdir.service; enabled; vendor preset: disabled)
Active: inactive (dead)

如果 gitlab-runsvdir.service 服务没有响应,你可能要看一下内存是否满了,需要释放内存,老的版本需要 2G 内存,新版本需要至少 4G 内存。

其它错误

1
Error executing action `run` on resource 'bash[migrate gitlab-rails database]'

上面错误是数据库没有启动,我不知道如何启动,我重启了服务器,然后好球了。😆
https://gitlab.com/gitlab-org/gitlab-ce/issues/2052#note_1667899

1
NameError: uninitialized constant Devise::Async
1
2
Processing by RootController#index as HTML
Completed 401 Unauthorized in 17ms (ActiveRecord: 2.7ms)
1
2
/var/log/gitlab/nginx/gitlab_access.log <==
114.55.148.71 - - [04/Jan/2017:17:20:24 +0800] "GET /favicon.ico HTTP/1.0" 502 2662 "http://git.xxxxx.cn/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36"

参考资料

登录

username:root
password:安装时设置的密码

其它终端登录 $ ssh root@192.168.0.23

系统配置情况命令

系统

1
2
3
4
5
6
7
8
9
10
cat /etc/redhat-release # CentOS 查看系统信息
uname -a # 查看内核/操作系统/CPU信息
head -n 1 /etc/issue # 查看操作系统版本
cat /proc/cpuinfo # 查看CPU信息
hostname # 查看计算机名
lspci -tv # 列出所有PCI设备
lsusb -tv # 列出所有USB设备
lsmod # 列出加载的内核模块
env # 查看环境变量
dmidecode | grep "Product Nmae" #查看服务器型号

资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
free -m                # 查看内存使用量和交换区使用量
df -h # 查看各分区使用情况
du -sh <目录名> # 查看指定目录的大小
grep MemTotal /proc/meminfo # 查看内存总量
grep MemFree /proc/meminfo # 查看空闲内存量
uptime # 查看系统运行时间、用户数、负载
cat /proc/loadavg # 查看系统负载
# 查看内存的插槽数,已经使用多少插槽。每条内存多大,已使用内存多大
dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range
# 查看内存支持的最大内存容量
dmidecode|grep -P 'Maximum\s+Capacity'
# 查看内存的频率
dmidecode|grep -A16 "Memory Device"
dmidecode|grep -A16 "Memory Device"|grep 'Speed'

磁盘和分区

1
2
3
4
5
mount | column -t      # 查看挂接的分区状态
fdisk -l # 查看所有分区
swapon -s # 查看所有交换分区
hdparm -i /dev/hda # 查看磁盘参数(仅适用于IDE设备)
dmesg | grep IDE # 查看启动时IDE设备检测状况

网络

1
2
3
4
5
6
ifconfig               # 查看所有网络接口的属性
iptables -L # 查看防火墙设置
route -n # 查看路由表
netstat -lntp # 查看所有监听端口
netstat -antp # 查看所有已经建立的连接
netstat -s # 查看网络统计信息

进程查看

1
2
ps -ef                 # 查看所有进程
top # 实时显示进程状态

系统时间

UTC: 整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated)。
GMT: 格林威治标准时间 (Greenwich Mean Time)指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。(UTC与GMT时间基本相同)
CST: 中国标准时间 (China Standard Time)。GMT + 8 = UTC + 8 = CST
DST: 夏令时(Daylight Saving Time) 指在夏天太阳升起的比较早时,将时钟拨快一小时,以提早日光的使用。(中国不使用)
硬件时钟: RTC(Real-Time Clock)或CMOS时钟,一般在主板上靠电池供电,服务器断电后也会继续运行。仅保存日期时间数值,无法保存时区和夏令时设置。
系统时钟: 一般在服务器启动时复制RTC时间,之后独立运行,保存了时间、时区和夏令时设置。

1
2
3
4
5
6
7
8
9
10
11
12
13
timedatectl  # 等同于 timedatectl status
timedatectl set-time "YYYY-MM-DD HH:MM:SS" # 设置时间
timedatectl list-timezones # 列出所有时区
timedatectl set-timezone Asia/Shanghai # 设置时区
timedatectl set-ntp yes # 是否NTP服务器同步, yes或者no

# 将硬件时钟调整为与本地时钟一致
timedatectl set-local-rtc 1
hwclock --systohc --localtime # 与上面命令效果一致

# 硬件时间设置成 UTC
timedatectl set-local-rtc 1
hwclock --systohc --utc //与上面命令效果一致

用户

1
2
3
4
5
6
w                         # 查看活动用户
id <用户名> # 查看指定用户信息
last # 查看用户登录日志
cut -d: -f1 /etc/passwd # 查看系统所有用户
cut -d: -f1 /etc/group # 查看系统所有组
crontab -l # 查看当前用户的计划任务

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# 查看网络接口统计数据的,两种发放
ip link # 或者下面方法
ip -s link

yum install net-tools # net-tools包提供了ifconfig命令
ifconfig -a # 查看IP地址
ip addr # 查看IP地址
route -n # 使用最快的速度查找主机的路由

cat /proc/version # 查看系统信息
uname -a # 方法二
uname -r #方法三
getconf LONG_BIT # 查看系统是64位还是32位


uname -a # 查看内核/操作系统/CPU信息
head -n 1 /etc/issue #查看操作系统版本
cat /proc/cpuinfo #查看CPU信息
hostname #查看计算机名
lspci -tv #列出所有PCI设备
lsusb -tv #列出所有USB设备
lsmod #列出加载的内核模块
env #查看环境变量
arch # 显示机器的处理器架构(1)
uname -m # 显示机器的处理器架构(2)
uname -r # 显示正在使用的内核版本
dmidecode -q # 显示硬件系统部件
hdparm -i /dev/hda # 罗列一个磁盘的架构特性
hdparm -tT /dev/sda # 在磁盘上执行测试性读取操作
cat /proc/interrupts # 显示中断
cat /proc/meminfo # 校验内存使用
cat /proc/swaps # 显示哪些swap被使用
cat /proc/version # 显示内核的版本
cat /proc/net/dev # 显示网络适配器及统计
cat /proc/mounts # 显示已加载的文件系统
lspci -tv # 罗列 PCI 设备
lsusb -tv # 显示 USB 设备
date # 显示系统日期
date 041217002007.00 # 设置日期和时间 – 月日时分年.秒
cal 2007 # 显示2007年的日历表
clock -w # 将时间修改保存到 BIOS

系统的关机、重启以及登出

1
2
3
4
5
6
7
8
shutdown -h now # 关闭系统(1)   
init 0 # 关闭系统(2)
telinit 0 # 关闭系统(3)
shutdown -h hours:minutes & #按预定时间关闭系统
shutdown -c #取消按预定时间关闭系统
shutdown -r now # 重启 (1)
reboot #重启 (2)
logout # 注销

查看网络配置的命令

1
2
3
4
5
6
ifconfig # 查看所有网络接口的属性   
iptables -L # 查看防火墙设置
route -n # 查看路由表
netstat -lntp # 查看所有监听端口
netstat -antp # 查看所有已经建立的连接
netstat -s # 查看网络统计信息

查看linux进程

1
2
3
ps -aux | grep node # 查看`node`进程  
ps -ef # 查看所有进程
top # 实时显示进程状态

杀进程

1
2
3
killall -9 websocket # 干掉`websocket`服务进程  
ps aux | grep mysql # 查看mysql进程
kill -9 35562 # 根据进程号杀

查看用户的命令

1
2
3
4
5
6
w # 查看活动用户   
id <用户名> # 查看指定用户信息
last # 查看用户登录日志
cut -d: -f1 /etc/passwd # 查看系统所有用户
cut -d: -f1 /etc/group # 查看系统所有组
crontab -l # 查看当前用户的计划任务

log日志查看

1
2
3
4
cat /var/log/messages # 查询日志的全部内容
head -5 /var/log/messages # 查询日志的前5行
tail -5 /var/log/messages # 查询日志的最新5行
sed -n '5,10p' /var/log/messages # 查询日志的5到10行

查看系统服务的命令

1
2
chkconfig –list # 列出所有系统服务   
chkconfig –list | grep on # 列出所有启动的系统服务

安装程序的命令

rpm -qa 查看所有安装的软件包

获取帮助的命令

man <命令> 获得命令帮助

安装软件方法

1
2
3
4
5
6
7
8
9
10
# 安装下载工具wget
$ yum install wget

# 解压
$ wget https://nodejs.org/dist/v4.4.4/node-v4.4.4-linux-x64.tar.xz
https://nodejs.org/dist/v4.4.5/node-v4.4.5-linux-x64.tar.xz

# 测试安装
# 没有用到`gzip`压缩去掉`z`参数
$ sudo tar --strip-components 1 -xzvf node-v* -C /usr/local

yum错误

yum错误:Cannot retrieve repository metadata (repomd.xml) for repository解决方法

1
2
cd /etc/yum.repos.d/
ls

找到yum.repos.d这个目录,里面有个文件,以.repo 结尾的,例如zl.repo删除
然后#yum clean all

安装源

http://dl.fedoraproject.org/pub/
http://rpms.remirepo.net/enterprise/

下载

www.centos.org

http://mirror.neu.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://centos.ustc.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://mirrors.163.com/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://mirrors.hust.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://mirrors.zju.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://mirrors.cqu.edu.cn/CentOS/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://mirrors.cug.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://mirrors.neusoft.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://mirrors.skyshe.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://mirrors.aliyun.com/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://mirrors.nwsuaf.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://mirror.bit.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://mirror.lzu.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://ftp.sjtu.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://mirrors.yun-idc.com/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
http://mirrors.pubyun.com/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso

安装配置vsftpd做FTP服务,我们的项目应用使用git管理进行迭代,公共文件软件存储使用开源网盘Seafile来管理,基本够用。想不到FTP的使用的场景,感觉它好像老去了,虽然现在基本没有用到这个工具,刚好公司公司刷一个硬件需要使用FTP来配置下载文件,于是研究使用了一下,记录了一下使用过程。😀

安装

在安装前查看是否已安装vsftpd

1
2
3
4
5
6
7
8
9
10
# 查看是否已安装 方法一
[root@localhost ~]# rpm -q vsftpd
vsftpd-3.0.2-21.el7.x86_64

# 查看是否已安装 方法二
[root@localhost ~]# vsftpd -v
vsftpd: version 3.0.2

# 安装 vsftpd
[root@localhost ~]# yum -y install vsftpd

查看位置

1
2
[root@localhost ~]# whereis vsftpd
vsftpd: /usr/sbin/vsftpd /etc/vsftpd /usr/share/man/man8/vsftpd.8.gz

启动vsftpd服务

1
systemctl start vsftpd.service

关闭firewall和SELinux

1
2
3
4
5
6
7
8
9
10
11
12
13
setenforce 0   # 设置SELinux 成为permissive模式  (关闭SELinux)
setenforce 1 # 设置SELinux 成为enforcing模式 (开启SELinux)

# 或者修改配置
vi /etc/selinux/config
# SELINUX=enforcing
# 注释掉
# SELINUXTYPE=targeted
# 注释掉
SELINUX=disabled
# 增加
:wq! #保存退出
setenforce 0

或者设置SELinux

1
2
3

getsebool -a | grep ftp
setsebool -P ftpd_full_access on
1
2
3
4
systemctl stop firewalld.service
#停止firewall
systemctl disable firewalld.service
#禁止firewall开机启动

如果你不愿意关闭防火墙,需要防火墙添加FTP服务。

1
2
firewall-cmd --permanent --zone=public --add-service=ftp
firewall-cmd --reload

修改配置文件

配置文件/etc/vsftpd/vsftpd.conf

1
2
3
4
5
6
7
8
9
anonymous_enable=NO        # 不允许匿名访问,禁用匿名登录
chroot_local_user=YES # 启用限定用户在其主目录下
use_localtime=YES # 使用本地时(自行添加)
chroot_list_enable=YES
local_enable=YES # 允许使用本地帐户进行FTP用户登录验证
allow_writeable_chroot=YES # 如果启用了限定用户在其主目录下需要添加这个配置,解决报错 500 OOPS: vsftpd: refusing to run with writable root inside chroot()
xferlog_enable=YES # 启用上传和下载的日志功能,默认开启。
local_umask=022 # 设置本地用户默认文件掩码022
# FTP上本地的文件权限,默认是077,不过vsftpd安装后的配置文件里默认是022

虚拟用户高级参数

1
2
3
4
5
6
7
8
9
10
11
12
13
当virtual_use_local_privs=YES 时,虚拟用户和本地用户有相同的权限;
当virtual_use_local_privs=NO 时,虚拟用户和匿名用户有相同的权限,默认是NO。
当virtual_use_local_privs=YES,write_enable=YES时,虚拟用户具有写权限(上传、下载、删除、重命名)。
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=YES,
anon_upload_enable=YES时,虚拟用户不能浏览目录,只能上传文件,无其他权限。
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=NO,
anon_upload_enable=NO时,虚拟用户只能下载文件,无其他权限。
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=NO,
anon_upload_enable=YES时,虚拟用户只能上传和下载文件,无其他权限。
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=NO,
anon_mkdir_write_enable=YES时,虚拟用户只能下载文件和创建文件夹,无其他权限。
当virtual_use_local_privs=NO,write_enable=YES,anon_world_readable_only=NO,
anon_other_write_enable=YES时,虚拟用户只能下载、删除和重命名文件,无其他权限。

匿名登录

安装完默认情况下是开启匿名登录的,对应的是 /var/ftp 目录,这时只要服务启动了,就可以直接连上FTP了。默认用户名是ftp,密码是空的。如果你在配置里面配置了anonymous_enable=NO,匿名就无法登录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ ftp 192.168.188.114

Connected to 192.168.188.114.
220 (vsFTPd 3.0.2)
Name (192.168.188.114:kennywang): ftp
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
229 Entering Extended Passive Mode (|||47867|).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 12 Jan 18 06:31 README.md
drwxr-xr-x 2 0 0 6 Nov 05 19:43 pub
226 Directory send OK.

多用户配置

多用户配置需要自己手工添加配置,下面内容到vsftpd.conf末尾

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 
#

use_localtime=YES # 使用本地时(自行添加)
listen_port=21
chroot_local_user=YES # 启用限定用户在其主目录下
idle_session_timeout=300

data_connection_timeout=120 # 数据连接超时时间
guest_enable=YES # 设定启用虚拟用户功能
guest_username=ftpuser # 指定虚拟用户的宿主用户 ftpuser(就是我们后面会新建这个用户)
# guest_username=www
# 如果ftp目录是指向网站根目录,用来上传网站程序,
# 可以指定虚拟用户的宿主用户为nginx运行账户www,可以避免很多权限设置问题


user_config_dir=/etc/vsftpd/vuser_conf # 虚拟用户配置文件目录
virtual_use_local_privs=YES # NO时,虚拟用户和匿名用户有相同的权限,默认是NO

pasv_min_port=10060 # 被动模式最小端口号10060
pasv_max_port=10090 # 被动模式最大端口号10090

accept_timeout=5
connect_timeout=1

创建宿主用户

新建系统用户ftpuser,用户目录为/home/vsftpd, 用户登录终端设为/bin/false(即使之不能登录系统)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 方法一
# 创建用户 ftpuser 指定 `/home/vsftpd` 目录
useradd -g root -M -d /home/vsftpd -s /sbin/nologin ftpuser

# 设置用户 ftpuser 的密码
passwd ftpuser
# 把 /home/vsftpd 的所有权给ftpuser.root
chown -R ftpuser.root /home/vsftpd

# 方法二
useradd ftpuser -d /home/vsftpd -s /bin/false
chown ftpuser:ftpuser /home/vsftpd -R

# 如果虚拟用户的宿主用户为www,需要这样设置
# www目录是你应用的目录
chown www:www /home/www -R

删除用户 userdel ftpuser

建立虚拟用户文件

1
2
3
4
5
6
7
8
9
10
touch /etc/vsftpd/vuser_passwd
# 编辑虚拟用户名单文件:(
# 第一行账号,第二行密码,注意:不能使用root做用户名,系统保留)
vi /etc/vsftpd/vuser_passwd
# 编辑内容,下面是 vuser_passwd 内容
wcj
123456
hss
123456
#保存退出

生成虚拟用户数据文件

1
2
db_load -T -t hash -f /etc/vsftpd/vuser_passwd /etc/vsftpd/vuser_passwd.db
chmod 600 /etc/vsftpd/vuser_passwd.db

创建用户配置

1
2
3
mkdir /etc/vsftpd/vuser_conf  # 建立虚拟用户个人vsftp的配置文件
cd /etc/vsftpd/vuser_conf # 进入目录
touch hss wcj # 这里创建两个虚拟用户配置文件

每一个文件配置文件都差不多,只是参数local_root不一样。

1
2
3
4
5
6
local_root=/home/vsftpd/hss   # 用户 hss 配置目录,这个地方不一样
write_enable=YES # 允许本地用户对FTP服务器文件具有写权限
anon_world_readable_only=NO
anon_upload_enable=YES # 允许匿名用户上传文件(须将全局的write_enable=YES,默认YES)
anon_mkdir_write_enable=YES # 允许匿名用户创建目录
anon_other_write_enable=YES # 允许匿名用户删除和重命名权限(自行添加)

创建用户目录

每个用户目录文件夹是有root用户创建的,也就是上面local_root配置目录,其权限应设置为755。因为权限的问题在该文件夹内无法直接上传文件。而如果设置为777则无法访问,这是由于vsftpd的安全性设置。解决上传问题的方法是在local_root文件夹内新建一个upload的文件夹,权限设置为777,可将文件上传到该文件夹。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mkdir -p /home/vsftpd/hss     # 每个用户对于一个目录,创建两个目录“hss”、“wcj”

# 下面是目录结构
/home/vsftpd
├── hss
│   ├── filename.md
│   └── upload
└── wcj
└── filename.md


# 赋予其权限
chmod -R 777 /var/vsftpd/hss/upload/

# 在/var/ftp下新建一个目录来实现匿名用户上传
mkdir /var/ftp/upload

vsftpd中几种用户的区分:

本地用户:用户在FTP服务器拥有账号,且该账号为本地用户的账号,可以通过自己的账号和口令进行授权登录,登录目录为自己的home目录$HOME
虚拟用户:用户在FTP服务器上拥有账号,但该账号只能用于文件传输服务。登录目录为某一特定的目录,通常可以上传和下载
匿名用户:用户在FTP服务器上没有账号,登录目录为/var/ftp

最后重启vsftpd服务器

1
systemctl restart vsftpd.service

服务运维

1
2
3
systemctl restart vsftpd.service  # 重启服务
systemctl start vsftpd.service # 启动服务
systemctl status vsftpd.service # 服务状态查看

FTP命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
ftp> ascii  # 设定以ASCII方式传送文件(缺省值) 
ftp> bell # 每完成一次文件传送,报警提示.
ftp> binary # 设定以二进制方式传送文件.
ftp> bye # 终止主机FTP进程,并退出FTP管理方式.
ftp> case # 当为ON时,用MGET命令拷贝的文件名到本地机器中,全部转换为小写字母.
ftp> cd # 同UNIX的CD命令.
ftp> cdup # 返回上一级目录.
ftp> chmod # 改变远端主机的文件权限.
ftp> close # 终止远端的FTP进程,返回到FTP命令状态, 所有的宏定义都被删除.
ftp> delete # 删除远端主机中的文件.
ftp> dir [remote-directory] [local-file] # 列出当前远端主机目录中的文件.如果有本地文件,就将结果写至本地文件.
ftp> get [remote-file] [local-file] # 从远端主机中传送至本地主机中.
ftp> help [command] # 输出命令的解释.
ftp> lcd # 改变当前本地主机的工作目录,如果缺省,就转到当前用户的HOME目录.
ftp> ls [remote-directory] [local-file] # 同DIR.
ftp> macdef # 定义宏命令.
ftp> mdelete [remote-files] # 删除一批文件.
ftp> mget [remote-files] # 从远端主机接收一批文件至本地主机.
ftp> mkdir directory-name # 在远端主机中建立目录.
ftp> mput local-files # 将本地主机中一批文件传送至远端主机.
ftp> open host [port] # 重新建立一个新的连接.
ftp> prompt # 交互提示模式.
ftp> put local-file [remote-file] # 将本地一个文件传送至远端主机中.
ftp> pwd # 列出当前远端主机目录.
ftp> quit # 同BYE.
ftp> recv remote-file [local-file] # 同GET.
ftp> rename [from] [to] # 改变远端主机中的文件名.
ftp> rmdir directory-name # 删除远端主机中的目录.
ftp> send local-file [remote-file] # 同PUT.
ftp> status # 显示当前FTP的状态.
ftp> system # 显示远端主机系统类型.
ftp> user user-name [password] [account] # 重新以别的用户名登录远端主机.
ftp> ? [command] # 同HELP. [command]指定需要帮助的命令名称。如果没有指定 command,ftp 将显示全部命令的列表。
ftp> ! # 从 ftp 子系统退出到外壳。

关闭FTP连接

1
2
3
bye
exit
quit

下载文件

1
2
ftp> get readme.txt # 下载 readme.txt 文件
ftp> mget *.txt # 下载

上传文件

1
2
ftp> put /path/readme.txt # 上传 readme.txt 文件
ftp> mput *.txt # 可以上传多个文件

状态码

  • 230 - 登录成功
  • 200 - 命令执行成功
  • 150 - 文件状态正常,开启数据连接端口
  • 250 - 目录切换操作完成
  • 226 - 关闭数据连接端口,请求的文件操作成功

参考资料

阿里云Linux安装镜像源地址:http://mirrors.aliyun.com/

第一步:备份你的原镜像文件,以免出错后可以恢复。

1
2
3
cp /etc/yum.repos.d/CentOS-Base.repo{,.backup}
# 或者
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

第二步:下载新的CentOS-Base.repo 到/etc/yum.repos.d/

如果 wget 没有安装,运行下面命令安装 wget 软件。

1
2
yum update --skip-broken
yum -y install wget

安装完成更新下载源

1
2
3
4
5
6
# CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
# CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
# CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

第三步:运行yum makecache生成缓存

1
2
yum clean all
yum makecache

yum 安装报错

1
File "/usr/bin/yum", line 30 except KeyboardInterrupt, e:

解决:修改文件/usr/bin/yum/usr/libexec/urlgrabber-ext-down头中相应python为#!/usr/bin/python2.7

163官方说明:

http://mirrors.163.com/
http://mirrors.163.com/.help/centos.html

中国开源镜像站点

其它命令

sudo chmod 755 -R node 修改目录权限
sudo lsof -nP -iTCP -sTCP:LISTEN 查看本地服务
ps -ef | grep websocket 查看websocket进程
ps aux | grep mysql 查看mysql进程
sudo kill 443 杀掉进程

给目录权限

sudo chmod -R 777 目录

搜索

find path -option [-print] [-exec -ok command] { }\;

  • pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
  • -print: find命令将匹配的文件输出到标准输出。
  • -exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;,注意{ }\;之间的空格。
  • -ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
1
2
3
4
5
6
7
8
9
10
11
$ find ~ -name "*.txt"  -print   #在$HOME中查.txt文件并显示
$ find . -name "*.txt" -print
$ find . -name "[A-Z]*" -print #查以大写字母开头的文件
$ find /etc -name "host*" -print #查以host开头的文件
# 查以两个小写字母和两个数字开头的txt文件
$ find . -name "[a-z][a-z][0–9][0–9].txt" -print
$ ind . -perm 755 -print
$ ind . -perm -007 -exec ls -l {} \; #查所有用户都可读写执行的文件同-perm 777
$ ind . -type d -print
$ ind . ! -type d -print
$ ind . -type l -print

ls

类似于dos下的dir命令

ls最常用的参数有三个: -a -l -F。

ls –a

Linux上的文件以.开头的文件被系统视为隐藏文件,仅用ls命令是看不到他们的,而用ls -
a除了显示一般文件名外,连隐藏文件也会显示出来。
ls –l
该参数显示更详细的文件信息。
ls –F
使用这个参数表示在文件的后面多添加表示文件类型的符号,例如*表示可执行,/表示目录,@表示连结文件,这都是因为使用了-F这个参数。但是现在基本上所有的Linux发行版本的ls都已经内建了-F参数,也就是说,不用输入这个参数,我们也能看到各种分辨符号。

cd

用于切换用户当前工作目录

cd aaa 进入aaa目录
cd 命令后不指定目录,会切换到当前用户的home 目录
cd ~ 作用同cd后不指定目录,切换到当前用户的home 目录
cd - 命令后跟一个减号,则会退回到切换前的目录
cd .. 返回到当前目录下的上一级目录

pwd

用于显示用户当前工作目录

mkdir 和 rmdir

midir:创建目录 / rmdir:删除目录

两个命令都支持-p参数,对于mkdir命令若指定路径的父目录不存在则一并创建,对于rmdir命令则删除指定路径的所有层次目录,如果文件夹里有内容,则不能用rmdir命令

如下:

1
2
mkdir -p 1/2/3
rmdir -p 1/2/3

mkdir循环创建目录

How do I make multiple directories at once in a directory?

1
2
3
4
5
6
7
for char in {A..Z}; do
mkdir $char
done

for num in {1..100}; do
mkdir $num
done

cp

复制命令

  • 复制一个文件到另一目录:cp 1.txt ../test2
  • 复制一个文件到本目录并改名:cp 1.txt 2.txt
  • 复制一个文件夹a并改名为b,-r或-R 选项表明递归操作:cp -r a b
  • 同时拷贝多个文件,我们只需要将多个文件用空格隔开。cp file_1.txt file_2.txt file_3.txt /home/pungki/office

mv

移动命令

将一个文件移动到另一个目录:mv 1.txt ../test1
将一个文件在本目录改名:mv 1.txt 2.txt
将一个文件一定到另一个目录并改名:mv 1.txt ../test1/2.txt

rm命令

命令用于删除文件,与dos下的del/erase命令相似,rm命令常用的参数有三个:-i,-r,-f。

  1. –i :系统在删除文件之前会先询问确认,用户回车之后,文件才会真的被删除。需要注意,linux下删除的文件是不能恢复的,删除之前一定要谨慎确认。
  2. –r:该参数支持目录删除,功能和rmdir命令相似。
  3. –f:和-i参数相反,-f表示强制删除

find

find 查找目录和文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-name   filename   #查找名为filename的文件
-perm #按执行权限来查找
-user username #按文件属主来查找
-group groupname #按组来查找
-mtime -n +n #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
-atime -n +n #按文件访问时间来查GIN: 0px">

-ctime -n +n #按文件创建时间来查找文件,-n指n天以内,+n指n天以前

-nogroup #查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser #查无有效属主的文件,即文件的属主在/etc/passwd中不存
-newer f1 !f2 #找文件,-n指n天以内,+n指n天以前
-ctime -n +n #按文件创建时间来查找文件,-n指n天以内,+n指n天以前
-nogroup #查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser #查无有效属主的文件,即文件的属主在/etc/passwd中不存
-newer f1 !f2 #查更改时间比f1新但比f2旧的文件
-type b/d/c/p #查是块设备、目录、字符设备、管道、符号链接、普通文件
-size n[c] #查长度为n块[或n字节]的文件
-depth #使查找在进入子目录前先行查找完本目录
-fstype #查更改时间比f1新但比f2旧的文件
-type b/d/c/p #查是块设备、目录、字符设备、管道、符号链接、普通文件
-size n[c] #查长度为n块[或n字节]的文件
-depth #使查找在进入子目录前先行查找完本目录
-fstype #查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到
-mount #查文件时不跨越文件系统mount点
-follow #如果遇到符号链接文件,就跟踪链接所指的文件
-cpio %; #查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到
-mount #查文件时不跨越文件系统mount点
-follow #如果遇到符号链接文件,就跟踪链接所指的文件
-cpio #对匹配的文件使用cpio命令,将他们备份到磁带设备中
-prune #忽略某个目录

例子:

  • ~ 代表的是$home目录,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
$ find . -name '*.DS_Store' -type f -delete # 删除所有.DS_Store文件
$ find ~ -name "*.txt" -print # 在$HOME中查.txt文件并显示
$ find . -size +1000000c -print # 查长度大于1Mb的文件
$ find . -size 100c -print # 查长度为100c的文件
$ find . -size +10 -print # 查长度超过期作废10块的文件(1块=512字节)
$ find -name april* # 在当前目录下查找以april开始的文件
$ find -name april* fprint file # 在当前目录下查找以april开始的文件,并把结果输出到file中
$ find -name ap* -o -name may* # 查找以ap或may开头的文件
$ find /mnt -name tom.txt -ftype vfat # 在/mnt下查找名称为tom.txt且文件系统类型为vfat的文件
$ find /mnt -name t.txt ! -ftype vfat # 在/mnt下查找名称为tom.txt且文件系统类型不为vfat的文件
$ find /tmp -name wa* -type l # 在/tmp下查找名为wa开头且类型为符号链接的文件
$ find ~ -mtime -2 # 在/home下查最近两天内改动过的文件
$ find ~ -atime -1 # 查1天之内被存取过的文件
$ find ~ -mmin +60 # 在/home下查60分钟前改动过的文件
$ find ~ -amin +30 # 查最近30分钟前被存取过的文件
$ find ~ -newer tmp.txt # 在/home下查更新时间比tmp.txt近的文件或目录
$ find ~ -anewer tmp.txt # 在/home下查存取时间比tmp.txt近的文件或目录
$ find ~ -used -2 # 列出文件或目录被改动过之后,在2日内被存取过的文件或目录
$ find ~ -user cnscn # 列出/home目录内属于用户cnscn的文件或目录
$ find ~ -uid +501 # 列出/home目录内用户的识别码大于501的文件或目录
$ find ~ -group cnscn # 列出/home内组为cnscn的文件或目录
$ find ~ -gid 501 # 列出/home内组id为501的文件或目录
$ find ~ -nouser # 列出/home内不属于本地用户的文件或目录
$ find ~ -nogroup # 列出/home内不属于本地组的文件或目录
$ find ~ -name tmp.txt -maxdepth 4 # 列出/home内的tmp.txt 查时深度最多为3层
$ find ~ -name tmp.txt -mindepth 3 # 从第2层开始查
$ find ~ -empty # 查找大小为0的文件或空目录
$ find ~ -size +512k # 查大于512k的文件
$ find ~ -size -512k # 查小于512k的文件
$ find ~ -links +2 # 查硬连接数大于2的文件或目录
$ find ~ -perm 0700 # 查权限为700的文件或目录
$ find ~ -perm 755 -print | more # 查找权限为755的文件
$ find /tmp -name tmp.txt -exec cat {} \;
$ find /tmp -name tmp.txt ok rm {} \;

$ find / -amin -10 # 查找在系统中最后10分钟访问的文件
$ find / -atime -2 # 查找在系统中最后48小时访问的文件
$ find / -empty # 查找在系统中为空的文件或者文件夹
$ find / -group cat # 查找在系统中属于 groupcat的文件
$ find / -mmin -5 # 查找在系统中最后5分钟里修改过的文件
$ find / -mtime -1 # 查找在系统中最后24小时里修改过的文件
$ find / -nouser # 查找在系统中属于作废用户的文件
$ find / -user fred # 查找在系统中属于FRED这个用户的文件

du、df命令

du命令可以显示目前的目录所占用的磁盘空间,df命令可以显示目前磁盘剩余空间。

如果du命令不加任何参数,那么返回的是整个磁盘的使用情况,如果后面加了目录的话,就是这个目录在磁盘上的使用情况。

  1. du -hs 指定目录 查看指定目录的总大小
  2. du -hs ./* 查看当前目录下的所有文件夹和文件的大小

这两个命令都支持-k,-m和-h参数,-k和-m类似,都表示显示单位,一个是k字节一个是兆字节,-h则表示human-readable,即友好可读的显示方式。

cat命令

cat命令的功能是显示或连结一般的ascii文本文件。cat是concatenate的简写,类似于dos下面的type命令。用法如下:

  1. cat file1 显示file1文件内容
  2. cat file1 file2 依次显示file1,file2的内容
  3. cat file1 file2 > file3 把file1, file2的内容结合起来,再“重定向(>)”到file3文件中。
  4. >是右重定向符,表示将左边命令结果当成右边命令的输入,注意:如果右侧文件是一个已存在文件,其原有内容将会被清空,而变成左侧命令输出内容。如果希望以追加方式写入,请改用”>>”重定向符。

如果”>”左边没有指定文件,如: cat >file1,将会等用户输入,输入完毕后再按[Ctrl]+[c]或[Ctrl]+[d],就会将用户的输入内容写入file1。

echo命令

echo命令的使用频率不少于ls和cat,尤其是在shell脚本编写中。
语法:echo [-ne][字符串]
功能:echo会将输入的字符串送往标准输出,输出的字符串间以空白字符隔开, 并在最后加上换行符。

参数:

  • -n 显示字串时在最后自动换行
  • -e 支持以下格式的转义字符, -E 不支持以下格式的转义字符
  • /a 发出警告声;
  • /b 删除前一个字符;
  • /c 最后不加上换行符号;
  • /f 换行但光标仍旧停留在原来的位置;
  • /n 换行且光标移至行首;
  • /r 光标移至行首,但不换行;
  • /t 插入tab;
  • /v 与/f相同;
  • // 插入/字符;
  • /nnn 插入nnn(八进制)所代表的ASCII字符;

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
kenny@jstest:~/hgd> echo "123" "456"
123 456
kenny@jstest:~/hgd> echo "123/n456"
123/n456
kenny@jstest:~/hgd> echo -e "123/n456"
123
456
kenny@jstest:~/hgd> echo -E "123/n456"
123/n456
kenny@jstest:~/hgd> echo -E "123///456"
123//456
kenny@jstest:~/hgd> echo -e "123///456"
123/456
kenny@jstest:~/hgd> echo -e "123/100456"
123@456

注意事项:
在Linux使用的bash下,单引号’’和双引号是有区别的,单引号忽略所有的转义,双引号不会忽略以下特殊字符:
Dollar signs ($),Back quotes (`),Backslashes (/),Excalmatory mark(!)

示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
kenny@jstest:~> echo "`TEST`"
-bash: TEST: command not found
kenny@jstest:~> echo '`TEST`'
`TEST`
kenny@jstest:~> echo "$TEST"

kenny@jstest:~> echo '$TEST'
$TEST
kenny@jstest:~> echo "//TEST"
/TEST
kenny@jstest:~> echo '//TEST'
//TEST
kenny@jstest:~> echo "Hello!"
echo "Hello"
Hello
kenny@jstest:~> echo 'Hello!'
Hello!

more,less,clear

more,less命令

1
这两个命令用于查看文件,如果一个文件太长,显示内容超出一个屏幕,用cat命令只能看到最后的内容,用more和less两个命令可以分页查看。more指令可以使超过一页的文件内容分页暂停显示,用户按键后才继续显示下一页。而less除了有more的功能以外,还可以用方向键往上或往下的滚动文件,更方便浏览阅读。

less的常用动作命令:

  • 回车键 向下移动一行;
  • y 向上移动一行;
  • 空格键 向下滚动一屏;
  • b 向上滚动一屏;
  • d 向下滚动半屏;
  • h less的帮助;
  • u 向上洋动半屏;
  • w 可以指定显示哪行开始显示,是从指定数字的下一行显示;比如指定的是6,那就从第7行显示;
  • g 跳到第一行;
  • G 跳到最后一行;
  • p n% 跳到n%,比如 10%,也就是说比整个文件内容的10%处开始显示;
  • /pattern 搜索pattern ,比如 /MAIL表示在文件中搜索MAIL单词;
  • v 调用vi编辑器;
  • q 退出less
  • !command 调用SHELL,可以运行命令;比如!ls 显示当前列当前目录下的所有文件;
  • clear命令 clear命令是用来清除当前屏幕显示的,不需任何参数,和dos下的cls命令功能相同。

head,tail

  • head和tail命令都用于查看文本文件,区别在于: head显示文件的头n行,tail显示文件的尾n行,缺省情况n都为10行。可以通过-n方式指定行数,如:head -100 file和tail -100 file分别表示显示文件头100行和尾100行内容。
  • tail -f命令可以实时查看文件新增内容。

wc命令

该命令用于统计指定文件中的字节数、字数、行数。该命令各选项含义如下:

  • -l 统计行数
  • -w 统计字数
  • -c 统计字节数

这些选项可以组合使用。输出列的顺序和数目不受选项的顺序和数目的影响。总是按下述顺序显示并且每项最多一列。
行数、字数、字节数、文件名
如果命令行中没有文件名,则输出中不出现文件名。

例如:

1
2
3
4
5
6
oracle@hjtest:~> wc 1.txt 2.txt
460 1679 16353 1.txt
300 1095 10665 2.txt
760 2774 27018 总用量
oracle@hjtest:~> wc -l 1.txt
460 1.txt

缺省参数为-lcw,即wc file1 file2命令的执行结果与上面一样。

grep 命令

grep是(global search regular expression(RE) and print out the line的缩写,用于从文件面搜索包含指定模式的行并打印出来,它是一种强大的文本搜索工具,支持使用正则表达式搜索文本。grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被””引用,模板后的所有字符串被看作文件名。搜索结果送到屏幕,不影响原文件内容。

grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。我们利用这些返回值就可进行一些自动化的文本处理工作。
示例:

1
2
3
4
5
6
7
8
9
10
$ ls -l | grep '^a'
通过管道过滤ls -l输出的内容,只显示以a开头的行。
$ grep 'test' d*
显示所有以d开头的文件中包含test的行。
$ grep 'test' aa bb cc
显示在aa,bb,cc文件中匹配test的行。
$ grep '[a-z]/{5/}' aa
显示所有包含每个字符串至少有5个连续小写字符的字符串的行。
$ grep 'w/(es/)t.*/1' aa
如果west被匹配,则es就被存储到内存中,并标记为1,然后搜索任意个字符(.*),这些字符后面紧跟着另外一个es(/1),找到就显示该行。如果用egrep或grep -E,就不用"/"号进行转义,直接写成'w(es)t.*/1'就可以了。

man,logout命令

man命令,man是manual的缩写,相当于Unix/Linux的联机Help,每个系统命令和调用都有非常详细的说明,绝大多数都是英文。如:man ls即是查看ls命令的使用说明,一般还有另一种方法用来查看帮助,如:ls –help,这种方式绝大多数命令都支持。

logout命令,该命令用于退出系统,与login命令对应。

管道和xargs

管道

利用Linux所提供的管道符“|”将两个命令隔开,管道符左边命令的输出就会作为管道符右边命令的输入。连续使用管道意味着第一个命令的输出会作为第二个命令的输入,第二个命令的输出又会作为第三个命令的输入,依此类推。
注意:管道左边命令的输入作为管道右边命令的输入(命令的输入是一定的),不是参数,并不是所有命令都支持管道
例子:ls | grep a 查看当前目录下名称包含a的文件或文件夹

xargs

大多数 Linux 命令都会产生输出:文件列表、字符串列表等。但如果要使用其他某个命令并将前一个命令的输出作为参数该怎么办?例如,file 命令显示文件类型(可执行文件、ascii 文本等);你能处理输出,使其仅显示文件名,目前你希望将这些名称传递给 ls -l 命令以查看时间戳记。xargs 命令就是用来完成此项工作的。
注意:find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去

例子:

  1. 在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中:
1
$ find / -name "core" -print | xargs echo "" >/tmp/core.log
  1. 当一个目录下文件太多时,直接用rm * 命令会包参数过长,用如下方法可以全部删除
1
$ls | xargs rm

basename 和 dirname

basename用于查看文件不含路径的名字,dirname则用于查看文件路径,使用效果我们测试一下便知:

1
2
3
4
5
6
7
8
> basename /home/hj/1.txt
1.txt
> dirname /home/hj/1.txt
/home/hj
> basename 1.txt
1.txt
> dirname 1.txt
.