Monday, September 23, 2024

Create 'myuser' Linux user to run your dedicated software

sudo apt update -y && sudo apt upgrade -y
sudo adduser myuser

Note the password you used for defining myuser user

(reference)

Give myuser 'sudo' permissions

sudo visudo

Add the below line right after the line with 'root' definitions

myuserALL=(ALL) NOPASSWD:ALL

Add 'myuser' as a tty user so it can run 'screen' with no issues:

sudo vi /etc/group

Modify

tty:x:5:

to look

tty:x:5:
myuser

Create folder for myuser software & logs

su - myuser
sudo mkdir /opt/myuser
sudo mkdir /opt/myuser/logs
su - myuser
sudo mkdir /opt/myuser/logs
sudo mkdir /opt/myuser &&

sudo chown -R myuser:myuser/opt/myuser

Allow user 'myuser' to remotly access the AMI via SSH

cd ~    
mkdir .ssh
chmod 700 .ssh
touch .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
vi .ssh/authorized_keys

Export public key from a key-pair:

On Windows:

Open PuttyGen -> Conversions ->Import Key and import your ".pem" file. Right-click in the UI field labeled "Public key for pasting..." -> Select All ->Copy

On MAC/Linux use command:

ssh-keygen -y -f <.pem-file>

Note: the file should start from ssh-rsa and should end with imported-openssh-key

Paste content (public key) into vi editor, save and close the file.

Extend the user limit: add the below two lines

myuser    hard    nofile      500000
myuser    soft    nofile      500000

to sudo vi /etc/security/limits.conf

Check that you can SSH to the instance with your new user myuser.

Next, reboot and remove the original user - either 'ubuntu' or 'ec2-user'

sudo deluser ubuntu

Optional:

Add a cron job to clean up any our log file older than 14 days

crontab -e

if asked to select an editor for crontab select option '3' - vi.

Add to the crontab file

# Every 4 hours try removing any of our log files older than 14 days back
0 */4 * * * find /opt/myuser/logs -name '*.log.*' -mtime +2 -print -delete

save file and exit the editor.