1.进入控制台:
[root@hostname ~]# gitlab-rails console production
或者:
[root@hostname ~]# gitlab-rails console
2.定位到需要修改的用户
方法一:
通过用户名查找(推荐):
irb(main):001:0> user=User.where(username:'root').first
另一种写法(只能定位到普通用户,很容易出错造成找不到用户,返回 => nil,没有此用户的结果):
irb(main):002:0> User.where(name:'doudou').first
方法二:
通过id查找(一般修改root使用,因为root id=1):
irb(main):003:0> user=User.where(id:1).first
=> #<User id:1 @root>
方法三:
通过邮箱查找:
irb(main):004:0> user=User.find_by(email:'123@qq.com')
=> #<User id:92 @doudou>
3.重置密码:
一般数字密码就不用加引号:
irb(main):005:0>user.password=12345678
=> 12345678
有字符的密码一定要有引号(等号两边有空格):
irb(main):006:0>user.password = 'root@123'
=> “root@123”
4.确认新密码(可以省略):
irb(main):007:0>user.password_confirmation = 'root@123'
=> “root@123”
5.保存新密码:
irb(main):022:0>user.save!
Enqueued ActionMailer::DeliveryJob (Job ID: 04e11d87-7fc0-47a5-a792-aff11918335d) to Sidekiq(mailers) with arguments: “DeviseMailer”, “password_change”, “deliver_now”, #<GlobalID:0x00007fe595b63078 @uri=#<URI::GID gid://gitlab/User/10>>
=> true
最后是true就保存可以了!!!
irb(main):023:0>exit
扩展:
用脚本实现非交互式重置密码:
1.脚本需要使用到expect和spawn,yum安装一些包:
[root@hostname~]# yum install -y tcl tclx tcl-devel expect
[root@gmc-repository test]# vim gitlab.sh
#!/bin/sh
# \
exec expect -f "$0" "$@"
if { $argc != 2 } {
puts "Usage: $argv0 <username> <passwd>"
exit 1
}
set password [lindex $argv 1]
set name [lindex $argv 0]
spawn gitlab-rails console production
sleep 180
expect "irb(main)"
send "user=User.where(name:'$name').first\r"
expect "irb(main)"
send "user.password=$password\r"
expect "irb(main)"
send "user.password_confirmation=$password\r"
expect "irb(main)"
send "user.save!\r"
sleep 3
expect "irb(main)"
send "quit\r"
expect eof
执行脚本需要先加权限:
chmod u+x gitlab.sh
执行脚本需要两个参数:username passwd:
[root@hoastname test]# ./gitlab.sh weiliyan 12345678