Tag Archives: one liner scripts

One liner scripts to ease your Linux tasks

An assorted collection of one-liner scripts that are helpful in Linux sysadmin’s day to day tasks.

One liner scripts!

In this article, I am consolidating many one-liner scripts that I used or came across which will help you to perform Linux day to day tasks. Great way to save your time in repetitive work ensuring zero human errors!

Setting up hostname in SUSE (older versions)

I always prefer hostnamectl to set hostname in systems running on newer kernels.

# echo myserver.mydomain.com > /etc/HOSTNAME
# sed --in-place 's/preserve_hostname: false/preserve_hostname: true/' /etc/cloud/cloud.cfg #For Cloud servers
# sed --in-place 's/DHCLIENT_SET_HOSTNAME="yes"/DHCLIENT_SET_HOSTNAME="no"/' /etc/sysconfig/network/dhcp
# hostname myserver

Add your hostname instead of myserver and your FQDN domain instead of mydomain.


Setting up nameservers in Linux

# echo "nameserver 10.8.14.33 #Lab nameserver1
nameserver 10.8.17.33 #Lab nameserver2
search lab.kerneltalks.com">>/etc/resolv.conf

Add your own nameserver IPs and search domain in above code.


Add FQDN in hostfile

This applies to server with single IP allocated only.

# echo "`hostname -I` `hostname`.labs.kerneltalks.com `hostname`">>/etc/hosts

Add your own domain instead of labs.kerneltalks.com


Configure sudo so that it asks user’s password when user tries to execute sudo

# sed --in-place 's/Defaults targetpw/#Defaults targetpw/' /etc/sudoers

Remove existing NTP servers and add new in /etc/ntp.conf

# sed -e '/^server/s/^/#/g' -i /etc/ntp.conf
# echo "server 10.8.14.8 #Lab NTP1
server 10.8.14.9 #Lab NTP2">>/etc/ntp.conf

Commands to enable root access in Linux server

Below is a list of the commands you can execute to enable root access on the Cloud server or AWS Linux server.

# sed --in-place 's/PermitRootLogin no/PermitRootLogin yes/' /etc/ssh/sshd_config

# sed --in-place 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
# service sshd restart
# passwd root

If you are doing it on a public cloud server make sure that you reset the root account password since cloud server spin up with key-based authentication and their root does not carry a password initially.


Test port connectivity using telnet and exit in single command

# echo 'exit' | telnet 10.10.0.1 7657
Trying 10.10.0.1...
Connected to 10.10.0.1.
Escape character is '^]'.
Connection closed by foreign host.