Skip to content

Instantly share code, notes, and snippets.

@dnozay
Last active December 27, 2024 02:52
Show Gist options
  • Save dnozay/188f256839d4739ca3e4 to your computer and use it in GitHub Desktop.
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: "[email protected]")
# 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
@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.

@sbarcoe90
Copy link

I too could not log in for a while, after getting GitLab running on my macos via Docker Desktop. Eventually, I realised here, it tells you where to get the root password:

docker exec -it dockerContainerIDHere grep 'Password:' /etc/gitlab/initial_root_password

In my case:

sbarcoe@macbookm2 gitlab-ee % docker exec -it f2d006d2944e grep 'Password:' /etc/gitlab/initial_root_password
Password: c8OuRl4Hf0UECI+tFo5WRWVNIdW3sdUuTges3YnvtQI=``

@GarvjeetSingh
Copy link

sudo gitlab-rake gitlab:password:reset
//hostname:
//password:
//password_confimation:

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