版本管理之gitlab实践教程:基础篇(16)

这篇文章介绍一下如何修改初始管理账号root的密码。

命令行方式

确认gitlab-rails

使用gitlab-rails进行设定

 which gitlab-rails
/opt/gitlab/bin/gitlab-rails
# 
# gitlab-rails -v
Rails 4.2.7.1
# 

使用console方式

# gitlab-rails console production
Loading production environment (Rails 4.2.7.1)
irb(main):001:0>

查询root用户

一般使用mail地址进行用户定位,但是root用户在创建是往往没有设定邮箱,可以根据其ID进行定位,可以使用User.where(id:1).first

irb(main):001:0> user = User.where(id:1).first
=> #1, email: "[email protected]", encrypted_password: "$2a$10$c.TKRpA/e7RD2k0hFa2j8eAcOpYJO7SKaexc1NPB.Pl...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 2, current_sign_in_at: "2018-07-30 06:16:31", last_sign_in_at: "2018-07-29 22:33:21", current_sign_in_ip: "192.168.163.2", last_sign_in_ip: "192.168.163.2", created_at: "2018-07-29 22:13:41", updated_at: "2018-07-30 06:16:31", name: "Administrator", admin: true, projects_limit: 10, skype: "", linkedin: "", twitter: "", authentication_token: "9-9rh18mQz4h3eDMQf5M", theme_id: 2, bio: nil, failed_attempts: 0, locked_at: nil, username: "root", can_create_group: true, can_create_team: false, state: "active", color_scheme_id: 1, password_expires_at: nil, created_by_id: nil, last_credential_check_at: nil, avatar: nil, confirmation_token: nil, confirmed_at: "2018-07-29 22:13:41", confirmation_sent_at: nil, unconfirmed_email: nil, hide_no_ssh_key: false, website_url: "", notification_email: "[email protected]", hide_no_password: false, password_automatically_set: false, location: nil, encrypted_otp_secret: nil, encrypted_otp_secret_iv: nil, encrypted_otp_secret_salt: nil, otp_required_for_login: false, otp_backup_codes: nil, public_email: "", dashboard: 0, project_view: 0, consumed_timestep: nil, layout: 0, hide_project_limit: false, unlock_token: nil, otp_grace_period_started_at: nil, ldap_email: false, external: false>
irb(main):002:0> 

设定密码

irb(main):002:0> user.password='liumiaocn'
=> "liumiaocn"
irb(main):003:0> user.password_confirmation='liumiaocn'
=> "liumiaocn"
irb(main):004:0> 

保存用户密码

irb(main):004:0> user.save!
Enqueued ActionMailer::DeliveryJob (Job ID: 4842b3ec-3337-4861-9229-acae59dc12ab) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", gid://gitlab/User/1
=> true
irb(main):005:0>

确认结果

使用修改后的密码获取用户的token以验证密码是否正确

[root@platform ~]# curl http://192.168.163.118:32001/api/v3/session --data 'login=root&password=liumiaocn'
{"name":"Administrator","username":"root","id":1,"state":"active","avatar_url":"http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon","web_url":"http://1c79a0cf3ed8/u/root","created_at":"2018-07-29T22:13:41.119Z","is_admin":true,"bio":null,"location":null,"skype":"","linkedin":"","twitter":"","website_url":"","last_sign_in_at":"2018-07-29T22:33:21.419Z","confirmed_at":"2018-07-29T22:13:41.249Z","email":"[email protected]","theme_id":2,"color_scheme_id":1,"projects_limit":10,"current_sign_in_at":"2018-07-30T06:16:31.645Z","identities":[],"can_create_group":true,"can_create_project":true,"two_factor_enabled":false,"external":false,"private_token":"9-9rh18mQz4h3eDMQf5M"}[root@platform ~]#

HereDocument方式

设定脚本

使用HereDocument方式进行用户密码的设定,比如可以使用如下脚本, 通过传递一个参数来设定密码

# cat change_root_pw.sh
#!/bin/sh

PASSWORD="$1"

echo "## change root password to $PASSWORD"
gitlab-rails console production <1).first
user.password = "$PASSWORD"
user.password_confirmation = "$PASSWORD"
user.save!
EOF

echo "## Finished."
# 

执行日志

# sh change_root_pw.sh "12345678"
## change root password to 12345678
Loading production environment (Rails 4.2.7.1)
Switch to inspect mode.
user = User.where(id:1).first
# 1, email: "[email protected]", encrypted_password: "$2a$10$PMzzvp4KpnTxC7U1hYm2/uwQgUQxJbA5QpiZaxr9XrO...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 2, current_sign_in_at: "2018-07-30 06:16:31", last_sign_in_at: "2018-07-29 22:33:21", current_sign_in_ip: "192.168.163.2", last_sign_in_ip: "192.168.163.2", created_at: "2018-07-29 22:13:41", updated_at: "2018-07-30 07:43:00", name: "Administrator", admin: true, projects_limit: 10, skype: "", linkedin: "", twitter: "", authentication_token: "9-9rh18mQz4h3eDMQf5M", theme_id: 2, bio: nil, failed_attempts: 0, locked_at: nil, username: "root", can_create_group: true, can_create_team: false, state: "active", color_scheme_id: 1, password_expires_at: nil, created_by_id: nil, last_credential_check_at: nil, avatar: nil, confirmation_token: nil, confirmed_at: "2018-07-29 22:13:41", confirmation_sent_at: nil, unconfirmed_email: nil, hide_no_ssh_key: false, website_url: "", notification_email: "[email protected]", hide_no_password: false, password_automatically_set: false, location: nil, encrypted_otp_secret: nil, encrypted_otp_secret_iv: nil, encrypted_otp_secret_salt: nil, otp_required_for_login: false, otp_backup_codes: nil, public_email: "", dashboard: 0, project_view: 0, consumed_timestep: nil, layout: 0, hide_project_limit: false, unlock_token: nil, otp_grace_period_started_at: nil, ldap_email: false, external: false>
user.password = "12345678"
"12345678"
user.password_confirmation = "12345678"
"12345678"
user.save!
Enqueued ActionMailer::DeliveryJob (Job ID: 9febfb5e-a9c7-4d84-a9d5-df56cab89369) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", gid://gitlab/User/1
true

## Finished.
# 

结果确认

[root@platform tmp]# curl http://192.168.163.118:32001/api/v3/session --data 'login=root&password=12345678'
{"name":"Administrator","username":"root","id":1,"state":"active","avatar_url":"http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon","web_url":"http://1c79a0cf3ed8/u/root","created_at":"2018-07-29T22:13:41.119Z","is_admin":true,"bio":null,"location":null,"skype":"","linkedin":"","twitter":"","website_url":"","last_sign_in_at":"2018-07-29T22:33:21.419Z","confirmed_at":"2018-07-29T22:13:41.249Z","email":"[email protected]","theme_id":2,"color_scheme_id":1,"projects_limit":10,"current_sign_in_at":"2018-07-30T06:16:31.645Z","identities":[],"can_create_group":true,"can_create_project":true,"two_factor_enabled":false,"external":false,"private_token":"9-9rh18mQz4h3eDMQf5M"}[root@platform tmp]# 

你可能感兴趣的:(工具,#,版本管理)