50 سوال ابتدایی در مصاحبه لینوکسی که باید بدانید/قسمت اول

1.how to remove files older than 7 days by creating a cron job to run every night?
02 4 * * * /bin/find /path/to/backup/* -type f -mtime +7 -exec rm -rf {} \; \\every day at 4:02 AM
Needed Info:
file1
file2
file3
find . -exec ls '{}' \; :
ls file1
ls file2
ls file3
find . -exec ls '{}' \+ :
ls file1 file2 file3

crontab -l
cat /etc/crontab
crontab -e
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

2.Run a Command that shows all line except any lines starting with the a character # in a file?
cat test | grep -v ^# (grep -v '^[#;]' //grep lines which does not begin with “#” or “;”)

3.Which 2 files contain default values when creating a user with useradd command?
/etc/default/useradd
/etc/login.defs

4.what is the command to create a user with a pre defined uid, shell and home directory?
useradd -m -d /home/user -s /bin/bash -u 9000 user
Result -> grep user /etc/passwd

5.how to delete a user with his home directory?
userdel -r user

6.how to create a user specifying a primary/secondary groups?
useradd user -g primary -G other groups

7.how to change primary group for any user?
usermod -g primarygroup user

8.how can you give a normal user all the root level privileges?
cat /etc/sudoers.d OR visudo
sudo su - sobhan
sudo -l

9.how can you give sudo access to any user without asking him to provide password every time he runs a command?
user ALL=(ALL) NOPASSWD:ALL

10.how to view the users login and logout details?
last user

11.how to lock & unlockthe the user account?
passwd -l & passwd -u

12.what is the command to view and change the expiry date for any user?
chage -l user
chage -E YYYY-MM-DD user

13.what are the fields of /etc/passwd file?
7 fields
sobhan:x:1000:1000:sobhan:/home/sobhan:/bin/bash
1st field: username
2st field: x tells that an encrypted password is stored in /etc/shadow
3st field: uid
4st field: gid
5st field: Description
6st field: home directory
7st field: default login shell

14.what is the difference between .bash_profile and .bashrc?
.bash_profile -> when you login to a linux machine .bash_profile file is executed.
.bashrc? -> if you already logged in and you open a new terminal then .bashrc file is executed.

15.name 3 files which are automatically created inside any users home directory when a user is added?
.bashrc
.bash_profile
.bash_history

16.how to find an error in a file?
grep error /var/log/messages
grep -i error /var/log/messages //case sensitive

17.how to check if a package is installed?
rpm -qa | grep bind

18.where are zone file located for dns?
/var/named

19.how to check files permissions?
ls -l

20.how to find file type of a file?
file filename

21.how to find where passwd command is located?
which,whereis passwd

22.how to count total lines of a file?
wc -l file

23.notes for command:
groupadd group
init 6
/etc/shadow
ps -ef
/etc/ntpd.conf

24.how to view all messages generated by the system since the last reboot on RHEL7/Centos7?
journalctl //use journalctl to view the systemd journal. the boot process, kernel and all systemd services put messages into the systemd journal.
journalctl | grep ssh

25.what are 2 different ways of showing the kernel messages?
dmesg
journalctl -k

26.how can you continously monitor log as they come in?
tail -f /var/log/messages
journalctl -f

27.where can you find messages related to the installation of linux?
anaconda.log
/var/log/anaconda/anaconda.log

28.to improve performance, how can you safely set the limit of process for root to be unlimited (increase max user processes)?
ulimit -u unlimited // Ulimit is the number of open file descriptors per process.

29.where can you set the resource limits for users logged in via pam?
/etc/security/limits.cong

30.how to check ulimit for a user?
ulimit -a //Report all soft resource limits.

31.how to check and increase the limit of opened files in linux?
cat /proc/sys/fs/file-max
to change:
vi /etc/sysctl.conf
(and add line)
fs.file-max=100000
sysctl -p
cat /proc/sys/fs/file-max

32.how to view run time kernel parameters?
sysctl -a //Display all values currently available.

33.how to change runtime kernel parameters for maximum shared segment size in byte?
shmmax - > A shared memory is an extra piece of memory that is attached to some address spaces for their owners to use. As a result, all of these processes share the same memory segment and have access to it.
sysctl -a | grep shmmax
echo kernel.shmmax=1788444444444444444444444 >> /etc/sysctl.conf
sysctl -p

34.how to view boot time parameters and which file is modified to change these parameters?
to view - > cat /proc/cmdline
these are supplied - > /boot/grub2/grub.cfg

35.Fix "Too many open files"
ulimit -a | grep open -> open files (-n) 524288
ulimit -n 524288
Solutaion1:
nano /etc/security/limits.conf file
Description:
A soft limit is like a warning and hard limit is a real max limit.
For example, following will prevent anyone in the student group from having more than 50 processes, and a warning will be given at 30 processes.
<domain> <type> <item> <shell limit value>
domain> can be:
an user name
a group name, with @group syntax
the wildcard *, for default entry
the wildcard %, can be also used with %group syntax, for maxlogin limit
<type> can have the two values:
“soft” for enforcing the soft limits (soft is like warning)
“hard” for enforcing hard limits (hard is a real max limit)
<item> can be one of the following:
core - limits the core file size (KB)
<shell limit value> can be one of the following:
core - limits the core file size (KB)
data - max data size (KB)
fsize - maximum filesize (KB)
memlock - max locked-in-memory address space (KB)
nofile - Maximum number of open file descriptors
rss - max resident set size (KB)
stack - max stack size (KB) - Maximum size of the stack segment of the process
cpu - max CPU time (MIN)
nproc - Maximum number of processes available to a single user
as - address space limit
maxlogins - max number of logins for this user
maxsyslogins - max number of logins on the system
priority - the priority to run user process with
locks - max number of file locks the user can hold
sigpending - max number of pending signals
msgqueue - max memory used by POSIX message queues (bytes)
nice - max nice priority allowed to raise to
rtprio - max realtime priority
chroot - change root to directory (Debian-specific)
@student hard nproc 50
@student soft nproc 30
Hard limits are maintained by the kernel while the soft limits are enforced by the shell.
Solutaion2:
nano /etc/sysctl.conf

36.how to check system load without top command?
w
uptime
//21:04:30 up 15:38, 3 users, load average: 0.00, 0.01, 0.05 - > uptime gives a one line display of the following information. The current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, and 15 minutes.

37.how can you get the physical and virtual memory statistics?
free -m or g
vmstat
-a -> Display active and inactive memory
-d -> Report disk statistics
vmstat -a 2(every 2 sec) 4(4 intervals)

38.how to check cpu utilization and other statistics?
*apt install sysstat
sar -u -> shows cpu utilization stats for the current day
sar -u 2 3 -> (shows realtime cpu stats every 2 seconds with 3 intervals)
sar -r -> memory utilization
sar -S -> swap space

39.how to find process id of a process and kill it immediatley?
pidof command // find the process ID of a running program
kill -9 PID

40.how to list all files opened by a particular command?
lsof -c command\\example lsof -c cat

41.how to list all files opened by specified user?
lsof -u user

42.how can you list all network connections by port 22?
lsof -i :22

43.how to list all created users on the system and send them to a file?
cut -d: -f1 /etc/passwd > users

44.how would you list only the 2nd column from a file?
awk '{print $2}' myfile

45.how to broadcast a messageto all logged -in users?
wall -n "server is going down for 5 minutes"

46.how to create a user with no login access?
useradd -s /sbin/nologin test_user

47.how to schedule a server reboot in 15 minutes?
shutdown -r + 15

48.how to find disk usage by the largest directories?
du -S | sort -n | more //for directories do not include size of subdirectories

49.how to prevent users from deleting other users files in a directory?
* use stickybit
chmod 1777 myfile

50.how to display 10th line of a file only?
tail -10 myfile | head -1

 

Leave a reply:

Your email address will not be published.

Site Footer