Ubuntu reset root password

If you forgot root’s password follow these steps to reset it:
1. Reboot, press ESC to get to GRUB boot loader menu
2. Choose from the list kernel with

(recovery mode)

3. From the menu screen choose

"Drop to root shell prompt"

4. By default it gives you read-only filesystem Run this for read/write:

mount -o remount,rw /

5. Run this to change root’s password:

passwd root

Mysql bulk UPDATE

Run the following statement for bulk UPDATE

UPDATE table1 SET column2 = CASE 
WHEN column1 = 'A' THEN 'AB'
WHEN column1 = 'B' THEN 'BC'
WHEN column1 = 'C' THEN 'CD'
ELSE column2 
END;

Make sure you included ELSE statement, if you skip it then it will NULL rest of the column2 fields.
So, if you have any pre-filled column2 field and you want to keep their values then use ELSE column2 to keep them.

Git show last changes in file

git diff composer.json

PHP sending email via gmail

apt-get install msmtp
add gmail account to /etc/msmtp
vi php.ini
 
sendmail_path = "/usr/bin/msmtp --logfile /var/log/msmtp_php/msmtp-php-cli.log -a gmail -t"

Git link local branch to remote

git branch --set-upstream-to=origin/branchname

Ssh key generation and folder files explain

Ssh protocol version 1 key generation.

ssh-keygen

When asked for a “passphrase”, we won’t enter one. Just press enter twice.
It will generate 2 files:

/home/user/.ssh/id_rsa.pub
/home/user/.ssh/id_rsa

2. Copy id_rsa.pub to the remove host

scp id_rsa.pub user@hostname:/home/user/.ssh/id_rsa.pub

or

ssh-copy-id -i .ssh/id_rsa.pub user@hostname

3. Login to remote host and add to authorized_keys file

ssh hostname -v
cd /home/user/.ssh
cat id_rsa.pub >> authorized_keys
chown user /home/user/.ssh/authorized_keys
chgrp user /home/user/.ssh/authorized_keys

Ssh protocol version 2 key generation.

ssh-keygen -t dsa

When asked for a “passphrase”, we won’t enter one. Just press enter twice.
It will generate 2 files:

/home/user/.ssh/id_dsa.pub
/home/user/.ssh/id_dsa

2. Copy id_dsa.pub to the remove host

scp id_dsa.pub user@hostname:/home/user/.ssh/id_dsa.pub

or

ssh-copy-id -i .ssh/id_rsa.pub user@hostname

3. Login to remote host and add to authorized_keys2 file

ssh hostname -v
cd /home/user/.ssh
cat id_dsa.pub >> authorized_keys2
chown user /home/user/.ssh/authorized_keys2
chgrp user /home/user/.ssh/authorized_keys2

Web server production and prerelease environments

ln -s /var/www /var/www.prod
 
ln -s /var/www2 /var/www.prerelease

Backup mysql configs and database

sudo tar cvfvz /backup/mysql_conf.tgz /etc/mysql
sudo sh -c 'mysqldump -u root -p -A --events > /backup/backup_db.sql'
sudo tar cvfvz /backup/mysql_data.tgz /var/lib/mysql

Mysql create new database and user

CREATE DATABASE testdatabase;
 
CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'testpassword';
 
GRANT ALL PRIVILEGES ON testdatabase.* TO 'testuser'@'localhost';

Find programs that listens on network ports/sockets

sudo netstat -peanut