How to reset FreeBSD root password

Here are the steps to reset root password on FreeBSD 11.

Boot the system, and wait till you get to the Welcome to FreeBSD boot menu.

Once you are there, press 2 to boot the system to single user mode.

FreeBSD 11 boot menu

Wait till the server shows a following message: “When prompted Enter full path name of shell or RETURN for /bin/sh:”, and then press Enter key to complete the boot process to single user mode.

You will now be presented with console input.

In the console type following commands:

mount -u /
mount -a

Now you can use passwd command to change root user password, by typing in the new password when asked, and they retyping it on second prompt.

FreeBSD 11 single user mode

Once you have set a new password, reboot the server, and log in with the new password.

Reset root password on CentOS 6 machine

If you have forgotten root user password on your CentOS 6 machine, you can reset the password to new value, without knowing the old one, by booting the machine to single user mode, and resetting the password with passwd command.

Boot to single user mode

During the CentOS boot process, you will be presented with a countdown before CentOS boot process actually begins to load the OS.

Press any key to enter the GNU GRUB menu.

CentOS 6 boot countdown
CentOS boot countdown

On the GNU GRUB menu highlight the kernel you want to boot, and press ‘e’ key to edit the kernel commands before booting.

CentOS 6 kernel select
CentOS 6 kernel select

Once you have entered the kernel edit mode, find the line beginning with kernel, and highlight it, and press ‘e’ key to edit it.

Edit kernel line
Edit kernel line

On the end of the line add the word ‘single’, with white space before ‘single’, and press ENTER to accept the change.

Add single to then end of the kernel line
Add single to then end of the kernel line

Boot the machine with the edited kernel argument, and you will be logged in as root in single user mode.

Now just issue passwd command, and enter the new password two times, when asked.

Change password with passwd command
Change password with passwd command

Once you have changed the password, reboot the machine, and log to it with your new root password.

How to reset root password on Debian 8 (Jessie)

If you have forgotten your root user password for your Debian system, you can reset the password to new value, without entering the old one, by going into grub and editing the commands before booting the system.

Edit GRUB before Debian boot

During you machine boot process, you should be presented with GNU GRUB screen with countdown, before the default boot option is activated.

Press any key during the countdown period stop the boot process, and then press e key to edit the commands before booting.

Debian 8 boot menu
Debian 8 boot menu

One you have entered into editing mode, find the line starting with linux, followed by /boot/vmlinuz-*, and containing section with root=UUID=.

Line should be towards the end, usually two lines before the final line.

On the end of that line, in most case after ro quiet, add one of the following two: init=/bin/bash or init=/bin/sh, as shown on picture below.

Edit kernel line
Edit kernel line

After you have added the init value, press Ctrl+x or F10 key to boot the with the selected options.

Debian should now boot to single user mode, with the root filesystem mounted in read-only mode.

You need to remount it to read-write mode with the following command:
mount -n -o remount,rw /.

After that you can use passwd command to change the root password to a new value.

Change root password with passwd
Change root password with passwd

Type passwd to be asked for a new password, and then reenter the new password again, once asked to retype it, and you should then be presented with following message:

passwd: password updated successfully

You can now reboot your machine, and use the new root password on your system.

Alternative method

If this method is not working for your, you can try to boot the system from a LiveCD, and then mount the partition holding your /etc, and edit the shadow file.

In the shadow file, find the line starting with root:, and change the hashed password between “root:” and next “:” to just “!”.

Shadow file should initially look something like this:

root:random_characters_of_hashed_password:12345::12345:1:::bin:*:12345:0:12345:1:::

After the change it should look something like this:

root:!:12345::12345:1:::bin:*:12345:0:12345:1:::

This allows you to get access to the system with no root password, when you access recovery mode in Advanced options for Debian GNU/Linux.

Press any key during the countdown period stop the boot process, and then select Advanced options for Debian GNU/Linux.

Select Advanced options in menu
Select Advanced options in menu

After you selected Advanced options, select to boot recovery mode.

Select recovery mode in menu
Select recovery mode in menu

After you press Enter to boot recovery mode, you will be logged as root, and can just issue passwd command to change your root password.

Debian 8 recovery mode
Debian 8 recovery mode

 

How to provide SSH password inside a script or oneliner

If you ever need to provide a password for SSH login inside a bash script or a shell command, to avoid being asked a password when SSH keys are not used, it can be done with usage of expect command, or sshpass utility.

Using expect

Expect is a program that “talks” to other interactive programs according to a script.

http://www.tcl.tk/man/expect5.31/expect.1.html

Lets say you want to SSH to a server and run a remote command, ls for instance, with a command like this:

ssh [email protected] "ls -lh file"

If you are not using SSH keys, you will be prompted a password, and will need to enter it manually.

If you want to avoid entering a password, and have it inputted to login prompt automatically you can use expect command.

Provide SSH password inside a script.

Expect reads cmdfile for a list of commands to execute. Expect may also be invoked implicitly on systems which support the #! notation by marking the script executable, and making the first line in your script:
#!/usr/local/bin/expect -f

Example of a script which runs a remote command over SSH, with password being provided inside a script.

#!/usr/bin/expect -f
spawn ssh [email protected] "ls /file"
expect "assword:"
send "mypassword\r"
interact

One-liner

Running a SSH command with provided password inside a one-liner, can be done using expect -c and then putting the commands inside single quotes.

The -c flag prefaces a command to be executed before any in the script. The command should be quoted to prevent being broken up by the shell. This option may be used multiple times. Multiple commands may be executed with a single -c by separating them with semicolons.

Example of a one-line command:

expect -c 'spawn ssh [email protected] "ls -lh file"; expect "assword:"; send "mypassword\r"; interact'

Using sshpass

SSH password prompt can also be bypassed by connecting with sshpass command, which is available in EPEL repo in CentOS

To install sshpass, first make sure you have EPEL repo on your server.

You can install EPEL with

yum -y install epel-release

Install sshpass with

yum -y install sshpass

You can then connect to remote server with sshpass using a command similar to this:

sshpass -p 'password' ssh [email protected]

If you have never connected to the server before, you will probably not get connected to remote server due to host key checking.
To bypass host key checking use -o StrictHostKeyChecking=no option:

sshpass -p 'password' ssh -o StrictHostKeyChecking=no  [email protected]
References:

http://unix.stackexchange.com/questions/252777/use-she-bang-in-oneliner

http://stackoverflow.com/questions/1924464/bash-controlling-ssh

http://stackoverflow.com/questions/16928004/how-to-enter-ssh-password-using-bash

http://linux.die.net/man/1/expect

http://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/

How to reset lost root password on SUSE Linux Enterprise Server

I had to use one of my virtual machines that I didn’t use for a while today, and of course  I couldn’t remember the password I used for it, so I had to change it.

Here is how to reset a forgotten root password on Novell SUSE

This is a guide for Novell SUSE Linux Enterprise Server 10 SP3.

On the boot menu select the first option “SUSE Linux enterprise server” and press ‘e’ for edit.

SLES boot menu
SLES boot menu

On the second menu select the kernel option and press ‘e’ for edit.

Select kernel line
Select kernel line

type init=/bin/bash (leave empty space at the begging), and press ‘Enter’ and then ‘b’ to boot with that option.

Add init=/bin/bash one the end of the line
Add init=/bin/bash one the end of the line

The system will boot with the root user logged on, type passwd to change the password and input your new password, or just press ‘Enter’ for no password (blank password)

Use passwd to change root password
Use passwd to change root password
Reboot the server and log on with your new password.