Skip to content

Instantly share code, notes, and snippets.

@dnozay
Last active May 31, 2023 02:12
  • Star 42 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dnozay/188f256839d4739ca3e4 to your computer and use it in GitHub Desktop.
Collection of useful stuff for interacting with gitlab.

Reset root/admin password

Lost the root/admin password? You can reset it using the command-line. Recipe adapted from gitlab issue #308.

# start the console
sudo gitlab-rails console

Then in the ruby interpreter:

# find the user:
# user = User.find_by(email: "admin@example.com")
# user = User.find_by(username: "root")
# user = User.find_by(name: "Administrator")
# user = User.find_by(admin: true)
user = User.find_by(username: "root")

# change the password
# or use the ask function from 'highline/import'
user.password = 'secret_pass'
user.password_confirmation = 'secret_pass'

# and save
user.save

Re-enable standard login

# locate application settings
# ApplicationSetting.find_each
appsettings = ApplicationSetting.find_by(signin_enabled: false)
appsettings.signin_enabled = true
appsettings.save

Pre-populate users

I have found that you can just post the info with your admin token:

import requests
headers = { 'PRIVATE-TOKEN' : 'insert-token-here' }
data = {
    'email': email,
    'extern_uid': dn,
    "provider": "ldapmain",
    "name": name,
    "username": username,
    'password': '1234567890',
    "confirm": False,
}
requests.post('https://gitlab.example.com/api/v3/users/', data, headers=headers)

I get the email, dn, name, username from ldap by using python-ldap; I use a filter to only match engineers. The password is required, but you can use a dummy value that matches the security parameters (min 8 characters ...etc)

Pre-confirm users

In case you missed to add confirm=false when creating users.

gitlab-rails console production

then

for user in User.where(confirmed_at: nil) do
    user.confirmed_at = Time.now
    user.confirmation_token = nil
    user.save!
end
Copy link

ghost commented Feb 6, 2018

Do you know of a way to interact with gitlab-rails console non-interactive via a script?

@Fobhep
Copy link

Fobhep commented Feb 12, 2018

@mdebord1 try this:
echo 'user = User.find_by(username: "root");user.password="secret_pass!";user.password_confirmation="secret_pass!";user.save' | gitlab-rails console

not very nice, but functional

@melroy89
Copy link

melroy89 commented Oct 16, 2018

I can not exit this console. I tried Ctrl+C. I tried exit q I don't know anymore.
Finally: exit().

@mooreofthesame
Copy link

mooreofthesame commented Sep 12, 2019

I can not exit this console. I tried Ctrl+C. I tried exit q I don't know anymore.
Finally: exit().

To exit the console, you can hit Ctrl+d (this is the same as EOF, or End Of File)

@ntwrkguru
Copy link

Do you know of a way to interact with gitlab-rails console non-interactive via a script?

You can use the rails runner

@nitesh8860
Copy link

nitesh8860 commented Apr 2, 2020

one line command to reset password:
gitlab-rails runner "user = User.where(id: 1).first; user.password = 'secret$'; user.password_confirmation = 'secret$';"

@drehren
Copy link

drehren commented May 31, 2023

I was doing this to change the root password because it would not log in, but it didn't work.

Finally after hours of looking at the database differences (because had one that could not migrate) the root user had password_automatically_set to true.

So changing that in the console:

user = User.find(1)
user.password_automatically_set=false
user.save!

fixed the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment