DEV Community

Cover image for Linux From Zero to Hero
MUHAMMAD ARBAB ANJUM
MUHAMMAD ARBAB ANJUM

Posted on • Edited on

Linux From Zero to Hero

Access Linux Server Remotely Using SSH

ssh -i ./keyPair.pem ubuntu@remote_host

If you want to access the server on your VM, then in VirtualBox, you have to change the network setting from `NAT` to `Bridge Adapter`

# Sometimes you need to install this package to access your server remotely. 

sudo apt install openssh-server
systemctl start ssh

# Check ssh service
systemctl status ssh.service

# Access from Windows:
ssh ubuntu@remote_host
Enter fullscreen mode Exit fullscreen mode

SCP (Secure Copy Protocol)

# from current server to remote
scp /local/file ubuntu@remote_host:/path/

# from remote to current server
scp -v ubuntu@remote_host:/path/ <current_server_path>
Enter fullscreen mode Exit fullscreen mode

Editors

vim Editor

vim abc.txt

Press i -> start editing the file
Esc + d -> to stop editing
esc, shift + :wq -> Save File
:q! -> quit without Save
shift + g -> Move to end of file
small g two times --> Move to top of file
/hello -> will seach the hello word in the file
Press n -> To move to the next search location of that word
?search → use ? when searching from bottom to top
Shift + * → to search on the highlighted word
:%s/searc_word/replace_word/g → Replace word, %s (Substitue), /g (Globally) 
U → undo everything
Ctrl + r → redo everything
Press O → to enter text in the next line where you are currently
Press Shift + O → to enter text in before the line where you are currently
Shift + I → insert in the start of the line
Shift + A → insert in the end of the line
Press r → to replace the selected character
Press d d  → press d two times to delete the line
:e! → revert all the changes
Type 15 and press dd two times to delet 15 lines from the place where your cursor is currently
Press p → to paste the line in the clipboard
Shift + p → paste before the line where you are currently
Shift +v → select the lines you want to copy → press y → to copy the selected lines
:set nu → show line number next to each line in the editor
:set nonu → hide line number next to each line in the editor
:syntax on → show colors in the file → mostly for code purpose
:syntax off → hide colors
:100 → jump to line number 100
Enter fullscreen mode Exit fullscreen mode

User Account Management

View all users

less /etc/passwd

# View with encrypted password
less /etc/shadow
Enter fullscreen mode Exit fullscreen mode

Create user

# Add user
useradd <username>

# Check if user created or not?
id <username>

# Personalize user info while account creation?
useradd -g <group_name> -s /bin/bash -c <comment here> -m -d /home/<user_name> <user_name>

# -g -> Group
# -s -> default shell
# -c -> comment or any message 
# -m -> Want to make home dir or not
# -d -> if -m then -d means directory path

Enter fullscreen mode Exit fullscreen mode

Delete user

userdel <user_name>
userdel -r <user_name> (remove home dir also)
userdel -f <user_name> (force delete even if user is logged in)
Enter fullscreen mode Exit fullscreen mode

Modify user

# Add user to new group but default group will remain same
usermod -G <group_name> <user_name>

# Change default group
usermod -g <group_name> <user_name>

# -L/-U -> Lock or unlock user from login
# -p -> change password OR passed <user_name>
# -s -> Change shell type
# -m -d -> move home content to the new folder

Enter fullscreen mode Exit fullscreen mode

Group Management

Create Group

# New group
groupadd <group_name>

# View all groups
less /etc/group 
Enter fullscreen mode Exit fullscreen mode

Password Aging

For single user:

#One time using 'chage' command
#Making default for every new user by making change in /etc/login.def file

#Format:

chage [-m mindays] [-M maxdays] [-d lastday] [-I inactive] [-E expiredate] [-W warndays] <user_name>

-m -> No of days required a user is allowed to change password
-M -> Max no of days password is valid
-d -> Days since Jan 1, 1970 that password was last changed
-I -> No of days after password expire, account is disable
-E -> Days since Jan 1, 1970 that password was last changed


# Password Aging matrix

less /etc/shadow
username:password:last_pass_change:min_pass_age:max_pass_age:warning_period:inactivity_period:expiration_date:unused

# Example: password valid until 90 days and when 10 days are left throw a warning.
chage -M 90 -W 10 <user_name>

Enter fullscreen mode Exit fullscreen mode

For multiple user:

nano /etc/login.def

PASS_MAX_DAYS 90
PASS_MIN_DAYS 10
PASS_MIN_LEN 8
PASS_WARN_AGE 10
Enter fullscreen mode Exit fullscreen mode

SU and SUDO Command

#switch to the home directory of the login user
su - <user_name> 

#Even after switching user stays in same dir of previous user
su <user_name> 

#switch to root user
su -

# view details of sudoers
less /etc/sudoers

#Update sudoers file from anywhere 
visudo 
Enter fullscreen mode Exit fullscreen mode

Firewall

Tools for managing firewall.

- iptables
- firewalld -> latest now a days
Enter fullscreen mode Exit fullscreen mode

Linux Hardening

Making linux system more secure by turning off things you don't need, limiting access, and add extra layer of protection to keep it safe from hackers. List below to have secure system:

- Principle of Least Privilege (PoLP)
- Minimize Attack Surface
- Keep the System Updated
- Strong Authentication and Password Policies
- Secure Remote Access
- Logging and Monitoring 

**Enable Logging:** Use tools like rsyslog, syslog-ng, or
journald to record system events.

**Log Analysis:** Use intrusion detection tools like
fail2ban or AIDE to monitor logs and detect suspicious activity.

Regularly check system logs and automate alerts for
unusual activities (e.g., /var/log/auth.log).

- Firewall Configuration
- File System Security
- System Auditing
- Disable Unused Network Services and Ports
- File and Directory Permissions
- Kernel Hardening
- Use Security Tools and Enhanced Security Applications
- Data Backup and Recovery
- Physical Security
- Secure Boot Process
- Network Security Configurations
- Intrusion Detection and Prevention
- Authentication Management
Enter fullscreen mode Exit fullscreen mode

Cockpit - Tool

Access your server with help of Web UI

sudo apt install cockpit
access using: http://<server_IP>:9090
Enter fullscreen mode Exit fullscreen mode

LogRotate - Tool

# Log files location in linux
cd /var/log/

# Config Files

/etc/logrotate.conf
/etc/logrotate.d

Log Files Location

/var/log
Enter fullscreen mode Exit fullscreen mode

Top comments (0)