Add new user and password to linux via a sql database - sql

so a user enters data in a web form, data gets sent to main database table, and then a script runs periodically, like every day to then do a few tasks like adding a user to a linux system. I want to add a user with a password that was stored in the database. So far I only have this:
$MYSQL -e "select name from users registered" | sudo xargs -L 1 useradd
$MYSQL -e "select password users registered" | sudo xargs -L 1 passwd
but it seems to think the password field in the database is a username?

Related

how to specify login role(user) in postgresql

I am trying to learn SQL. I have picked up postresql . now I hae a query myself. How can specify login role(user) or switch the role ?
I had create new users like this:-
in zsh shell with postgres user
% createuser --interactive sqltest01
and
% psql
postgres=> create user sqltest02 with password '1';
CREATE ROLE
now I have 3 users or roles
postgres=> \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
sqltest01 | Create role, Create DB | {}
sqltest02 | | {}
How can I login with these users ?
I usely login like this :-
% sudo su postgres
% psql
which logs me in with user postgres
\conninfo
You are connected to database "postgres" as user "postgres" via socket in "/run/postgresql" at port "5432".
how can I login with sqltest01 or sqltest02 ?
If you are logged into the same computer that Postgres is running on you can use the following psql login command, specifying the database (mydb) and username (myuser):
psql -d mydb -U myuser
If for some reason you are not prompted for a password when issuing these commands, you can use the -W option:
psql -d mydb -U myuser -W

Changing a password with one command in FreeBSD

How can I, as a simple user, change my own password with one command (in one line) in FreeBSD. I tried using passwd --stdin but that seems like it's a Linux command only.
Use the pw command to take input from STDIN like this:
echo "mynewpassword" | pw usermod admin -h 0
See man pw for more details.
Just type passwd and follow the prompts.
Also man 1 passwd for documentation.
Update with a copy of my comment, below, from September 2013:
The whole point about passwd is to make that hard for automated password changers to guess passwords. If you have a port like expect on the system you could script it. If you are root, you could use pw usermod username -h0. If you're just an ordinary user, just do it interactively.

How should I set password for a user I created using ssh?

I have 20 machines where I need to create a user and set his password. I can create the accounts and set the passwords using a for loop. The inside of the for loop is given as follows
ssh -t user1#$node_name 'sudo useradd user2'
ssh -t user1#$node_name 'sudo passwd user2'
However, this requires me to input the password for user1 first and then input the new password for user2. I tried it for 2 machines and it works. I however do not like the wasteful effort involved and am guessing there would me a more efficient way of doing so. Any ideas?
To remove the need to enter user1's password, you can mess with the sudo -A or -a options on $node_name to get authentication to happen automatically in some other way.
To remove the need to type user2's password, you can try something like this:
ssh -t user1#$node_name "sudo echo $newpass | useradd user2 --stdin"

Accessing postgres without being root users

I have created a postgres database in postgres named "databaseName". Now I can access this data2database through
su - postgres
and then typing my password
Then I enter into the database through: psql databaseName
I have created users of this database through:
createuser -P userName1
Now I dont want the users of the database to access the database as root user. Now when the users try to login into postgres as
su - postgres -u userName1
or through
psql databaseName -u userName1
I get error...can someone guide me as to how the users can get access to postgres and database without being root user?
Configure pg_hba.conf to accept peer connections over unix sockets or ident connections over host (tcp) connections. If you prefer you can use md5 to use password authentication instead.
By default psql will connect with the same username as your local OS username; this can be overridden by the -U flag, eg:
psql -U myusername thedatabase
Note that it's -U not -u (it's upper case).
This is all covered in detail in the PostgreSQL documentation; see:
Client Authentication
pg_hba.conf
psql
BTW, if you want to run commands as the postgres user, rather than su'ing you can just write:
sudo -u postgres psql

Add database name to sql file when exporting using mysqldump via linux

I need to backup a range of databases each day and I would like to do this via command line.
I'm using mysqldump to dump the db into a folder on the root of the server appended with the date. I would like to add the name of the database dynamically to the exported filename, rather than hard coding it into the query. Currently I have:
[~]# mysqldump -u user -h localhost -p unique_database_name > unique_database_name_1_$(date +%d%m%y).sql
The goal is to have 'unique_database_name' appended to the filename, so the script is a little more portable.
This script would do it:
#!/bin/bash
dbs='firstdb seconddb thirddb'
echo -n 'Enter database password: '
read pw
for db in $dbs
do
mysqldump -u user -h localhost -p$pw $db > $db_1_$(date +%d%m%y).sql
done